简体   繁体   中英

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?

If you just want to run schedule job, just write in `app/console/kernel.php``:

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        // Do something
        DB::table('recent_users')->delete();
    })->daily();
}

And Put below code to /etc/cron.d/laravel-project :

* * * * * cd /path/to/laravel-project-root && php artisan schedule:run >> /dev/null 2>&1

See: https://laravel.com/docs/5.7/scheduling#introduction


The Queue is provide to run asynchronous task, you can install supervisor to keep queue:work is always running.

See this doc and configure queue:work and supervisor , and use schedule task to dispatch your job to queue. Modify app/console/kernel.php :

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        // Do something
        App\MyScheduleJob::dispatch();
    })->daily();
}

Now, queue should always running, and schedule job can dispatch to queue everyday.

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 :)

as per documentation https://laravel.com/docs/5.8/scheduling#scheduling-queued-jobs

you can simply queue a job via:

$schedule->job(new Heartbeat)->everyFiveMinutes();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM