1. Install the Yii2 queue extension by running the following command
composer require --prefer-dist yiisoft/yii2-queue
2. Check composer.json; the installation was successful
"require": {
"yiisoft/yii2-queue": "^2.1"
},
3. Register multiple queue components in the console by editing \console\config\main.php
"require": {
"yiisoft/yii2-queue": "^2.1"
},
4. Configure the multiple queue components by editing \environments\dev\common\config\main-local.php and \environments\prod\common\config\main-local.php
'copyAssetQueue' => [ // 复制资源文件队列
'class' => 'yii\queue\redis\Queue',
'redis' => 'redis', // Redis 连接组件或它的配置
'channel' => 'cpa:queue:copy:asset', // 队列键前缀
'ttr' => 10 * 60, // 作业处理的最长时间,单位(秒)
'as log' => 'yii\queue\LogBehavior',
],
'qqCwVideoMultipartUploadQueue' => [ // 企鹅号的内容网站应用的视频文件分片上传队列
'class' => 'yii\queue\redis\Queue',
'redis' => 'redis', // Redis 连接组件或它的配置
'channel' => 'cpa:queue:qq:cw:video:multipart:upload', // 队列键前缀
'ttr' => 5 * 60, // 作业处理的最长时间,单位(秒)
'as log' => 'yii\queue\LogBehavior',
],
'pubArticleQueue' => [ // 发布文章队列
'class' => 'yii\queue\redis\Queue',
'redis' => 'redis', // Redis 连接组件或它的配置
'channel' => 'cpa:queue:pub:article', // 队列键前缀
'ttr' => 5 * 60, // 作业处理的最长时间,单位(秒)
'as log' => 'yii\queue\LogBehavior',
],
'sourceCallbackQueue' => [ // 来源回调队列
'class' => 'yii\queue\redis\Queue',
'redis' => 'redis', // Redis 连接组件或它的配置
'channel' => 'cpa:queue:source:callback', // 队列键前缀
'ttr' => 5 * 60, // 作业处理的最长时间,单位(秒)
'as log' => 'yii\queue\LogBehavior',
],
5. Define the base class for each task sent to the queue. Create a new file at \common\jobs\Job.php
<?php
/**
* Created by PhpStorm.
* User: terryhong
* Date: 2018/9/7
* Time: 下午4:54
*/
namespace common\jobs;
use yii\base\BaseObject;
use yii\queue\JobInterface;
/**
* 队列的任务的基础类
*
* @author Qiang Wang <shuijingwanwq@163.com>
* @since 1.0
*/
class Job extends BaseObject implements JobInterface
{
public function execute($queue){
}
}
6. Each task sent to the queue should be defined as a separate class. Based on the official example, if you need to download and save a file, the class might look like this
<?php
/**
* Created by PhpStorm.
* User: Qiang Wang
* Date: 2018/10/18
* Time: 10:39
*/
namespace common\jobs;
/**
* 下载并保存一个文件
*
* @author Qiang Wang <shuijingwanwq@163.com>
* @since 1.0
*/
class DownloadJob extends Job
{
public $url;
public $file;
public function execute($queue)
{
file_put_contents($this->file, file_get_contents($this->url));
}
}
7. Define the corresponding download class in the qq application. Create a new file at \qq\jobs\DownloadJob.php
<?php
/**
* Created by PhpStorm.
* User: Qiang Wang
* Date: 2018/10/19
* Time: 10:28
*/
namespace qq\jobs;
/**
* 下载并保存一个文件
*
* @author Qiang Wang <shuijingwanwq@163.com>
* @since 1.0
*/
class DownloadJob extends \common\jobs\DownloadJob
{
}8. Test the job status by editing \qq\rests\qq_cw_app\IndexAction.php
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace qq\rests\qq_cw_app;
use Yii;
use qq\jobs\DownloadJob;
use yii\data\ActiveDataProvider;
use yii\base\InvalidConfigException;
/**
*
* For more details and usage information on IndexAction, see the [guide article on rest controllers](guide:rest-controllers).
*
* @author Qiang Wang <shuijingwanwq@163.com>
* @since 1.0
*/
class IndexAction extends \yii\rest\IndexAction
{
/**
* Prepares the data provider that should return the requested collection of the models.
* @return ActiveDataProvider
* @throws InvalidConfigException if the configuration is invalid.
*/
protected function prepareDataProvider()
{
// 将作业推送到队列并获得其ID
$id = Yii::$app->copyAssetQueue->push(new DownloadJob([
'url' => 'http://wjdev2.chinamcloud.com:8661/robots.txt',
'file' => './qq/runtime/robots.txt',
]));
// 这个作业等待执行
var_dump(Yii::$app->copyAssetQueue->isWaiting($id));
// Worker 从队列获取作业,并执行它
var_dump(Yii::$app->copyAssetQueue->isReserved($id));
// Worker 作业执行完成
var_dump(Yii::$app->copyAssetQueue->isDone($id));
echo $id;
exit;
}
}9. Open the URL: http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps?group_id=spider . The printed results show that the task was not executed
bool(true)
bool(false)
bool(false)
1
10. Check Redis; the queue has been stored, as shown in Figure 1

600;O:19:"qq\jobs\DownloadJob":2:{s:3:"url";s:45:"http://wjdev2.chinamcloud.com:8661/robots.txt";s:4:"file";s:23:"./qq/runtime/robots.txt";}
11. The info command prints information about the queue status; one task is waiting
.\yii copy-asset-queue/info
Jobs
- waiting: 1
- delayed: 0
- reserved: 0
- done: 0
12. The run command fetches and executes tasks in a loop until the queue is empty, as shown in Figure 2

.\yii copy-asset-queue/run --verbose=1 --isolate=1 --color=0
.\yii copy-asset-queue/info
2018-10-19 11:07:30 [pid: 36016] - Worker is started
2018-10-19 11:07:30 [2] qq\jobs\DownloadJob (attempt: 1, pid: 36016) - Started
2018-10-19 11:07:30 [2] qq\jobs\DownloadJob (attempt: 1, pid: 36016) - Done (0.255 s)
2018-10-19 11:07:30 [pid: 36016] - Worker is stopped (0:00:00)
Jobs
- waiting: 0
- delayed: 0
- reserved: 0
- done: 1
13. Check Redis; the keys cpa:queue:copy:asset.messages and cpa:queue:copy:asset.waiting have both been deleted, as shown in Figure 3

14. The file \qq\runtime\robots.txt has been saved, as shown in Figure 4

15. To make the development process more friendly, you can add a panel to the Yii2 debug module. The panel displays counters and a list of queue tasks. Edit \environments\dev\api\config\main-local.php, \environments\dev\backend\config\main-local.php, \environments\dev\frontend\config\main-local.php, \environments\dev\qq\config\main-local.php, \environments\dev\rpc\config\main-local.php, and \environments\dev\wx\config\main-local.php
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
'panels' => [
'queue' => 'yii\queue\debug\Panel',
],
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
16. View the Yii2 debug module; the queue panel has been added, as shown in Figure 5

17. The local environment is Windows 10. I am preparing to deploy to the development, testing, staging, and production environments based on Supervisor. The latter four are all CentOS 7. Supervisor is a process monitor for Linux. It will automatically start your console processes.
\build\c_files\etc\supervisord.d\yii-copy-asset-queue-worker.ini
[program:yii-copy-asset-queue-worker]
process_name = %(program_name)s_%(process_num)02d
command = php /sobey/www/channel-pub-api/yii copy-asset-queue/listen --verbose=1 --color=0
autostart = true
autorestart = true
startsecs = 1
stopwaitsecs = 10
numprocs = 4
redirect_stderr = true
stderr_logfile = /data/logs/yii-copy-asset-queue-worker-stderr.log
stdout_logfile = /data/logs/yii-copy-asset-queue-worker-stdout.log
\build\c_files\etc\supervisord.d\yii-qq-cw-video-multipart-upload-queue-worker.ini
[program:yii-qq-cw-video-multipart-upload-queue-worker]
process_name = %(program_name)s_%(process_num)02d
command = php /sobey/www/channel-pub-api/yii qq-cw-video-multipart-upload-queue/listen --verbose=1 --color=0
autostart = true
autorestart = true
startsecs = 1
stopwaitsecs = 10
numprocs = 4
redirect_stderr = true
stderr_logfile = /data/logs/yii-qq-cw-video-multipart-upload-queue-worker-stderr.log
stdout_logfile = /data/logs/yii-qq-cw-video-multipart-upload-queue-worker-stdout.log
\build\c_files\etc\supervisord.d\yii-pub-article-queue-worker.ini
[program:yii-pub-article-queue-worker]
process_name = %(program_name)s_%(process_num)02d
command = php /sobey/www/channel-pub-api/yii pub-article-queue/listen --verbose=1 --color=0
autostart = true
autorestart = true
startsecs = 1
stopwaitsecs = 10
numprocs = 4
redirect_stderr = true
stderr_logfile = /data/logs/yii-pub-article-queue-worker-stderr.log
stdout_logfile = /data/logs/yii-pub-article-queue-worker-stdout.log
\build\c_files\etc\supervisord.d\yii-source-callback-queue-worker.ini
[program:yii-source-callback-queue-worker]
process_name = %(program_name)s_%(process_num)02d
command = php /sobey/www/channel-pub-api/yii source-callback-queue/listen --verbose=1 --color=0
autostart = true
autorestart = true
startsecs = 1
stopwaitsecs = 10
numprocs = 4
redirect_stderr = true
stderr_logfile = /data/logs/yii-source-callback-queue-worker-stderr.log
stdout_logfile = /data/logs/yii-source-callback-queue-worker-stdout.log
18. After the container upgrade is complete, execute the command, as shown in Figure 6

php /sobey/www/channel-pub-api/init --env=Development --overwrite=All
php /sobey/www/channel-pub-api/yii migrate --interactive=0
19. Open the URL: http://wjdev2.chinamcloud.com:8661/qq/v1/qq-cw-apps?group_id=spider . The printed results show that the task was not executed
bool(true)
bool(false)
bool(false)
1
20. The info command prints information about the queue status; one task is waiting
php /sobey/www/channel-pub-api/yii copy-asset-queue/info
Jobs
- waiting: 1
- delayed: 0
- reserved: 0
- done: 0
21. Manually run the queue/listen command to listen for and process queue tasks
php /sobey/www/channel-pub-api/yii copy-asset-queue/listen --verbose=1 --color=0
2018-10-19 15:56:04 [pid: 1151] - Worker is started
2018-10-19 15:56:05 [5] qq\jobs\DownloadJob (attempt: 1, pid: 1151) - Started
2018-10-19 15:56:05 [5] qq\jobs\DownloadJob (attempt: 1, pid: 1151) - Error (0.031 s)
> yii\base\ErrorException: file_put_contents(./qq/runtime/robots.txt): failed to open stream: No such file or directory
^C2018-10-19 15:56:13 [pid: 1151] - Worker is stopped (0:00:09)
22. Test the job status by editing \qq\rests\qq_cw_app\IndexAction.php and setting file to an absolute path
// 将作业推送到队列并获得其ID
$id = Yii::$app->copyAssetQueue->push(new DownloadJob([
'url' => 'http://wjdev2.chinamcloud.com:8661/robots.txt',
'file' => '/sobey/www/channel-pub-api/qq/runtime/robots.txt',
]));
23. After upgrading to the development environment, I manually ran the queue/listen command to listen for and process queue tasks. The queue task executed successfully, but the Supervisor configuration did not take effect; it did not automatically process tasks in the queue, which remained waiting
php /sobey/www/channel-pub-api/yii copy-asset-queue/listen --verbose=1 --color=0
2018-10-19 17:04:57 [pid: 1717] - Worker is started
2018-10-19 17:04:57 [10] qq\jobs\DownloadJob (attempt: 1, pid: 1717) - Started
2018-10-19 17:04:57 [10] qq\jobs\DownloadJob (attempt: 1, pid: 1717) - Done (0.021 s)
^C2018-10-19 17:05:01 [pid: 1717] - Worker is stopped (0:00:04)
24. The local environment is Windows 10. I am preparing to deploy to the development, testing, staging, and production environments based on Supervisor. The latter four are all CentOS 7. Supervisor is a process monitor for Linux. It will automatically start your console processes. Adjust the value of startsecs to 0
\build\c_files\etc\supervisord.d\yii-copy-asset-queue-worker.ini
[program:yii-copy-asset-queue-worker]
process_name = %(program_name)s_%(process_num)02d
command = php /sobey/www/channel-pub-api/yii copy-asset-queue/listen --verbose=1 --color=0
autostart = true
autorestart = true
startsecs = 0
stopwaitsecs = 10
numprocs = 4
redirect_stderr = true
stderr_logfile = /data/logs/yii-copy-asset-queue-worker-stderr.log
stdout_logfile = /data/logs/yii-copy-asset-queue-worker-stdout.log
\build\c_files\etc\supervisord.d\yii-qq-cw-video-multipart-upload-queue-worker.ini
[program:yii-qq-cw-video-multipart-upload-queue-worker]
process_name = %(program_name)s_%(process_num)02d
command = php /sobey/www/channel-pub-api/yii qq-cw-video-multipart-upload-queue/listen --verbose=1 --color=0
autostart = true
autorestart = true
startsecs = 0
stopwaitsecs = 10
numprocs = 4
redirect_stderr = true
stderr_logfile = /data/logs/yii-qq-cw-video-multipart-upload-queue-worker-stderr.log
stdout_logfile = /data/logs/yii-qq-cw-video-multipart-upload-queue-worker-stdout.log
\build\c_files\etc\supervisord.d\yii-pub-article-queue-worker.ini
[program:yii-pub-article-queue-worker]
process_name = %(program_name)s_%(process_num)02d
command = php /sobey/www/channel-pub-api/yii pub-article-queue/listen --verbose=1 --color=0
autostart = true
autorestart = true
startsecs = 0
stopwaitsecs = 10
numprocs = 4
redirect_stderr = true
stderr_logfile = /data/logs/yii-pub-article-queue-worker-stderr.log
stdout_logfile = /data/logs/yii-pub-article-queue-worker-stdout.log
\build\c_files\etc\supervisord.d\yii-source-callback-queue-worker.ini
[program:yii-source-callback-queue-worker]
process_name = %(program_name)s_%(process_num)02d
command = php /sobey/www/channel-pub-api/yii source-callback-queue/listen --verbose=1 --color=0
autostart = true
autorestart = true
startsecs = 0
stopwaitsecs = 10
numprocs = 4
redirect_stderr = true
stderr_logfile = /data/logs/yii-source-callback-queue-worker-stderr.log
stdout_logfile = /data/logs/yii-source-callback-queue-worker-stdout.log
25. The Supervisor configuration has taken effect and will automatically process tasks in the queue. The creation time of the /sobey/www/channel-pub-api/qq/runtime/robots.txt file is constantly updating, as shown in Figure 7

