部署 Laravel "schedule:run" 对比 "queue:work"

Deploying Laravel "schedule:run" vs "queue:work"

当我们有一个工作实例时,我们可以运行这个命令:

php artisan queue:work --queue=default --daemon --tries=5

对吗?然后,如果我们已经有运行 前面的命令,是否需要将schedule:run 添加到cron 作业中?

php artisan schedule:run >> /dev/null 2>&1

这两个命令都是必需的?还是只有一个?

这两个命令完全不同,做两件完全不同的事情。
artisan queue:work 为您的异步作业和事件侦听器启动队列工作程序,以便在调度时在后台 运行。
artisan schedule:run 在指定时间执行您的 Console\Kernel schedule 函数中定义的命令(这就是 CRON 的意思)

例子

1- queue:work

得到这样的工作 Class

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class LogStuff implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        info('stuff');
    }
}

以及像这样调度作业的路由
routes/web.php

use App\Jobs\LogStuff;

Route::get('/', function () {
    dispatch(new LogStuff());
    return view('welcome');
});

QUEUE_CONNECTION.env这样

QUEUE_CONNECTION=redis

Nothing is logged until the php artisan queue:work command is ran

作业被处理了两次,因为没有队列工作者的第一次调度 运行ning 只是将它添加到队列中(例如在 redis 中)

2- 预定命令

给定一个在 App\Console\Kernel 中定义的预定命令,就像这样

protected function schedule(Schedule $schedule)
{
   $date = now()->format('Y-m-d');
   $logfileFullpath = storage_path("logs/laravel-{$date}.log");
   $schedule->command('inspire')
            ->everyMinute()
            ->sendOutputTo($logfileFullpath);
}

Nothing is outputted to the console until php artisan schedule:run is executed

1 分钟后

我希望这已经够清楚了