简体   繁体   English

延迟从代码排队的Laravel Artisan命令

[英]Delay a Laravel Artisan command queued from code

I am running an Artisan command from a controller in my Laravel app. 我在Laravel应用程序中从控制器运行Artisan命令。 As the docs specify , you can queue like this: 正如文档所指定的那样 ,您可以像这样排队:

Artisan::queue('email:send', [
    'user' => 1, '--queue' => 'default'
]);

This takes care the queue logic and, in my case, sends the job off to Redis where it's processed almost immediately. 这会照顾队列逻辑,在我的情况下,将作业发送到Redis,几乎立即处理它。

I want to delay the job. 我想推迟这份工作。 You can normally do this when calling a queue command like so: 通常可以在调用队列命令时执行此操作

$job = (new SendReminderEmail($user))->delay(60);

$this->dispatch($job);

Is there a way to join these functions so I can delay my Artisan command for 5 minutes? 有没有办法加入这些功能,所以我可以延迟我的Artisan命令5分钟? I assumed there's be a simple option to delay it. 我假设有一个简单的选择来延迟它。

If not, I could create another Job class to stand between my controller and Artisan command, which I could queue in the normal way and delay, then have that Job call my Artisan command. 如果没有,我可以创建另一个Job类来站在我的控制器和Artisan命令之间,我可以按正常方式排队并延迟,然后让Job调用我的Artisan命令。 But this seems like a really convoluted way to make it work. 但这似乎是让它运作起来的一种非常复杂的方式。 Is there a better way to delay a queued Artisan command? 是否有更好的方法来延迟排队的Artisan命令?

Thank you 谢谢

Since the console kernel uses "push" to queue a command, this is not possible for laravel 5.3 and earlier. 由于控制台内核使用“push”对命令进行排队,因此对于laravel 5.3及更早版本,这是不可能的。

However you could make a pull request to the framework to implement the "later" call on the kernel, which could just pass through to the queue´s "later" function. 但是,您可以向框架发出拉取请求,以在内核上实现“稍后”调用,该调用可以直接传递到队列的“稍后”函数。

Or just implement a job class, like you already stated. 或者只是实现一个工作类,就像你已经说过的那样。

But there is a better solution for your use case. 但是对于您的用例有一个更好的解决方案。 Just use the Mail facade: 只需使用邮件门面:

Mail::later(5, 'emails.welcome', $data, function ($message) {
    //
});

See https://laravel.com/docs/5.2/mail#queueing-mail for documentation. 有关文档,请参阅https://laravel.com/docs/5.2/mail#queueing-mail

you can solve the problem, using a scheduled task that is going to be watching for emails to send in the desired time. 您可以使用计划任务来解决问题,该计划任务将在所需时间内监视要发送的电子邮件。 you can also use a table to set the email variables like the subject, template, template vars, time to send the email, etc. 您还可以使用表格来设置电子邮件变量,如主题,模板,模板变量,发送电子邮件的时间等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM