php - 如何在 Laravel 上同步调度队列作业
问题描述
当我写这篇文章时,几乎就像标题所说的那样kernel.php
$schedule->job(new Heartbeat)->everyFiveMinutes();
它异步运行代码,无论如何我可以dispatchNow()
按计划执行吗?
我正在使用 Laravel 7
解决方案
您可以使用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));
}
推荐阅读
- javascript - I'm generating a list from an array how can I know what element I'm clicking?
- python - 使用带有 Python 的 Gmail API,搜索邮件返回 Requested entity was not found
- r - 在 R 版本 4.0.0 中导入 .txt 文件,其中数据包含在 {}
- javascript - css 文件中的问题 - 空 css 规则问题?
- powerbi - 无论如何在同一个报告中使用多个数据集?
- javascript - Gremlin-Javascript:支持 SubgraphStrategy 和 subgraph step
- java - Java Spring - 强制不继承bean
- javascript - DOM 元素未初始化
- json - 从此 JSON 创建数据库表
- function - 在方案中显示和返回某些内容的程序?