如何解决带有计划的Laravel队列Laravel 7x
我正试图潜入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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。