PHP code example of not-empty / omniq
1. Go to this page and download the library: Download not-empty/omniq 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/ */
not-empty / omniq example snippets
declare(strict_types=1);
use Omniq\Client;
port: 6379,
);
$jobId = $omniq->publish(
queue: 'demo',
payload: ['hello' => 'world'],
timeoutMs: 30000,
);
echo $jobId . PHP_EOL;
declare(strict_types=1);
use Omniq\Client;
erializable
{
public function __construct(
public string $orderId,
public int $amount,
public string $currency,
) {
}
public function jsonSerialize(): array
{
return [
'order_id' => $this->orderId,
'amount' => $this->amount,
'currency' => $this->currency,
];
}
}
$omniq = new Client(
host: 'omniq-redis',
port: 6379,
);
$jobId = $omniq->publishJson(
queue: 'orders',
payload: new OrderCreated(
orderId: 'ORD-1',
amount: 1000,
currency: 'USD',
),
maxAttempts: 5,
timeoutMs: 60000,
);
declare(strict_types=1);
use Omniq\Client;
use Omniq\JobCtx;
mniq->consume(
queue: 'demo',
handler: static function (JobCtx $ctx): void {
echo 'processing=' . $ctx->jobId . PHP_EOL;
sleep(2);
echo 'done=' . $ctx->jobId . PHP_EOL;
},
verbose: true,
drain: false,
);
$omniq->consume(
queue: 'demo',
handler: static function (JobCtx $ctx): void {
$isLastAttempt = $ctx->attempt >= $ctx->maxAttempts;
var_dump($isLastAttempt);
},
);
declare(strict_types=1);
use Omniq\Client;
use Omniq\QueueMonitor;
= new QueueMonitor($omniq);
$queues = $monitor->scanQueues();
var_dump($queues);
$omniq->retryFailed('demo', '01ABC...');
$results = $omniq->retryFailedBatch('demo', ['01A...', '01B...']);
$omniq->removeJob('demo', '01ABC...', 'failed');
$results = $omniq->removeJobsBatch('demo', 'failed', ['01A...', '01B...']);
$omniq->pause('demo');
$paused = $omniq->isPaused('demo');
$omniq->resume('demo');
$omniq->publish(
queue: 'demo',
payload: ['i' => 1],
gid: 'company:acme',
groupLimit: 1,
);
$omniq->publish(
queue: 'demo',
payload: ['i' => 2],
);
$omniq->consume(
queue: 'documents',
handler: static function (JobCtx $ctx): void {
if (is_array($ctx->payload)) {
$payload = $ctx->payload;
} else {
$payload = [];
}
$completionKey = (string) $payload['document_id'];
$pages = (array) $payload['pages'];
$ctx->exec->childsInit($completionKey, count($pages));
foreach ($pages as $page) {
$ctx->exec->publish(
queue: 'pages',
payload: [
'completion_key' => $completionKey,
'page' => $page,
],
);
}
},
);
$omniq->consume(
queue: 'pages',
handler: static function (JobCtx $ctx): void {
if (is_array($ctx->payload)) {
$payload = $ctx->payload;
} else {
$payload = [];
}
$completionKey = (string) $payload['completion_key'];
$remaining = $ctx->exec->childAck($completionKey);
echo 'remaining=' . $remaining . PHP_EOL;
},
);