Why yii2 queues work only triggers: Queue::EVENT_BEFORE_PUSH and Queue::EVENT_AFTER_PUSH?

Good day,

Configured support acarda as in the documentation:

https://github.com/yiisoft/yii2-queue

The queue work fine. As the driver indicated 'db' and created a mysql table like in the dock

Next in basics:

https://github.com/yiisoft/yii2-queue/blob/master/...

Trying to listen to events. I am more interested in follow:

Queue::EVENT_AFTER_EXEC
ExecEvent
After each successful job


But not able to get into it but all otrabatyvaet successfully.

./yii queue/listen --color --verbose --isolate
----------------------
2018-10-09 14:43:10 [156] common\jobs\DownloadJob (attempt: 1, pid: 4297) - Started
2018-10-09 14:43:11 [156] common\jobs\DownloadJob (attempt: 1, pid: 4297) - Done (0.379 s)

Catch:

EVENT_BEFORE_PUSH
EVENT_AFTER_PUSH

Don't catch:

EVENT_BEFORE_EXEC
EVENT_AFTER_EXEC
EVENT_AFTER_ERROR
EVENT_WORKER_START
EVENT_WORKER_LOOP
EVENT_WORKER_STOP

Example of capture:

$afterExecAlert = function($event) {
 throw new \Exception('Hurra - EVENT_AFTER_EXEC');
};
Yii::$app->queue->on(Queue::EVENT_AFTER_EXEC, $afterExecAlert);

Yii::$app->queue->push(new DownloadJob([
 'url' => 'https://picsum.photos/200/500',
 'file' => $full_path,
 ]));


Config:

'queue' => [
 'class' => \yii\queue\db\Queue::class,
 'db' => 'db', // DB connection component or its config
 'tableName' => '{{%queue}}', // Table name
 'channel' => 'default', // Queue channel key
 'mutex' => \yii\mutex\MysqlMutex::class, // Mutex used to sync queries
 'as log' => \yii\queue\LogBehavior::class
 ],


Please tell me what is the reason?
March 12th 20 at 08:00
1 answer
March 12th 20 at 08:02
Sending and receiving messages between processes. You hang the handler EVENT_AFTER_EXEC in the process that sends messages. The worker process doesn't know about that, namely he triggered EVENT_AFTER_EXEC.

Execute the event handler in the form of Behavior-and ( like LogBehavior), and plug it into the General config. This will solve the problem.
Thank you, if I understand correctly, it is necessary so:

'queue' => [
 'class' => \yii\queue\db\Queue::class,
 'db' => 'db', // DB connection component or its config
 'tableName' => '{{%queue}}', // Table name
 'channel' => 'default', // Queue channel key
 'mutex' => \yii\mutex\MysqlMutex::class, // Mutex used to sync queries
 'as log' => \yii\queue\LogBehavior::class,
 'on '.yii\queue\Queue::EVENT_BEFORE_EXEC => function($event) {
 throw new \Exception('oops');
}
 ],


In throw new \Exception('oops'); I do not get.

I may not understand the terminology, I just Laravel came to Yii2 recently.

And another point - if globally to hang still succeed, then that handler will handle absolutely all completed successfully workeri. But what if the logic of such completions may differ? - April commented on March 12th 20 at 08:05
if I understand correctly, it is necessary so


Technically, Yes, your variant will work. But to write the code in configs is a bad practice. For this is the Behavior-s. In terms of Laravel and Symfony is something like EventListener. In Yii2 there is no separate event-Manager. Dispetcherizaciya occurs at the level of any class inherited from `\yii\base\Component`. Created heir `\yii\base\Behavior`, in which you want to implement event handlers. And is connected to the config:

'queue' => [
 'class' => \yii\queue\db\Queue::class,
 'as xxx' => \...\MyQueueBehavior::class,
],


And another point - if globally to hang still succeed, then that handler will handle absolutely all completed successfully workeri. But what if the logic of such completions may differ?


Yes, absolutely everything. It is forwarding to the handler parameter of type `\yii\queue\JobEvent` in it, among other things, the link is processed the job. On it and build a condition that the treating and what not. - Maia.Harber32 commented on March 12th 20 at 08:08
Thanks, but a bit hard to understand.

So can we or not multiple handlers in the config to be hung?

Is there any example in the documentation with the behavior? - April commented on March 12th 20 at 08:11
a bit hard to understand.


It's basic principles. Behavior is well described in the documentation of yii2. - Maia.Harber32 commented on March 12th 20 at 08:14
Here 's how to create behavior. In one behavior on one event you can hang only one handler. But to a component you can connect several behaviors that one and the same event will hang their handlers. Logs from LogBehavior also written through the events. - Maia.Harber32 commented on March 12th 20 at 08:17
With behaviors it is clear, they cling to the components or models of how end-to-end functionality.

1) I created a behavior with the event EVENT_AFTER_EXEC (where the handler?)
2) which component to hook this event? - April commented on March 12th 20 at 08:20
To a component queue, as described above:

'queue' => [
 'class' => \yii\queue\db\Queue::class,
 'as xxx' => \...\MyQueueBehavior::class // <--
],
- Maia.Harber32 commented on March 12th 20 at 08:23

Find more questions by tags YiiMySQL