PHP code example of codemonster-ru / queue

1. Go to this page and download the library: Download codemonster-ru/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/ */

    

codemonster-ru / queue example snippets


use Codemonster\Queue\Contracts\JobInterface;
use Codemonster\Queue\QueueManager;

final class SendWelcomeEmail implements JobInterface
{
    public function handle(): void
    {
        // Send the email.
    }
}

$manager = new QueueManager([
    'default' => 'sync',
    'connections' => [
        'sync' => [
            'driver' => 'sync',
        ],
    ],
]);

$manager->connection()->push(new SendWelcomeEmail());

$manager = new QueueManager([
    'default' => 'redis',
    'connections' => [
        'redis' => [
            'driver' => 'redis',
            'host' => '127.0.0.1',
            'port' => 6379,
            'database' => 0,
            'prefix' => 'queue:',
            'retry_after' => 60,
            'max_attempts' => 3,
        ],
    ],
]);

use Codemonster\Queue\Contracts\WorkableQueueInterface;
use Codemonster\Queue\Worker;

/** @var WorkableQueueInterface $queue */
$queue = $manager->connection('database');
$queue->push(new SendWelcomeEmail());

(new Worker($queue))->workOnce();

use Codemonster\Queue\Contracts\JobOptionsInterface;

final class SendWelcomeEmail implements JobOptionsInterface
{
    public function handle(): void
    {
        // Send the email.
    }

    public function maxAttempts(): int
    {
        return 5;
    }

    public function backoff(): int|array
    {
        return [10, 30, 120];
    }

    public function timeout(): int
    {
        return 30;
    }
}

$failed = $manager->failedJobs('database');

foreach ($failed->all() as $job) {
    echo $job->id() . ': ' . $job->exception();
}

$failed->retry('1');
$failed->retryAll();
$failed->flush();