1. Go to this page and download the library: Download utopia-php/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/ */
utopia-php / queue example snippets
topia\Queue;
use Utopia\Queue\Consumer;
use Utopia\Queue\Message;
$createConsumer = static function (): Consumer {
return new Queue\Broker\Redis(
receive: new Queue\Connection\Redis('redis'),
commands: new Queue\Connection\Redis('redis'),
);
};
// Adapter is transport only (process count + namespace). Queue and concurrency
// are defined on job().
$adapter = new Queue\Adapter\Swoole($createConsumer, workerNum: 12);
$server = new Queue\Server($adapter);
$server
->job('my-queue', 1)
->inject('message')
->action(function (Message $message) {
var_dump($message);
});
$server
->error()
->inject('error')
->action(function ($error) {
echo $error->getMessage() . PHP_EOL;
});
$server
->workerStart()
->action(function () {
echo "Worker Started" . PHP_EOL;
});
$server->start();
// Publish with the same broker API
$publisher = new Queue\Broker\Redis(
receive: new Queue\Connection\Redis('redis'),
commands: new Queue\Connection\Redis('redis'),
);
$publisher->publish(new Queue\Queue('my-queue'), [
'type' => 'test_number',
'value' => 123,
]);
use Utopia\NATS\Connection;
use Utopia\Queue\Broker\Nats;
use Utopia\Queue\Queue;
// Pass a Closure so each forked worker / pooled lease resolves its own connection:
// a socket cannot survive a fork, and a pooled lease needs one of its own.
$broker = new Nats(
fn (): Connection => Connection::connect('nats://127.0.0.1:4222'),
ackWait: 30.0, // redelivery window if a worker dies before commit()
maxDeliver: 5, // delivery attempts before a message is dead-lettered
);
$broker->publish(new Queue('my-queue'), ['type' => 'test_number', 'value' => 123]);
use Utopia\Queue\PermanentFailure;
$server->job('v1-region-manager')->action(function (array $payload) use ($regions) {
$hostname = $regions[$payload['region']] ?? null;
if ($hostname === null) {
// No attempt will find it. Dead-letter now rather than in 21 minutes.
throw new PermanentFailure("Region hostname not configured: {$payload['region']}");
}
// ...
});
use Utopia\Queue\Broker\Provisioning;
$maintenance = new Nats($source, provisioning: Provisioning::Require);
$maintenance->retry(new Queue('my-queue')); // moves messages, changes no configuration
$broker->provision(new Queue('my-queue')); // streams + work consumer, no message
if ($coroutines > 1 && $consumer instanceof Consumer\Exclusive) {
$coroutines = 1; // and log why
}
use Utopia\Queue\Broker\Redis as Broker;
use Utopia\Queue\Codec\Compat;
use Utopia\Queue\Codec\Igbinary;
$broker = new Broker(
receive: $receive,
commands: $commands,
codec: new Compat(new Igbinary()),
);
use Swoole\Coroutine;
use Utopia\Queue\Broker\Background;
$publisher = new Background(
$broker,
capacity: 512,
coroutines: 1,
timeout: 0.1,
maxBatchInterval: 0.01,
maxBatchSize: 100,
);
Coroutine\run(function () use ($publisher, $queue, $payload): void {
$publisher->start();
$publisher->enqueue($queue, $payload);
$publisher->shutdown();
});
use Utopia\Queue;
use Utopia\Queue\Consumer;
use Utopia\Queue\Message;
$createConsumer = static function (): Consumer {
return new Queue\Broker\Redis(
receive: new Queue\Connection\Redis('redis'),
commands: new Queue\Connection\Redis('redis'),
);
};
$adapter = new Queue\Adapter\Swoole($createConsumer, workerNum: 1);
$server = new Queue\Server($adapter);
$server
->job('v1-functions', 8)
->inject('message')
->action(function (Message $message) {
// Handle a functions job
});
$server
->job('database_db_main', 1)
->inject('message')
->action(function (Message $message) {
// Handle a databases job
});
// Each consume loop calls the factory so blocking receive does not share a connection.
$server->error()->inject('error')->action(function ($error) {
echo $error->getMessage() . PHP_EOL;
});
$server->start();
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.