Skip to content

Latest commit

 

History

History
86 lines (58 loc) · 2.19 KB

usage.md

File metadata and controls

86 lines (58 loc) · 2.19 KB

Usage basics

Configuration

You can configure it with a DI container in the following way:

$logger = $DIContainer->get(\Psr\Log\LoggerInterface::class);

$worker = $DIContainer->get(\Yiisoft\Queue\Worker\WorkerInterface::class);
$loop = $DIContainer->get(\Yiisoft\Queue\Cli\LoopInterface::class);
$adapter = $DIContainer->get(\Yiisoft\Queue\Adapter\AdapterInterface::class);

$queue = new Queue(
    $adapter,
    $worker,
    $loop,
    $logger
);

See also the documentation for concrete adapters (synchronous adapter, AMQP adapter) and workers

Usage

Each job sent to the queue should be defined as a separate class. For example, if you need to download and save a file the class may look like the following:

$data = [
    'url' => $url,
    'destinationFile' => $filename,
];
$message = new \Yiisoft\Queue\Message\Message('file-download', $data);

Here's how to send a task to the queue:

$queue->push($message);

To push a job into the queue that should run after 5 minutes:

// TODO

Important: Not every adapter (such as synchronous adapter) supports delayed execution.

Queue handling

The exact way how a job is executed depends on the adapter used. Most adapters can be run using console commands, which the component registers in your application. For more details check the respective adapter documentation.

Job status

// Push a job into the queue and get a message ID.
$id = $queue->push(new SomeJob());

// Get job status.
$status = $queue->status($id);

// Check whether the job is waiting for execution.
$status->isWaiting();

// Check whether a worker got the job from the queue and executes it.
$status->isReserved($id);

// Check whether a worker has executed the job.
$status->isDone($id);

Limitations

When using queues it is important to remember that tasks are put into and obtained from the queue in separate processes. Therefore, avoid external dependencies when executing a task if you're not sure if they are available in the environment where the worker does its job.

All the data to process the task should be provided with your payload getData() method.