可以在Laravel中将延迟队列用作CRON作业的替代方法 [英] Can Delayed Queues in Laravel be used as an alternative to CRON jobs

查看:219
本文介绍了可以在Laravel中将延迟队列用作CRON作业的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel 4在队列方面有很多功能.此问题与Queue方法Queue.later() API有关文档第一个参数是delay.

Laravel 4 has a great list of features in terms of Queues. This question is with respect to the Queue method Queue.later() API Documentation the first parameter being the delay.

基本上利用Cron执行重复任务.

A Cron is basically made use of to execute recurring tasks.

如果下面的代码片段也可以在可配置的时间变得更加通用,则可以:

If the below snippet were to be made more generic with the time being configurable as well can:

  1. 这可以替代CRON作业吗?
  2. 假设我们使用 IronMQ
  3. ,这将是使用故障保护方法吗?
  1. This be used as an alternative to CRON jobs?
  2. Would this be fail safe approach to use assuming we use IronMQ

-

class SendEmail {

    public function fire($job, $data)
    {
    //Connect to SMTP and send email

    $job->delete();

    //Recall the queue with a delay
        Queue::later(60,'SendEmail@send', array('message' => $message));
    }
}


//app/events/MailHandler.php
public class MailHandler(){
    public function onMailListenerStarted(){
        Queue::push('SendEmail@send', array('message' => $message));    
    }
}

推荐答案

您必须记住,排队和Cron任务处理是两个不同的东西.

You have to keep in mind that Queuing and Cron-tasking are 2 different things.

  • cron作业每隔一分钟就会开始(取决于您的配置方式).
  • 在延迟时间结束后轮到队列工作,该轮到他处理了.

因此,为了将其与您对Cron执行重复执行任务"的定义进行比较,队列就不会那样做.乔布斯只会在队列中等待,他们什么也不会做.延迟的工作将为您带来的好处是,它至少要等到有时间才能发送出去,但它不会尝试一次性发送所有电子邮件.如果这样做的缺点是可能要花很长时间才能发送,但是要防止这种情况,您可以简单地使用更多的工作进程来处理队列.

So to compare this to your definition of a Cron "execute recurring tasks", the Queue does nothing like that. Jobs will simply wait in the queue, and they don't do anything. Delayed jobs will give you the advantage that it will at least wait till the time is there to send it, but it won't try and send all emails in one go. The downside if this is that it might take to long before it's send, but to prevent that, you can simply use more workers to process the queue.

您需要一个处理队列的脚本,您很可能希望以cron开头.

And you need a script that processes the queue, which you'll most likely want to start with a cron.

我在代码片段中看到的另一个问题是,如果将作业添加回队列时出了问题,该作业将丢失,并且永远不会添加回队列.

Another problem i see with the approach in the code snippet, is that, if something goes wrong with adding the job back to the queue, the job will be lost, and will never be added back to the queue.

所以回答您的问题:

  1. 否,队列不能替代作业,但是队列确实使cron脚本中的数据处理更加容易

  1. No, Queues are not an alternative to jobs, but Queues do make data processing in cron-scripts easier

从理论上讲,无论您的队列提供者多么出色,这都不是故障安全的方法.但是可以创建一些脚本来检查队列是否仍在执行应做的一切,但这确实需要一些日志记录(例如,在上次运行作业时保存).

In theory this won't be fail safe approach, no matter how good your queue provider is. But it's possible to create some scripts that check if the queue is still doing everything it should, but this does require some logging (e.g. Save when the job has been run for the last time).

这篇关于可以在Laravel中将延迟队列用作CRON作业的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆