commit | author | age
|
2207d6
|
1 |
<p align="center"> |
W |
2 |
<a href="https://github.com/yiisoft" target="_blank"> |
|
3 |
<img src="https://avatars0.githubusercontent.com/u/993323" height="100px"> |
|
4 |
</a> |
|
5 |
<h1 align="center">Yii2 Queue Extension</h1> |
|
6 |
<br> |
|
7 |
</p> |
|
8 |
|
|
9 |
An extension for running tasks asynchronously via queues. |
|
10 |
|
|
11 |
It supports queues based on **DB**, **Redis**, **RabbitMQ**, **AMQP**, **Beanstalk**, **ActiveMQ** and **Gearman**. |
|
12 |
|
|
13 |
Documentation is at [docs/guide/README.md](docs/guide/README.md). |
|
14 |
|
|
15 |
[![Latest Stable Version](https://poser.pugx.org/yiisoft/yii2-queue/v/stable.svg)](https://packagist.org/packages/yiisoft/yii2-queue) |
|
16 |
[![Total Downloads](https://poser.pugx.org/yiisoft/yii2-queue/downloads.svg)](https://packagist.org/packages/yiisoft/yii2-queue) |
|
17 |
[![Build Status](https://travis-ci.org/yiisoft/yii2-queue.svg?branch=master)](https://travis-ci.org/yiisoft/yii2-queue) |
|
18 |
|
|
19 |
Installation |
|
20 |
------------ |
|
21 |
|
|
22 |
The preferred way to install this extension is through [composer](http://getcomposer.org/download/): |
|
23 |
|
|
24 |
``` |
|
25 |
php composer.phar require --prefer-dist yiisoft/yii2-queue |
|
26 |
``` |
|
27 |
|
|
28 |
Basic Usage |
|
29 |
----------- |
|
30 |
|
|
31 |
Each task which is sent to queue should be defined as a separate class. |
|
32 |
For example, if you need to download and save a file the class may look like the following: |
|
33 |
|
|
34 |
```php |
|
35 |
class DownloadJob extends BaseObject implements \yii\queue\JobInterface |
|
36 |
{ |
|
37 |
public $url; |
|
38 |
public $file; |
|
39 |
|
|
40 |
public function execute($queue) |
|
41 |
{ |
|
42 |
file_put_contents($this->file, file_get_contents($this->url)); |
|
43 |
} |
|
44 |
} |
|
45 |
``` |
|
46 |
|
|
47 |
Here's how to send a task into the queue: |
|
48 |
|
|
49 |
```php |
|
50 |
Yii::$app->queue->push(new DownloadJob([ |
|
51 |
'url' => 'http://example.com/image.jpg', |
|
52 |
'file' => '/tmp/image.jpg', |
|
53 |
])); |
|
54 |
``` |
|
55 |
To push a job into the queue that should run after 5 minutes: |
|
56 |
|
|
57 |
```php |
|
58 |
Yii::$app->queue->delay(5 * 60)->push(new DownloadJob([ |
|
59 |
'url' => 'http://example.com/image.jpg', |
|
60 |
'file' => '/tmp/image.jpg', |
|
61 |
])); |
|
62 |
``` |
|
63 |
|
|
64 |
The exact way a task is executed depends on the used driver. Most drivers can be run using |
|
65 |
console commands, which the component automatically registers in your application. |
|
66 |
|
|
67 |
This command obtains and executes tasks in a loop until the queue is empty: |
|
68 |
|
|
69 |
```sh |
|
70 |
yii queue/run |
|
71 |
``` |
|
72 |
|
|
73 |
This command launches a daemon which infinitely queries the queue: |
|
74 |
|
|
75 |
```sh |
|
76 |
yii queue/listen |
|
77 |
``` |
|
78 |
|
|
79 |
See the documentation for more details about driver specific console commands and their options. |
|
80 |
|
|
81 |
The component also has the ability to track the status of a job which was pushed into queue. |
|
82 |
|
|
83 |
```php |
|
84 |
// Push a job into the queue and get a message ID. |
|
85 |
$id = Yii::$app->queue->push(new SomeJob()); |
|
86 |
|
|
87 |
// Check whether the job is waiting for execution. |
|
88 |
Yii::$app->queue->isWaiting($id); |
|
89 |
|
|
90 |
// Check whether a worker got the job from the queue and executes it. |
|
91 |
Yii::$app->queue->isReserved($id); |
|
92 |
|
|
93 |
// Check whether a worker has executed the job. |
|
94 |
Yii::$app->queue->isDone($id); |
|
95 |
``` |
|
96 |
|
|
97 |
For more details see [the guide](docs/guide/README.md). |