tarantool/queue
PHP bindings for Tarantool Queue (LuaRock). Connect to a Tarantool instance and work with tubes: put tasks, consume/reserve/ack/bury/release, inspect stats, and call custom queue methods. Install via Composer; requires a configured running Tarantool server.
Installation:
composer require tarantool/queue
Ensure your composer.json includes "minimum-stability": "dev" if using pre-release versions.
Configuration:
Add to .env:
QUEUE_CONNECTION=tarantool
TARANTOOL_QUEUE_HOST=127.0.0.1
TARANTOOL_QUEUE_PORT=3301
TARANTOOL_QUEUE_USER=guest
TARANTOOL_QUEUE_PASSWORD=
TARANTOOL_QUEUE_DB=test
First Job: Define a job class:
namespace App\Jobs;
use Tarantool\Queue\Job;
use Tarantool\Queue\ShouldQueue;
class ProcessPayment implements ShouldQueue
{
use Dispatchable, InteractsWithQueue;
public $paymentId;
public function __construct($paymentId)
{
$this->paymentId = $paymentId;
}
public function handle()
{
// Logic here
}
}
Dispatch a Job:
ProcessPayment::dispatch($paymentId);
Run the Queue Worker:
php artisan queue:work tarantool --sleep=3 --tries=3
config/queue.php for connection settings.app/Jobs/ for reusable job logic.Job Dispatching:
dispatch() or dispatchNow() for synchronous/asynchronous execution.ProcessPayment::dispatch($paymentId)->onQueue('high_priority');
ProcessPayment::dispatch($paymentId)->delay(now()->addMinutes(10));
Queue Workers:
php artisan queue:work tarantool --queue=high_priority
php artisan queue:work tarantool --daemon --sleep=3 --tries=3 &
php artisan queue:work tarantool --daemon --sleep=3 --tries=3 &
supervisord to manage persistent workers.Job Monitoring:
failed_jobs table (if using database) or Tarantool’s admin interface.config/queue.php:
'log' => env('QUEUE_LOG', true),
'log_level' => env('QUEUE_LOG_LEVEL', 'info'),
Retry Logic:
app/Exceptions/Handler.php:
public function register()
{
$this->reportable(function (JobFailedException $e) {
if ($e->attempts() >= 3) {
// Log or notify
}
});
}
Laravel Events: Convert events to jobs for async processing:
event(new PaymentProcessed($paymentId));
// In listener:
ProcessPayment::dispatch($paymentId);
Tarantool-Specific Features:
Tarantool\Queue\Job::call().serialize()/unserialize() in jobs for complex data.Testing:
Queue::fake() for unit tests:
public function test_job_dispatch()
{
Queue::fake();
ProcessPayment::dispatch(123);
Queue::assertPushed(ProcessPayment::class);
}
docker run -p 3301:3301 tarantool/tarantool
Connection Issues:
waiting state or worker crashes.TARANTOOL_QUEUE_HOST/PORT in .env. Use --verbose in worker:
php artisan queue:work tarantool --verbose
TARANTOOL_CONNECT_TIMEOUT in config if network latency is high.Job Serialization:
ShouldQueue trait and implement serialize()/unserialize():
public function serialize()
{
return [
'payment_id' => $this->paymentId,
'user_id' => $this->user->id, // Ensure user is loaded
];
}
Failed Jobs:
php artisan queue:retry to retry failed jobs.php artisan queue:flush
-- In Tarantool console:
box.space._queue:drop()
Worker Stalling:
--timeout in worker (default: 60s):
php artisan queue:work tarantool --timeout=120
--memory to limit memory usage:
php artisan queue:work tarantool --memory=128M
Enable Debug Mode:
QUEUE_DEBUG=true
Logs will show job lifecycle events.
Inspect Queue:
box.space._queue:select()
php artisan queue:list
Custom Middleware:
namespace App\Jobs\Middleware;
class LogJobExecution
{
public function handle($job, $next)
{
\Log::info("Job started: {$job->job}");
$next($job);
\Log::info("Job completed: {$job->job}");
}
}
app/Console/Kernel.php:
protected $middleware = [
\App\Jobs\Middleware\LogJobExecution::class,
];
Custom Queue Connection:
Extend TarantoolQueue for custom logic:
namespace App\Queues;
use Tarantool\Queue\TarantoolQueue as BaseQueue;
class CustomTarantoolQueue extends BaseQueue
{
public function pushRaw($job, $data, $queue = null)
{
// Custom logic (e.g., add metadata)
return parent::pushRaw($job, $data, $queue);
}
}
Register in config/queue.php:
'connections' => [
'tarantool' => [
'driver' => 'custom-tarantool',
// ...
],
],
Job Events: Listen for job events globally:
use Tarantool\Queue\Events\JobProcessed;
use Tarantool\Queue\Events\JobFailed;
Event::listen(JobProcessed::class, function ($event) {
// Log success
});
Event::listen(JobFailed::class, function ($event) {
// Notify Slack/email
});
Tarantool Lua Extensions: Write Lua scripts for Tarantool to process jobs natively:
-- Save as /path/to/process_payment.lua
local function process_payment(payment_id)
-- Tarantool logic here
end
Call from PHP:
$result = \DB::connection('tarantool')->selectOne('process_payment', [$paymentId]);
How can I help you explore Laravel packages today?