带有计划的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
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...