【问题标题】:Laravel Queue with Schedule (Laravel 7x)Laravel 队列与时间表 (Laravel 7x)
【发布时间】: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 ...你的工作应该是查询数据库以获取随机记录,然后不需要传递任何数据
  • 谢谢,工作正常

标签: laravel queue schedule


【解决方案1】:

创建工匠命令是更好的解决方案。

在应用\控制台\内核中:

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多