A
A
Arseny Sokolov2018-09-21 08:04:36
Laravel
Arseny Sokolov, 2018-09-21 08:04:36

How to not add duplicate jobs to the queue in Laravel?

I can't find a solution to avoid adding duplicate jobs to the queue. The client now has the ability to execute tasks from the queue in only 1 thread, so a situation often arises when an important task is added, and there other tasks hang in front of an important task, which are essentially the same, i.e. if they perform a general analytics recalculation.
Is it possible to understand that such a task already exists in the system and not add it, so that queues of the same ones do not multiply, preventing more important tasks from being performed from the same queue?
About the software:
Laravel 5.6
PHP 7.1
MySQL 5.7
Queue in the database

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Morningstar_T, 2018-09-21
@Morningstar_T

Throw in the database, then when adding a task, compare for a duplicate.

K
Konstantin T, 2018-09-21
@RooTooZ

I recorded the answer for you in video format, I hope it is intelligible

  • Unique issue type key
  • Add completed_at field
  • G
    goodman0907, 2018-09-22
    @goodman0907

    You can do as the authors of previous answers suggest, but if you need to change the connection type, then this solution will not work.
    You can wrap adding to the queue in a command and run through the scheduler. It has a special method that allows you to check if the previous (same) command was completed before running it again.
    Like withoutOverlapping.
    Another option.
    Create different queues for tasks with different priorities. And pass them to the handler in the required sequence.
    php artisan queue:work --queue=high,low
    One has to look in each case to see what works best.

    Didn't find what you were looking for?

    Ask your question

    Ask a Question

    731 491 924 answers to any question