首页 > 解决方案 > 如何在 Laravel 上同步调度队列作业

问题描述

当我写这篇文章时,几乎就像标题所说的那样kernel.php

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

它异步运行代码,无论如何我可以dispatchNow()按计划执行吗?

我正在使用 Laravel 7

标签: phplaravelqueuejobsschedule

解决方案


您可以使用onConnection方法即时设置驱动程序。

$schedule->job((new Heartbeat)->onConnection('sync'))->everyFiveMinutes();

另一种选择可能是在调用该job方法时,设置connection.

/**
 * Add a new job callback event to the schedule.
 *
 * @param  object|string  $job
 * @param  string|null  $queue
 * @param  string|null  $connection
 * @return \Illuminate\Console\Scheduling\CallbackEvent
 */
public function job($job, $queue = null, $connection = null)
{
    return $this->call(function () use ($job, $queue, $connection) {
        $job = is_string($job) ? Container::getInstance()->make($job) : $job;

        if ($job instanceof ShouldQueue) {
            $this->dispatchToQueue($job, $queue ?? $job->queue, $connection ?? $job->connection);
        } else {
            $this->dispatchNow($job);
        }
    })->name(is_string($job) ? $job : get_class($job));
}

推荐阅读