11

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?

A. Dabak
  • 760
  • 1
  • 9
  • 23

2 Answers2

33

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

HosseyNJF
  • 491
  • 1
  • 6
  • 18
A. Dabak
  • 760
  • 1
  • 9
  • 23
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.

Farhat Aziz
  • 131
  • 1
  • 10