PHP code example of quillstack / queue

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

    

quillstack / queue example snippets


final class SendWelcomeEmail
{
    public function __construct(public readonly string $email)
    {
    }
}

final class SendWelcomeEmailHandler implements Handler
{
    public function __construct(private readonly Mailer $mailer)
    {
    }

    public function handle(object $message): void
    {
        $this->mailer->welcome($message->email);
    }
}

$handlers = new HandlerRegistry();
$handlers->handle(SendWelcomeEmail::class, SendWelcomeEmailHandler::class);

$queue->push(new SendWelcomeEmail('[email protected]'));

$queue->push(new SendReminder($id), 'emails', 3600);
$queue->push(new ChargeCard($id), delay: new DateInterval('PT5M'));

$worker = new Worker($queue, $handlers, $container, tries: 3, backoff: 10);

$worker->runOne();   // one message, or nothing when there is none
$worker->runAll();   // everything due now, and how many there were

$queue->failed();   // the messages which will not be tried again

$queue = new FileQueue(new LocalStorage(), __DIR__ . '/var/queue');

use Quillstack\Db\Connection;
use Quillstack\Queue\Queues\DatabaseQueue;

$queue = new DatabaseQueue(new Connection('pgsql:host=localhost;dbname=app', 'app', $password));
$queue->migrate();   // creates the table if it is not there; safe to run on every deploy

use Quillstack\Queue\Queues\RedisQueue;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$queue = new RedisQueue($redis);

$envelope = $queue->pop();

$queue->ack($envelope);              // handled; the queue may forget it
$queue->release($envelope, 30);      // not now; try again in thirty seconds
$queue->fail($envelope, 'why');      // never; set it aside

$queue = new DatabaseQueue($connection, DatabaseQueue::TABLE, null, visibility: 300);

use Quillstack\Queue\Subscriptions;
use Quillstack\Queue\Topics\QueueTopic;

$subscriptions = (new Subscriptions())
    ->subscribe('orders', 'orders.email')
    ->subscribe('orders', 'orders.ledger')
    ->subscribe('orders', 'orders.warehouse');

$topic = new QueueTopic($queue, $subscriptions);

$topic->publish(new OrderPlaced($id), 'orders');

$handlers
    ->handleOn('orders.email', OrderPlaced::class, SendReceipt::class)
    ->handleOn('orders.ledger', OrderPlaced::class, RecordSale::class);

$topic->publish(new OrderPlaced($id), 'oredrs');   // UnknownTopicException

$connection->transaction(fn () => $topic->publish(new OrderPlaced($id), 'orders'));