PHP code example of phpsoftbox / queue

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

    

phpsoftbox / queue example snippets


use PhpSoftBox\Queue\Drivers\InMemoryDriver;
use PhpSoftBox\Queue\ProgressAwareInterface;
use PhpSoftBox\Queue\QueueJob;
use PhpSoftBox\Queue\Worker;

$queue = new InMemoryDriver();
$queue->push(
    QueueJob::fromPayload(['type' => 'email', 'id' => 10])
        ->withMutex('tenant:1:company:15:import', 7200),
);

$worker = new Worker($queue, maxAttempts: 3);
$worker->run(function (mixed $payload, QueueJob $job, ProgressAwareInterface $progress): void {
    $progress->setTotal(100);
    $progress->increment();
    // обработка задания
});

use PhpSoftBox\Database\Configurator\DatabaseFactory;
use PhpSoftBox\Database\Connection\ConnectionManager;
use PhpSoftBox\Queue\Drivers\DatabaseDriver;
use PhpSoftBox\Queue\DatabaseQueueSchema;

$factory = new DatabaseFactory([
    'connections' => [
        'default' => 'main',
        'main' => [
            'read' => ['dsn' => 'sqlite:///:memory:'],
            'write' => ['dsn' => 'sqlite:///:memory:'],
        ],
    ],
]);

$queue = new DatabaseDriver(new ConnectionManager($factory), new DatabaseQueueSchema(), 'main');

use PhpSoftBox\Queue\ProgressStoreFactory;

$factory = new ProgressStoreFactory();

// Общая БД для HTTP-процесса и worker. $connections — ConnectionManagerInterface.
$database = $factory->create([
    'progress_driver' => 'database',
    'progress_connection' => 'default',
], $connections);

// Только внутри текущего процесса и экземпляра объекта.
$memory = $factory->create(['progress_driver' => 'memory']);

// Явное отключение наблюдения за прогрессом и запросов отмены.
$disabled = $factory->create(['progress_driver' => 'null']);

$worker = new Worker($queue, resetState: static fn () => $container->get(ServicesResetter::class)->reset());