comradefuzz/catena

基于Redis的队列式后台作业系统

安装: 7

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

1.0.0 2017-05-23 14:31 UTC

This package is not auto-updated.

Last update: 2024-09-29 01:34:08 UTC


README

resque启发,基于Redis的后台队列作业系统

安装

推荐通过 composer 安装此扩展。

运行以下命令之一

php composer.phar require --prefer-dist comradefuzz/catena "*"

或者

"comradefuzz/catena": "*"

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

注意

模块处于开发模式,不应在生产环境中使用

用法

按照以下方式配置您的应用程序

 'bootstrap' => ['catena'],
 'components' => [
    'queue' => 'comradefuzz\catena\components\Queue',
 ],
 'modules' => [
        'catena' => [
            'class' => 'comradefuzz\catena\Module',
            'autoRespawn' => false, // If set worker groups will be kept in actual state by respawning dead workers
                                    // (defaults `false`)
            'enableRegulars' => false,  // If this is set and `regularJobs` is not empty, jobs will be automatically set
                                        // to queues with specified interval (defaults `false`)
            'redis' => [
                // Redis connection
                'connection' => [
                    'class' => '\yii\redis\Connection',
                    'hostname' => 'redis',
                ],
                'namespace' => 'catena', // prefix all module data has (defaults to `catena`)
            ],
            // Worker groups configuration
            'workers' => [
                [
                    'queues' => 'harder', // define queues workers are listening to
                    'count' => 3, // specify workers count in group (more workers means faster processing)
                    'sleep' => 15, // number of seconds each worker sleeps if all jobs are done
                    'memoryLimit' => 3 * 1024 * 1024 // bytes of RAM allowed to use to each worker in group
                ],
                [
                    'queues' => 'better,stronger', // you can define several queues to worker group
                    'count' => 2
                ],
                ['queues' => 'faster'],
            ],
            'regularJobs' => [
                [
                    '\app\jobs\RegularJob', // Job class
                    'queue' => 'harder',    // Queue to place job
                    'args' => ['foo' => 'bar'], // Job arguments
                    'interval' => 15    // Interval in seconds in which job will be enqueued
                ],
                [
                    '\app\jobs\RegularJob',
                    'queue' => 'harder',
                    'args' => ['foo' => 'bazzz'],
                    'interval' => 15,
                    'descriptor' => 'another', // Allows to have several regular jobs of one class with different arguments
                ],
            ]
        ]
    ],

定义作业类

<?php
namespace app\jobs;

use comradefuzz\catena\models\BaseJob;

class MyJob extends BaseJob
{
    public $foo;
    public $bar;

    public function rules()
    {
       return [
           [['foo', 'bar'], 'string'],
           ['foo', 'required'],
       ];
    }
    public function perform()
    {
        \Yii::info("Job is done!");
    }
}

现在您可以在后台运行此作业

Yii::$app->queue->push(new \app\jobs\MyJob(['foo' => 'bar']), 'heavy');

然后启动catena守护进程 yii catena/start。使用 yii catena/status 查看模块状态。使用 yii catena/stop 停止守护进程。