1. Go to this page and download the library: Download laikait/laika-queue library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
laikait / laika-queue example snippets
namespace App\Job;
use Laika\Queue\Abstracts\Job;
class SendWelcomeEmail extends Job
{
public string $queue = 'emails';
public int $maxTries = 3;
public function __construct(protected int $userId) {}
public function handle(): void
{
// send the email
}
public function failed(\Throwable $e): void
{
// log, notify, etc.
}
}
use Laika\Core\Worker\Queue;
use App\Job\SendWelcomeEmail;
Queue::driver()->push(new SendWelcomeEmail($userId), 'emails');
return [
// Which backend the worker runs against: 'database' | 'redis' | 'json'.
'driver' => 'json',
// Connection name (see lf-config/database.php), used when 'driver' or
// 'failed_driver' is 'database'.
'connection' => 'default',
// Failed-job provider: 'database' | 'json'. null mirrors 'driver'.
'failed_driver' => null,
// How long (seconds) a reserved job may sit before a worker treats it as
// stalled and reclaims it. Redis driver only.
'reserve_timeout' => 90,
// Attempt ceiling applied when reclaiming stalled jobs: past this, the job
// goes to the dead set instead of back onto the queue. 0 disables the cap.
// Redis driver only.
'max_tries' => 3,
];
use Laika\Queue\Abstracts\Job;
class ChargeCard extends Job
{
public int $maxTries = 5;
public int $retryAfter = 30;
protected int|array|null $backoffStrategy = [10, 30, 60];
protected bool $jitter = true;
public function __construct(protected string $invoiceId) {}
public function handle(): void
{
// do the work; throw to trigger a retry
}
public function failed(\Throwable $e): void
{
// called after EVERY failed attempt — see the note below
}
}
public function failed(\Throwable $e): void
{
if ($this->tries >= $this->maxTries) {
// genuinely dead
}
}
use Laika\Core\Worker\Queue;
$driver = Queue::driver();
// push(Job $job, string $queue = 'default', int $delay = 0): string
$id = $driver->push(new SendWelcomeEmail($userId), 'emails');
// 10 seconds from now
$driver->push(new SendWelcomeEmail($userId), 'emails', 10);
use Laika\Model\Connection;
use Laika\Queue\Driver\DatabaseDriver;
Connection::add(config('database', 'default'));
$driver = new DatabaseDriver('default'); // a connection NAME, not a PDO instance
$driver->push(new SendWelcomeEmail($userId), 'emails');
public function push(Job $job, string $queue = 'default', int $delay = 0): string;
public function pop(string $queue = 'default'): ?Job;
public function ack(string $id, string $queue = 'default'): void;
public function release(string $id, string $queue = 'default', int $delay = 0): void;
public function delete(string $id, string $queue = 'default'): void;
public function size(string $queue = 'default'): int;
$worker->work(
queue: 'default',
sleep: 3, // seconds to idle when the queue is empty
timeout: 60, // per-job wall-clock limit
memoryLimit: null // MB; null = auto-derive
);
public function log(string $queue, string $payload, \Throwable $e): string;
public function all(?string $queue = null): array;
public function find(string $id): ?array;
public function forget(string $id): bool;
public function flush(?int $hours = null): void;
use Laika\Queue\Schema\QueueModelSchema;
use Laika\Queue\Schema\FailedJobModelSchema;
(new QueueModelSchema())->up(); // laika_queue_jobs
(new FailedJobModelSchema())->up(); // laika_failed_jobs
use Laika\Queue\Abstracts\Job;
Job::registerTrustedClasses([
SendWelcomeEmail::class,
ChargeCard::class,
]);
use Laika\Model\Connection;
use Laika\Queue\Abstracts\Job;
use Laika\Queue\Driver\DatabaseDriver;
use Laika\Queue\Driver\DatabaseFailedJobProvider;
use Laika\Queue\Schema\QueueModelSchema;
use Laika\Queue\Schema\FailedJobModelSchema;
use Laika\Queue\Worker;
Connection::add([
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'myapp',
'username' => 'root',
'password' => 'secret',
]);
Job::registerTrustedClasses([SendWelcomeEmail::class]);
// No laika executable out here, so create the tables directly (idempotent)
(new QueueModelSchema('default'))->up();
(new FailedJobModelSchema('default'))->up();
$driver = new DatabaseDriver('default');
$failer = new DatabaseFailedJobProvider('default');
$driver->push(new SendWelcomeEmail($userId), 'emails', 10);
(new Worker($driver, $failer))->work('emails');
bash
php laika job:make SendWelcomeEmail --queue=emails --max=3
bash
php laika queue:work emails
bash
php laika queue:work emails # or: php worker emails
bash
php laika queue:failed # what broke?
php laika queue:retry <id> # fix the cause, then re-push one
php laika queue:retry --all --queue=emails
php laika queue:flush --hours=168 # prune anything older than a week
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.