Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Run queue:work on laravel schedule

I have a schedule like this:

<?php

namespace App\Console;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Console\Scheduling\Schedule;
protected function schedule(Schedule $schedule)
{
   Artisan::call('queue:work');
}

I add this on my cronjob:

* * * * * cd /var/www/html/my_script_address && php artisan schedule:run

Is it correct code? I am asking, because every minute run Artisan::call('queue:work').

Is it the best way?

like image 703
paranoid Avatar asked Aug 31 '25 00:08

paranoid


2 Answers

Your queue worker should be running on its own. You should use supervisor to make sure it stays running. However, if you must start the queue worker from the scheduler it is probably best to use queue:work --stop-when-empty

like image 141
Adam Martinez Avatar answered Sep 02 '25 23:09

Adam Martinez


I wanted to write a long answer with explanation.

1- Crontab is a scheduled job manager. It calls your command every time the scheduler ticks.

eg: If you create a crontab entry to write hello world once in a minute, it will do it every minute, and to the eternity.

2- Queue worker listens to the queue and it works if there is any job awaiting to be done. Very similar to cron jobs, but the execution time is not predefined. See it yourself:

/**
 * Listen to the given queue in a loop.
 *
 * @param  string  $connectionName
 * @param  string  $queue
 * @param  \Illuminate\Queue\WorkerOptions  $options
 * @return void
 */
public function daemon($connectionName, $queue, WorkerOptions $options)
{
    // [...]
    while (true) {
        ...
    }
}

As you see, she continues until something from the outside world stops her.

4- Your scheduler will create one more worker like this every minute.


Use something like supervisor to watch your workers. And create your worker instances thoughtfully. See supervisor configuration part of laravel documentation.

If you insist to use cron and queue workers together, use queue:work --once to let your worker know when to stop :)

like image 38
Hilmi Erdem KEREN Avatar answered Sep 02 '25 22:09

Hilmi Erdem KEREN