yiisoft / yii2-queue
Yii2 Queue Extension which supports queues based on DB, Redis, RabbitMQ, Beanstalk, SQS, and Gearman
Fund package maintenance!
yiisoft
Open Collective
Tidelift
Installs: 7 934 856
Dependents: 162
Suggesters: 11
Security: 0
Stars: 1 069
Watchers: 78
Forks: 295
Open Issues: 67
Type:yii2-extension
Requires
- php: >=5.5.0
- symfony/process: ^3.3||^4.0||^5.0||^6.0||^7.0
- yiisoft/yii2: ~2.0.14
Requires (Dev)
- aws/aws-sdk-php: >=2.4
- cweagans/composer-patches: ^1.7
- enqueue/amqp-lib: ^0.8||^0.9.10||^0.10.0
- enqueue/stomp: ^0.8.39||^0.10.0
- opis/closure: *
- pda/pheanstalk: ~3.2.1
- php-amqplib/php-amqplib: ^2.8.0||^3.0.0
- phpunit/phpunit: 4.8.34
- yiisoft/yii2-debug: ~2.1.0
- yiisoft/yii2-gii: ~2.2.0
- yiisoft/yii2-redis: ~2.0.0
Suggests
- ext-gearman: Need for Gearman queue.
- ext-pcntl: Need for process signals.
- aws/aws-sdk-php: Need for aws SQS.
- enqueue/amqp-lib: Need for AMQP interop queue.
- enqueue/stomp: Need for Stomp queue.
- pda/pheanstalk: Need for Beanstalk queue.
- php-amqplib/php-amqplib: Need for AMQP queue.
- yiisoft/yii2-redis: Need for Redis queue.
This package is auto-updated.
Last update: 2024-11-08 16:42:06 UTC
README
Yii2 Queue Extension
An extension for running tasks asynchronously via queues.
It supports queues based on DB, Redis, RabbitMQ, AMQP, Beanstalk, ActiveMQ and Gearman.
Documentation is at docs/guide/README.md.
Installation
The preferred way to install this extension is through composer:
php composer.phar require --prefer-dist yiisoft/yii2-queue
Basic Usage
Each task which is sent to 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:
class DownloadJob extends BaseObject implements \yii\queue\JobInterface { public $url; public $file; public function execute($queue) { file_put_contents($this->file, file_get_contents($this->url)); } }
Here's how to send a task into the queue:
Yii::$app->queue->push(new DownloadJob([ 'url' => 'http://example.com/image.jpg', 'file' => '/tmp/image.jpg', ]));
To push a job into the queue that should run after 5 minutes:
Yii::$app->queue->delay(5 * 60)->push(new DownloadJob([ 'url' => 'http://example.com/image.jpg', 'file' => '/tmp/image.jpg', ]));
The exact way a task is executed depends on the used driver. Most drivers can be run using console commands, which the component automatically registers in your application.
This command obtains and executes tasks in a loop until the queue is empty:
yii queue/run
This command launches a daemon which infinitely queries the queue:
yii queue/listen
See the documentation for more details about driver specific console commands and their options.
The component also has the ability to track the status of a job which was pushed into queue.
// Push a job into the queue and get a message ID. $id = Yii::$app->queue->push(new SomeJob()); // Check whether the job is waiting for execution. Yii::$app->queue->isWaiting($id); // Check whether a worker got the job from the queue and executes it. Yii::$app->queue->isReserved($id); // Check whether a worker has executed the job. Yii::$app->queue->isDone($id);
For more details see the guide.