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?
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
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With