atlasmobile/yii2-queue

yii2 队列组件

维护者

详细信息

github.com/AtlasGit/yii2-queue

源代码

安装次数: 5,246

依赖: 0

建议者: 0

安全性: 0

星标: 2

关注者: 5

分支: 11

类型:yii2-extension

1.1.5 2016-06-01 11:57 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:14:39 UTC


README

此组件提供简单的队列包装器

要求

Redis

yii2-redis

安装

通过 composer 安装此扩展是首选方式。

运行以下命令之一

composer require --prefer-dist "atlasmobile/yii2-queue=*"

或者

"atlas/yii2-queue": "*"

将以下内容添加到您的 composer.json 文件的 require 部分。

应用配置

要使用此扩展,只需将以下代码添加到您的应用配置中

return [
    //....
    'components' => [
        'queue' => [
            'class' => \atlasmobile\queue\RedisQueue::class,
        ],
        'redis' => [
            'class' => \yii\redis\Connection::class,
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0
        ],
        
        'controllerMap' => [
            'queue' => \atlasmobile\queue\console\controllers\QueueController::class,
        ],
    ],
];

第一个任务

首先创建一个 Job 处理类

namespace console\jobs;

class MyJob implements \atlasmobile\queue\QueueHandler
{
    public function run(\atlasmobile\queue\Job $job, $data)
    {
        //process $data;
        var_dump($data);
    }
} 

或者

namespace console\jobs;

class MyJob extends \atlasmobile\queue\BaseTask
{
	public function beforeRun(Job $job, QueuePayload $payload) {
		//todo before running task
	}

    public function run(\atlasmobile\queue\Job $job, $data)
    {
        //process $data;
        var_dump($data);
    }
    
    public function afterRun(Job $job, QueuePayload $payload) {
    	//todo after running task
    }
    
    public function onFail(Job $job, QueuePayload $payload, \Exception $exception) {
    	//todo what to do on fail running task
    }
} 

直接将任务推送到队列

// You can use component directly or static method to push job to queue: 
\atlasmobile\queue\helpers\Queue::push($job, $data = null, $queue = 'default', $options = [])

// Push job to the default queue and execute "run" method
Yii::$app->queue->push(\console\jobs\MyJob::class, ['a', 'b', 'c']); 

// or push it and execute any other method
Yii::$app->queue->push('\console\jobs\MyJob@myMethod', ['a', 'b', 'c']);

// or push it to some specific queue
Yii::$app->queue->push(\console\jobs\MyJob::class, ['a', 'b', 'c'], 'myQueue');

// or both
Yii::$app->queue->push('\console\jobs\MyJob@myMethod', ['a', 'b', 'c'], 'myQueue');

从 1.0.5 版本开始支持延迟任务

// Just a string
Yii::$app->queue->pushDelayed(\console\jobs\MyJob::class, '+3 hours', ['a', 'b', 'c']);
 
// Or \DateTime
$dt = new \DateTime('now');
$dt->modify('+1 week')->modify('+3 days');
Yii::$app->queue->pushDelayed(\console\jobs\MyJob::class, $dt, ['a', 'b', 'c']);


// Or oldschool
Yii::$app->queue->pushDelayed(\console\jobs\MyJob::class, time() + 86400, ['a', 'b', 'c']);

监听器

如果您想使用 supervisor,请放置以下配置

[program:yiiqueue]
command=php /path/to/project/yii queue/listen
process_name=%(program_name)s_%(process_num)02d
numprocs=4  ; customize workers
directory=/path/to/project
autostart=true
autorestart=true
user=nginx ; executor user
stdout_logfile=/path/to/project/runtime/logs/queue.out.log
stdout_logfile_maxbytes=10MB
stderr_logfile=/path/to/project/runtime/logs/queue.err.log
stderr_logfile_maxbytes=10MB

队列监听器示例

# Process a first job from default queue and than exit the process
./yii queue/work

# continuously process jobs from default queue
./yii queue/listen

# process a job from specific queue and than exit the process
./yii queue/work --queue=queueName

# continuously process jobs from specific queue
./yii queue/listen --queue=myQueue

延迟监听器示例(自 1.0.5 版本起)

# Pop list of delayed jobs and if "time to work", puts them to queue
./yii queue/delayed

# Or for supervisor or just for "&"
./yii queue/listen-delayed

默认情况下,方法 listen-delayed 每隔 30 秒检查一次新的延迟任务,但您可以设置 --pollFreqSecs=MY_SECONDS

您还可以将失败的作业存储到数据库中

首先,运行迁移以创建包含失败作业的表

 ./yii queue/failed-table

然后

./yii queue/listen --storeFailedJobs=true

然后,经过一段时间,当表被失败作业填充后,执行以下操作

./yii queue/failed 

此命令将以 FIFO(先进先出)顺序将所有失败作业添加到队列中

清除包含失败作业的表

./yii queue/failed-flush

监控

在某些情况下,您可能想查看当前队列中有多少以及/或是什么任务。要查看基本信息,请使用

./yii queue/monitor