Queues allow you to defer the execution of functions without blocking the script. They can be used to run all sorts of functions, from e-mailing a large number of users to generating PDF reports.
As of Version 4.1, Laravel is compatible with the following queue drivers:
Beanstalkd, with the
pda/pheanstalk
packageAmazon SQS, with the
aws/aws-sdk-php
packageIronMQ, with the
iron-io/iron_mq
package
Each queue system has its advantages. Beanstalkd can be installed on your own server; Amazon SQS might be more cost-effective and require less maintenance, as will IronMQ, which is also cloud-based. The latter also lets you set up push queues, which are great if you cannot run background jobs on your server.
A job is nothing more than a class with a fire
method that accepts two parameters for the name of the job and the data and that either deletes the job or releases it back to the queue:
class Job { public function fire($job, $data){ // Perform job… // If the job was successful, delete it $job-delete(); // Put it back onto the queue and try to execute it again $job->release($seconds); } }
Push a job onto the queue. When called, this will execute the fire
method:
Queue::push('Job', $data);
Push a job onto the queue but execute a different method:
Queue::push('Job@method', $data);
You can store your jobs anywhere in your application folder as long as the class is resolvable (see Chapter 7, Architecting Ambitious Applications). A potential location would be app/jobs
. When developing, it is also fine to push anonymous functions onto the queue:
Queue::push(function($job) use ($vars, $from, $outer, $scope){ // Perform job });
The following are the functions used for listening to a queue and executing jobs:
We can listen to the default queue:
$ php artisan queue:listen
We can specify the connection on which to listen:
$ php artisan queue:listen connection
We can specify multiple connections in the order of their priority:
$ php artisan queue:listen important,not-so-important
The queue:listen
command has to run in the background in order to process the jobs as they arrive from the queue. To make sure that it runs permanently, you have to use a process control system such as forever (https://github.com/nodejitsu/forever) or supervisor (http://supervisord.org/).
To get notified when a job fails, we use the following functions and commands:
The following event listener is used for finding the failed jobs:
Queue::failing(function($job, $data) { // Send email notification });
Any of the failed jobs can be stored in a database table and viewed with the following commands:
$ php artisan queue:failed-table // Create the table $ php artisan queue:failed // View the failed jobs
Push queues do not require a background process but they only work with the iron.io driver. After signing up for an account on iron.io
and adding your credentials to app/config/queue.php
, you use them by defining a POST
route that receives all the incoming jobs. This route calls Queue::marshal()
, which is the method responsible for firing the correct job handler:
Route::post('queue/receive', function() { return Queue::marshal(); });
This route then needs to be registered as a subscriber with the queue:subscribe
command:
$ php artisan queue:subscribe queue_name http://yourapp.example.com/queue/receive
Once the URL is subscribed on http://iron.io/, any newly created jobs with Queue::push()
will be sent from Iron back to your application via a POST
request.