链式Laravel 5.6的任务计划作业

时间:2019-01-04 12:38:06

标签: laravel-5 queue

我需要运行一系列的作业,这些作业在Laravel中按计划的时间间隔(每周一次)运行。withChain方法非常适用于此:

firstJob::withChain([
    new secondJob,
    new thirdJob
 ]);

尝试在Scheduler中运行链时:

$schedule->job(firstJob::withChain([
    new secondJob,
    new thirdJob
 ]))->weekly();

我收到以下错误:

 In BoundMethod.php line 135:

 Method Illuminate\Foundation\Bus\PendingDispatch::handle() does not exist  

我从cli中的Scheduler获得的输出是:

Running scheduled command: Illuminate\Foundation\Bus\PendingDispatch

所以我知道job方法实际上不是在调用作业,而是Dispatchable特性中的dispatch()方法。

我的问题是如何在Laravel Task Scheduler中运行链式作业?

3 个答案:

答案 0 :(得分:0)

我通过用$ schedule-> call()替换$ schedule-> job()来解决此问题。简单的闭包运行job :: withChain()。我现在有一个监督命令,以确保queue:work artisan命令在后台运行,因此调度程序负责在分配的时间触发作业队列,从而触发队列。

答案 1 :(得分:0)

使用$schedule->call()会失去指定队列名称和其他作业特定参数的能力。

要保留此设置,您应该使用schedule job命令,该命令可以如下链接:

$schedule->job((new firstJob())->chain([
    new secondJob(),
    new thirdJob()
]), 'queue-name')->everyFiveMinutes();

答案 2 :(得分:0)

最新答案,但可能会帮助其他人:

$schedule->call(function () {
    firstJob::withChain([
        new secondJob,
        new thirdJob
    ])->dispatch()->allOnQueue('queue_name');
})->weekly();