yii2-extensions / queue
Yii2 Queue Extension which supported DB, Redis, RabbitMQ, Beanstalk, SQS and Gearman
Fund package maintenance!
yiisoft
Open Collective
Tidelift
Installs: 15
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 1
Type:yii2-extension
Requires
- php: >=8.1
- laravel/serializable-closure: ^v1.3.0
- symfony/process: ^6.3
- yiisoft/yii2: ^2.2
Requires (Dev)
- aws/aws-sdk-php: >=2.4
- enqueue/amqp-bunny: ^0.10.0
- enqueue/amqp-ext: ^0.10.8
- enqueue/amqp-lib: ^0.10.0
- enqueue/stomp: ^0.10.0
- maglnet/composer-require-checker: ^4.6
- pda/pheanstalk: ^v5.0.0
- php-amqplib/php-amqplib: ^3.0.0
- phpunit/phpunit: ^10.3.0
- yii2-extensions/debug: dev-main
- yii2-extensions/gii: dev-main
- yii2-extensions/phpstan: dev-main
- yii2-extensions/redis: dev-main
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-30 11:03:11 UTC
README
Queue
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.
Requirements
- PHP 8.1 or higher.
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.