1. Go to this page and download the library: Download initphp/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/ */
initphp / queue example snippets
use BabelQueue\Contracts\InboundMessage;
use InitPHP\Queue\Contracts\Handler;
final class SendWelcomeEmail implements Handler
{
public function handle(InboundMessage $message): void
{
$data = $message->getData(); // ['user_id' => 42, 'email' => '...']
// ... do the work. Throwing marks the message as failed.
}
}
use BabelQueue\Codec\EnvelopeCodec;
use InitPHP\Queue\Producer\Producer;
use InitPHP\Queue\Transport\Redis\RedisTransport;
$transport = new RedisTransport(new Predis\Client('tcp://127.0.0.1:6379'));
$producer = new Producer($transport, defaultQueue: 'emails');
// From a URN + pure-JSON data:
$producer->send('urn:babel:users:registered', ['user_id' => 42, 'email' => '[email protected]']);
// worker.php
use InitPHP\Queue\Consumer\Dispatcher;
use InitPHP\Queue\Consumer\Worker;
use InitPHP\Queue\Consumer\WorkerOptions;
use InitPHP\Queue\Routing\HandlerMap;
use InitPHP\Queue\Transport\Redis\RedisTransport;
pts: 3, backoff: [1, 5, 15]);
return new Worker($transport, new Dispatcher($handlers), $options);
$worker->run('emails'); // loop until SIGINT/SIGTERM or a configured limit
$worker->runOnce('emails'); // process at most one message
use InitPHP\Queue\Transport\Pdo\PdoTransport;
use InitPHP\Queue\Transport\Redis\RedisTransport;
use InitPHP\Queue\Transport\Amqp\AmqpTransport;
// Database (no extra broker to run):
$pdo = new PDO('mysql:host=127.0.0.1;dbname=app', 'user', 'pass');
$transport = new PdoTransport($pdo, table: 'jobs');
$transport->createSchema(); // dev/test convenience; see docs for production DDL
// Redis 6.2+ (reliable-queue: BLMOVE/LREM):
$transport = new RedisTransport(new Predis\Client('tcp://127.0.0.1:6379'));
// RabbitMQ:
$connection = new PhpAmqpLib\Connection\AMQPStreamConnection('127.0.0.1', 5672, 'guest', 'guest');
$transport = new AmqpTransport($connection->channel());
new WorkerOptions(
maxAttempts: 5, // total tries before dead-lettering
backoff: [1, 5, 30], // seconds between attempts (last value repeats)
maxJobs: 1000, // stop after N messages (pair with a supervisor)
memoryLimitMb: 128, // stop when memory grows past this
);
use InitPHP\Queue\Consumer\Dispatcher;
use InitPHP\Queue\Consumer\IdempotencyOptions;
use InitPHP\Queue\Consumer\InMemoryIdempotencyStore;
$dispatcher = new Dispatcher(
$handlers,
idempotency: new IdempotencyOptions(new InMemoryIdempotencyStore()),
);
new IdempotencyOptions(
store: $store,
keyPrefix: 'bq:idemp:', // namespaces keys in a shared store
ttl: 86_400, // seconds a processed key is retained (null = forever)
leaseSeconds: 300, // in-flight claim lease (frees a crashed worker's claim)
keyResolver: fn ($m) => $m->getData()['order_id'] ?? null, // optional
);
use InitPHP\Queue\Contracts\IdempotencyStore;
final class RedisIdempotencyStore implements IdempotencyStore
{
public function __construct(private \Redis $redis) {}
public function seen(string $key): bool
{
return (bool) $this->redis->exists($key);
}
public function claim(string $key, int $leaseSeconds = 0): bool
{
// Atomic test-and-set: only the first caller wins the claim.
$options = ['NX'];
if ($leaseSeconds > 0) {
$options['EX'] = $leaseSeconds;
}
return (bool) $this->redis->set($key, '1', $options);
}
public function remember(string $key, ?int $ttl = null): void
{
$ttl !== null && $ttl > 0
? $this->redis->set($key, '1', ['EX' => $ttl])
: $this->redis->set($key, '1');
}
public function forget(string $key): void
{
$this->redis->del($key);
}
}
use BabelQueue\Routing\UnknownUrnStrategy;
new Dispatcher($handlers, UnknownUrnStrategy::DEAD_LETTER);
// FAIL (default) | DELETE | RELEASE | DEAD_LETTER
bash
php bin/queue work --bootstrap=worker.php --queue=emails
# or process exactly one message and exit:
php bin/queue work --bootstrap=worker.php --queue=emails --once
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.