Skip to content

Queue Worker ​

Dispatch a job, run a worker, handle retries.

Define Job ​

php
<?php

namespace App\Jobs;

use Nexphant\Queue\Job;

class ProcessOrder extends Job
{
    public int $tries = 3;

    public function __construct(private int $orderId) {}

    public function handle(): void
    {
        $order = DB::table('orders')->find($this->orderId);

        if (!$order) {
            throw new \Exception("Order {$this->orderId} not found");
        }

        // process...
        DB::table('orders')->where('id', $this->orderId)
            ->update(['status' => 'processed']);
    }
}

Push Job ​

php
use Nexphant\Queue\Queue;
use App\Jobs\ProcessOrder;

// from a controller or route handler
Queue::dispatch(new ProcessOrder(orderId: $order->id));

Work ​

bash
php app.php queue:work

Or in code:

php
use Nexphant\Queue\Worker;

$worker = Worker::create(queue: 'default');
$worker->work();

Retry Basics ​

If handle() throws, the job is retried up to $tries times.

php
class ProcessOrder extends Job
{
    public int $tries = 3;
    public int $retryAfter = 30; // wait 30s between retries
}

After all retries fail, the job is moved to the failed jobs table.

Check Failed Jobs ​

bash
php app.php queue:failed

Released under the MIT License.