L
L
Leonid Gorshkov2021-09-02 17:01:59
Laravel
Leonid Gorshkov, 2021-09-02 17:01:59

Laravel, queue, database. How to repeat the same task?

How to repeat the same task?
In the task, if I myself call throw to stop its execution. But I need to add the same job back to the queue. The release($daily = 0) method does not fire inside the failed( Exception $exception) method of the Job class, but does fire in the provider

public function boot()
    {
        Queue::failing(function (JobFailed $event) {
            if ($event->job->getQueue() == SendApiJob::QUEUE_NAME) {
                // Если задача выполняется больше N раз
                if($event->job->attempts() >= 3){
                    // Отправить пользователю уведомление о том что попытки отправить api провалились
                    logger()->alert('Превышен лимит повторных выполнений заданий в очереди ' . SendApiJob::QUEUE_NAME);
                }else{
                    $event->job->release(10);
                }
            }
        });
    }

After it is added to the tasks again and starts execution, an Exception arrives such a "MaxAttemptsExceededException" saying that the maximum number of attempts to complete the task has been exceeded. ($tries=999)

How to be?

The throttle method suggested by laravel doesn't add the task back to the queue either
Redis::throttle('redis_queue_throttle')->allow(1)->every(5)->then(function () {
                // code ...
                throw new \Exception("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1);
            }, function () {
                // Could not obtain lock...
                return $this->release(5);
            });

The task is as follows: You need to send an http request, if it fails then send it later. According to the limit of attempts.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2021-09-02
@Synacs-U

https://laravel.com/docs/8.x/queues#time-based-attempts wrong?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question