【发布时间】:2020-12-13 21:59:25
【问题描述】:
我正在尝试使用 Laravel Queue。在我的示例中,我有一个 (Daily~)QuoteController 和一个函数选择,它选择一个报价 rand:
public function choose()
{
$quote = Quote::all()->random(1)->first();
return $quote;
}
还有一个函数 send,发送一个 SendReportingEmail-Job
public function send() {
$content = QuoteController::choose();
//Log::info("Request Cycle with Queues Begins");
$this->dispatch( new SendReportingEmail($content) );
//Log::info("Request Cycle with Queues Ends");
}
在作业 SendReportingEmail 中,我使用构造函数来获取内容
class SendReportingEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $content;
public function __construct($content)
{
$this->content = $content;
}
public function handle(Mailer $mailer)
{
... send E-Mail
}
到目前为止,一切都很好 - 队列正在运行。现在我想每 5 分钟安排一次这项工作,所以我添加到 Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->job(new SendReportingEmail(???how to pass $content for Job???), 'reportingemail')->everyFiveMinutes();
}
获得工作的最佳方式是什么? 谢谢!
【问题讨论】:
-
欢迎来到SO ...你的工作应该是查询数据库以获取随机记录,然后不需要传递任何数据
-
谢谢,工作正常