2017 © Pedro Peláez
 

yii2-extension yii2-queue-driver-conductor

Conductor driver for Yii2 Queue Extension which supported DB, Redis, RabbitMQ, Beanstalk and Gearman

image

macfly/yii2-queue-driver-conductor

Conductor driver for Yii2 Queue Extension which supported DB, Redis, RabbitMQ, Beanstalk and Gearman

  • Friday, March 16, 2018
  • by Macfly
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Conductor Driver

yiisoft/yii2-queue is an extension for running tasks asyncronously via queues., (*1)

This package is a new driver to make it works with netflix/conductor workflow (for push) and task (for worker)., (*2)

you'll find example how to use features which are specific to it. For more details see yii2-queue guide., (*3)

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

php composer.phar require --prefer-dist macfly/yii2-queue-driver-conductor

or add, (*6)

"macfly/yii2-queue-driver-conductor": "*"

to the require section of your composer.json file., (*7)

Workflow usage

Configuration example to push job to a conductor workflow:, (*8)

return [
    'bootstrap' => [
        'workflow', // The component registers own console commands
    ],
    'components' => [
        'conductor' => [
            'class'     => \yii\queue\conductor\components\Conductor::class,
            'baseUrl'   => 'http://127.0.0.1:8080',
            // ...
        ],
        'workflow' => [
            'class' => \yii\queue\conductor\Workflow::class,
            'name' => 'kitchensink', // Name of the workflow to trigger (https://netflix.github.io/conductor/metadata/kitchensink/)
            'version' => 12, // Optional. If not specified uses the latest version of the workflow (https://netflix.github.io/conductor/runtime/#with-input-only)
            'correlationId' => 'my_id_to_help_me_find_the_flow_trigger_by_that_component', // Optional. User supplied Id that can be used to retrieve workflows (https://netflix.github.io/conductor/runtime/#with-input-only)
            'taskToDomain' => ['task_1' => 'dev'], // Optional. see https://netflix.github.io/conductor/domains/ for more detail on domain usage.
            'supportDelayTtr' => true, // Optional (default: false). By default conductor doesn't not support delay and ttr, like the way they are used in yii2-queue, but we can make them work if the worker is also based on that driver and you're not overriding the serializer.
        ],
    ],
];

Usage in code

If you want to use model as a workflow input. For example, if you want to create a model that will run the Kitchensink Example workflow the class may look like the following:, (*9)

class KitchensinkModel extends \yii\base\Model
{
    public $mod;
    public $oddEven;
    public $task2Name;
}

Here's how to send a task into workflow:, (*10)

Yii::$app->workflow->push(new KitchensinkModel([
    'task2Name' => 'task_5',
]));

Pushes job into queue that run after 5 min:, (*11)

Yii::$app->workflow->delay(5 * 60)->push(new KitchensinkModel([
    'task2Name' => 'task_5',
]));

Important: see supportDelayTtr workflow component option to make that driver support delayed running., (*12)

Messaging third party workers

You may pass any data to workflow:, (*13)

Yii::$app->workflow->push([
    'task2Name' => 'task_5',
]);

This is useful if the queue is processed using a specially developer third party worker. In that case you should set supportDelayTtr to false or modify your worker to manage ttr and delay. They will be in the task input, so you need to get them and manage them., (*14)

Console

Console is used to manage workflow., (*15)

yii workflow/clear

clear command terminate all workflow of that type., (*16)

yii workflow/remove [id]

remove command terminate a workflow., (*17)

Task worker usage

Configuration example to process job from conductor task:, (*18)

return [
    'bootstrap' => [
        'task', // The component registers own console commands
    ],
    'components' => [
        'conductor' => [
            'class'     => \yii\queue\conductor\components\Conductor::class,
            'baseUrl'   => 'http://127.0.0.1:8080',
            // ...
        ],
        'task' => [
            'class' => \yii\queue\conductor\Task::class,
            'name' => 'task_1', // Name of the queue we want to bind on (in that example we will proceed the job of hez first task of the kitchensink workflow example https://netflix.github.io/conductor/metadata/kitchensink/)
            'model' => \app\models\TestTask::class, // Optional, if task are not send by the workflow component of that driver, we need to know which model will manage and proceed the data.
            'domain' => 'dev' // Optional, domain of task we want to proceed (see https://netflix.github.io/conductor/domains/ for more detail on domain usage).
        ],
    ],
];

Usage in code

Each task which is get from the queue should match an object or a model class (or the default one which is define in the task component will be used). For example, if you want to create a model that will run the first task (task_1) Kitchensink Example workflow, the class may look like the following:, (*19)

class TestTask extends \yii\base\Model implements \yii\queue\JobInterface
{
    public $mod;
    public $oddEven;
    public $env;

    public function execute($queue)
    {
        echo "Do the job you want to do here\n";
    }
}

Console

Console command is used to execute tasks., (*20)

yii task/listen [timeout]

listen command launches a worker in daemon which infinitely queries the queue. If there are new tasks they're immediately obtained and executed. timeout parameter is number of seconds to wait a job. This method is most efficient when command is properly daemonized via supervisor or systemd., (*21)

yii task/run

run command obtains and executes tasks in a loop until queue is empty. Works well with cron., (*22)

run and listen commands have options:, (*23)

  • --verbose, -v: print executing statuses into console.
  • --isolate: verbose mode of a job execute. If enabled, execute result of each job will be printed.
  • --color: highlighting for verbose mode.
yii task/create [model]

create command create/update task definition of the given model. Model need to implement TaskDefInterface or has TaskDefBehavior attached., (*24)

Here is model example that support TaskDefBehavior:, (*25)

class TestTask extends \yii\base\Model implements \yii\queue\JobInterface
{
    public $mod;
    public $oddEven;
    public $env;

    public function behaviors()
    {
        return [
            \yii\queue\conductor\behaviors\TaskDefBehavior::className(),
        ];
    }

    public function execute($queue)
    {
        echo "Do the job you want to do here\n";
    }
}
yii task/clear

clear command clears a queue., (*26)

yii task/remove [id]

remove command removes a job., (*27)

The Versions

16/03 2018

dev-master

9999999-dev

Conductor driver for Yii2 Queue Extension which supported DB, Redis, RabbitMQ, Beanstalk and Gearman

  Sources   Download

BSD-3-Clause

The Requires

 

by Charles DELFLY

rabbitmq redis yii queue beanstalk async db gearman conductor