Skip to content

nexphant/queue ​

Job queue with workers, retry configurations, and graceful drain support.

Install ​

bash
composer require nexphant/queue

Define a Job Handler ​

Job handlers extend Nexphant\Queue\JobHandler and implement the handle method:

php
use Nexphant\Queue\JobHandler;
use Nexphant\Queue\Job;

class SendEmailHandler extends JobHandler
{
    public function handle(array $payload, Job $job): void
    {
        $to = $payload['to'];
        $subject = $payload['subject'];
        
        // Process the background task
        mailer()->send($to, $subject);
    }
}

Setup and Register ​

Create a queue instance via QueueFactory and register the handler:

php
use Nexphant\Queue\QueueFactory;

$queue = QueueFactory::create([
    'driver' => 'database',
    'database' => $pdoConnection, // PDO connection instance
    'max_attempts' => 3,
    'retry_delay' => 60,
]);

// Register handler mapping a job name
$queue->register('send_email', SendEmailHandler::class);

Drivers ​

QueueFactory::create() can auto-select a driver, or you can choose one explicitly:

php
$memory = QueueFactory::create('memory');
$file = QueueFactory::create('file', ['path' => storage_path('queue')]);
$apcu = QueueFactory::create('apcu', ['prefix' => 'app_queue']);
$ring = QueueFactory::create('apcu_ring', ['scan_limit' => 2048]);
$redis = QueueFactory::create('redis', ['redis' => $redis]);
$database = QueueFactory::create('database', ['pdo' => $pdo]);

Driver options:

DriverRequiredCommon Options
memorynoneIn-process queue for tests and local jobs.
filewritable filesystempath, max_payload_size, max_file_size.
databasepdo or database PDO instancetable, dead_letter_table.
apcuext-apcuprefix, max_payload_size.
apcu_ring / lock_freeext-apcuprefix, max_payload_size, scan_limit.
redisredis Redis instanceprefix, max_payload_size.

Dispatch Jobs ​

Push payload data using the registered job name:

php
// Dispatch immediately
$queue->push('send_email', [
    'to' => 'user@example.com',
    'subject' => 'Welcome to Nexphant',
]);

// Dispatch with a delay (in seconds)
$queue->later(60, 'send_email', [
    'to' => 'user@example.com',
    'subject' => 'Delayed Welcome',
]);

Run Workers ​

Start the worker process loop:

php
// Start worker loop listening on default queue
$queue->work();

Run a single job in tests or scripts:

php
$processed = $queue->workOnce(workerId: 1);

Worker and queue controls:

php
$status = $queue->status();
$metrics = $queue->metrics()->toArray();
$failed = $queue->failed(limit: 50);
$queue->retry($jobId);
$queue->stop();

QueueWorker wraps a long-running worker process with signal handling and limits:

php
use Nexphant\Queue\QueueWorker;

$worker = new QueueWorker($queue, [
    'max_jobs' => 1000,
    'max_time' => 3600,
    'memory_limit' => 256 * 1024 * 1024,
    'sleep' => 0.5,
]);

$worker->start();

Job Handler Lifecycle Hooks ​

The JobHandler abstract class provides lifecycle hooks that can be overridden:

php
class SendEmailHandler extends JobHandler
{
    public function handle(array $payload, Job $job): void { }

    public function before(Job $job): void
    {
        // Called before execution
    }

    public function after(Job $job, mixed $result): void
    {
        // Called after successful execution
    }

    public function failed(Job $job, \Throwable $e): void
    {
        // Called when execution fails
    }
}

Job Options ​

push() and later() accept per-job options:

OptionDescription
max_attemptsMaximum retries before the job is moved to dead letters.
timeoutJob execution timeout in seconds.
metadataExtra metadata stored with the job. Runtime context is added automatically when available.

Queue config also supports workers, retry_delay, timeout, memory_limit, poll_interval, metrics_interval, verbose, and quiet.

Released under the MIT License.