如何在 Laravel 上同步安排 queue 作业
How do I schedule a queue job synchronously on Laravel
当我在 kernel.php
上写这篇文章时,标题的意思差不多
$schedule->job(new Heartbeat)->everyFiveMinutes();
它异步运行代码,我可以按计划执行 dispatchNow()
吗?
我正在使用 Laravel 7
您可以使用onConnection
方法即时设置驱动程序。
$schedule->job((new Heartbeat)->onConnection('sync'))->everyFiveMinutes();
Another option可能是在调用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));
}
当我在 kernel.php
$schedule->job(new Heartbeat)->everyFiveMinutes();
它异步运行代码,我可以按计划执行 dispatchNow()
吗?
我正在使用 Laravel 7
您可以使用onConnection
方法即时设置驱动程序。
$schedule->job((new Heartbeat)->onConnection('sync'))->everyFiveMinutes();
Another option可能是在调用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));
}