I’ve recently spent some time toying with Laravel and using Beanstalkd to run a queue system, in this mini tutorial I’m going to show you how to get started implementing your own queues.

This tutorial assumes you have a copy of Laravel downloaded and ready to go and that you have a copy of beanstalkd and supervisor installed! I will write up a tutorial later on how to set these up, but for now we’re focusing on getting the queues up and running.

Lets start by creating a route for the facility to add a job to your queue. Open the app/routes.php file and add the following:

Route::get('queue/add', '[email protected]');

Now we need to create our QueueController file. Inside the controllers directory create the file QueueController.php, which should look something like this:

class QueueController extends BaseController{ }

We now need to create the “add” function we referenced in the route above. Inside the QueueController class create a public function of add:

public function add(){ }

We need to create a second file that handles the actual function of the queue. These files can be created anywhere where they will be automatically called by Laravel, for example the models directory. Create a file inside the models directory called MyQueue.php. Inside the MyQueue file, create a very basic class of MyQueue:

class MyQueue{ }

Inside the MyQueue class we need to create a function called fire, which is the default class method used by queues… Think of it as a __construct for queues.

public function fire($job, $data){ }

We are now ready to start passing work to the queue, back to the Queue Controller we’re going to call a push method on the queue:

Queue::push('MyQueue', array());

We are simply calling the MyQueue class here, as explained previously this will run the fire() function. The second parameter is used for providing data to the fire function, for example a message. Back to the fire function, in here we’re going to basically run a database query for demonstration purposes.

In your Laravel database, create a table called queue with 2 columns:

  • id int(11) PK AI
  • timestamp DATETIME

Now inside the fire function we need to query the database, and then remove the job from the queue once complete.

public function fire($job, $data){
    $query = DB::insert('INSERT INTO queue (timestamp) VALUES(NOW())');
    $job->delete();
}

The above code should make plenty of sense, we insert in to the table with a current timestamp and then remove the job from the queue.

Great, we should now have all of our code in place. Just to recap, this is how your files should look:

QueueController.php

class QueueController extends BaseController
{
    public function add()
    {
        Queue::push('MyQueue', array());
    }
}

MyQueue.php

class MyQueue
{
    public function fire($job, $data)
    {
        $query = DB::insert('INSERT INTO queue (timestamp) VALUES(NOW())');
        $job->delete();
    }
}

routes.php

Route::get('queue/add', '[email protected]');

So in the present state, you would need to run beanstalkd manually and run the queue listener from Laravel, this is done like so:

$ beanstalkd
$ php artisan queue:listen

At this point you would be able to visit http://yourapp.com/queue/add – and if you followed the tutorial correctly, a timestamp should appear in your table!

Now I’m going to run you through the basic setup for Supervisor. Supervisor is a process control system, it runs specific daemons/processes for you and ensures that if they fall over – it will kick them back up again. It’s a nifty little tool in the sysadmin arsenal.

Create a config file on your server in /etc/ called supervisord.conf

In this file we need to tell it to run both of our processes, both beanstalkd and the artisan queue listener. If you haven’t already, make sure as a bear minimum you have the following in this file:

[supervisord]
[supervisorctl]

These are where you would specify any custom configuration details for Supervisor, on first load it looks for those 2 pointers and fails if it can’t find them.

Now on to our queue listener. Set your config up to look something like this:

[program:myqueue]
command=php artisan queue:listen
directory=/path/to/laravel
stdout_logfile=/path/to/laravel/app/storage/logs/supervisor_queue.log
[program:beanstalkd]
command=beanstalkd
stdout_logfile=/path/to/beanstalk_log_file

Both of these entries should look fairly standard, command is the command we need to run. The directory is the path to run the command from, in this instance we need to be in the laravel directory to get access to artisan. stdout_logfile is the location of the log file.

Once you have set your config file up, you need to run supervisor. To do so run the following:

$ supervisord

It really is that simple! If it struggles to pick up your config file, pass in a parameter to tell it where it is.

$ supervisord -c /etc/supervisord.conf

And that covers it! I will write a follow up to this tutorial eventually where I’ll cover the installation of beanstalkd & Supervisor. Any questions/problems, as usual – drop them in the comments below!