Laravel queue worker with cron
Asked Answered
D

3

12

I am trying to get my website to send confirmations emails every time someone new register.

i did it like following after reading about it, but i am still not convinced that this is the best way to do it.

in my cron runs every minute and calls php artisan schedule:run

in my console/Kernel

protected function schedule(Schedule $schedule)
{
    $schedule->command('queue:work --once')->everyMinute()->withoutOverlapping();
}

i added the --once parameter because the queue worker is not existing when finished, and i do not want to have many new processes running every minute.

is there a way to make the queue worker finish all the jobs and exit and then start it after one minute again so that i do not have many instances , or is it just one instance ??

i read that i can return null to exit the worker, but if this can be done, then how can i return null only after the last job is done?

Desireah answered 29/9, 2017 at 11:31 Comment(0)
D
35

for any one still looking for a solution, in laravel 5.7 they added support to run all jobs in the queue and then stop the queue worker when all jobs are done.

Your cronjob should run this: php /path/to/laravel/artisan queue:work --stop-when-empty

Queue worker command source code on Github

plus there is a package available for older versions of laravel

orobogenius/sansdaemon

Desireah answered 5/10, 2018 at 19:12 Comment(0)
D
2

For anyone looking for an alternative solution:

One approach for non-time-sensitive queue work (such as sending emails) is to add a task to the scheduler that starts the queue worker every minute;

Adding this line to the schedule method in app\Console\Kernel.php and then setting up a cron job to run the scheduler, ensures that the queue gets serviced every minute.

$schedule->command('queue:work --stop-when-empty')
             ->everyMinute()
             ->withoutOverlapping();

Then, create the scheduler cron job, like:

* * * * * /usr/local/bin/php /home/{account_name}/live/artisan schedule:run
Diphosgene answered 4/7 at 4:40 Comment(0)
C
0

After Laravel 5.7, queue:work run as daemon and once command has started, it will continue to run until it is manually stopped. You don't need to run a cron job to run it again and again after every minute. To keep the queue:work process running permanently in the background, you should use a process monitor such as Supervisor to ensure that the queue worker does not stop running. A queue:work process may stop running for a variety of reasons, such as an exceeded worker timeout or the execution of the queue:restart command. Supervisor can detect when your queue:work processes exit and automatically restart them.

Supervisor will sure worker queue always running in background. Below is link to supervisors

https://laravel.com/docs/10.x/queues#supervisor-configuration

Whenever a new user get registered on your website, a dispatched job will be added to queue. A singer work which is permanently active will execute that job. Using supervisor you can start multiple workers at a time if you have thousands of job every hour.

Correggio answered 10/8, 2023 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.