Z
Z
zifmezin2021-11-20 23:24:18
Yii
zifmezin, 2021-11-20 23:24:18

How to send notification of successfully completed Yii2 Queue worker?

Friends, please help me solve the problem.
There is a GridView in which I implemented the ability to send multiple rows to a task.
Each line (id and title) is sent to a separate task, where a long API request is made.
In the worker, data is received via the API and stored in the corresponding (by id) model.

Everything would be fine, but for the third day I can not decide - how to notify in the interface that such and such a line has received data? You won't understand until you refresh the page and find this row in the table.

It is logical that you can hang the yii\queue\Queue::EVENT_BEFORE_EXEC event and refresh the page by sending session->setFlash(), which I tried to do:
In the config:

'on '.yii\queue\Queue::EVENT_BEFORE_EXEC => function($event) {
                $model = new Model;
                $model->trigger(Model::EVENT_NAME);
            },

In the model:
public function init()
    {
        $this->on(Model::EVENT_NAME, function(){
            Yii::$app->session->setFlash('success', "Ok");
        });
        parent::init();
    }

But this scheme does not work, since there is no session in console.

Next, I tried to get around this by calling an action in the web application controller (disabling Csrf in the process):
'on '.yii\queue\Queue::EVENT_BEFORE_EXEC => function($event) {
                Yii::$app->controllerNamespace = "backend\controllers";
                Yii::$app->runAction('default/event');
            },

But it also fails, because the redirect() method in this action cannot be called from a console application.

While writing, he suddenly realized the delusional nature of his thoughts)) how stupid it would be to send a request to update the page while you are using the site.

It turns out that you definitely need to do this without reloading the page - which means most likely using ajax.
How are such problems generally solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2021-11-21
@KingstonKMS

So they decide, using ajax or a web socket.
You can store a queue in the database, when pushing a task you get an id and then check the queue for completion.
More about queues

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question