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
.
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:
- This be used as an alternative to CRON jobs?
- 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));
}
}
You have to keep in mind that Queuing and Cron-tasking are 2 different things.
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.
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.
So to answer your questions:
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).
..2 years later..
Would this be fail safe approach? Laravel Forge has made adding and supervising queue workers very reliable and much less painful, it's worth checking out.
So yeah don't know if fail-safe or not, but definitely more reliable than it used to be.