C
C
cryptodoxer2018-05-27 14:42:44
Laravel
cryptodoxer, 2018-05-27 14:42:44

How to add only one task to laravel queue?

There are long tasks, from 2 to 30 minutes. It is necessary that the queue does not grow if there is already a task in the worker.
What do we have.
Kernel.php

$schedule->command('source')->withoutOverlapping()->everyMinute();

command
$count = Redis::lrange('queues:source', 0, -1);

if (count($count) == 0)
    dispatch((new \App\Jobs\Source())->onConnection('redis')->onQueue('source'));

Job for long worker example
public function handle()
{
    dump('job work');
    sleep(2000);
}

But even so I see new tasks in the queue that hang on a timeout. That is, the task is added anyway, regardless of the conditions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maronus, 2018-05-28
@cryptodoxer

$schedule
            ->call(function () {
                if (Queue::size('source') == 0)
                    dispatch(new Source())->onQueue('source');
            })
            ->everyMinute();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question