问题描述
我正试图潜入Laravel Queue。在我的示例中,我有一个(Daily〜)QuoteController,它带有一个select函数,该函数选择一个报价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");
}
在Job 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();
}
获得工作的最佳方法是什么? 谢谢!
解决方法
创建工匠命令是更好的解决方案。
在app \ Console \ Kernel中:
protected function schedule(Schedule $schedule)
{
$schedule->command('email:send')->everyFiveMinutes();
}
在您的工匠命令类中:
/**
* Execute the console command.
*
* @return mixed
*/
public function handle(ReportingEmailService $service)
{
$service->send(); // the send() method you mentioned in your question
}