How to run queue worker on shared hosting

早过忘川 提交于 2021-02-19 02:38:05

问题


My Laravel application has a queued event listener and I have also set up the cronjob to run schedule:run every minute.

But I don't know how I can run the php artisan queue:worker command persistently in the background. I found this thread where it was the most voted approach:

$schedule->command('queue:work --daemon')->everyMinute()->withoutOverlapping();

However, on a different thread some people complained that the above-mentioned command creates multiple queue worker.

How can I safely run a queue worker?


回答1:


Since Laravel 5.7, there's a new queue command to stop working when empty:

php artisan queue:work --stop-when-empty

As this is mostly just for emails or few small jobs, I put it on a cronjob to run every minute. This isn't really a solution for more than 100 jobs per minute I'd say, but works for my emails. This will run about 5 seconds every minute just to send emails, depending on how many emails or how big the job.

Steps

  1. Create new command: php artisan make:command SendContactEmails
  2. In SendContactEmails.php, change: protected $signature = 'emails:work';
  3. In the handle() method, add:
return $this->call('queue:work', [
    '--queue' => 'emails', // remove this if queue is default
    '--stop-when-empty' => null,
]);
  1. Schedule your command every minute:
protected function schedule(Schedule $schedule)
{
    $schedule->command('emails:work')->everyMinute();
    // you can add ->withoutOverlapping(); if you think it won't finish in 1 minute
}
  1. Update your cronjobs:
* * * * * /usr/local/bin/php /home/username/project/artisan schedule:run > /dev/null 2>&1

Source

Processing All Queued Jobs & Then Exiting

The --stop-when-empty option may be used to instruct the worker to process all jobs and then exit gracefully. This option can be useful when working Laravel queues within a Docker container if you wish to shutdown the container after the queue is empty:

php artisan queue:work --stop-when-empty



回答2:


are you using cpanel?

you can set in the Scheduler or Cron Jobs menu. and set the command in there



来源:https://stackoverflow.com/questions/52273132/how-to-run-queue-worker-on-shared-hosting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!