Set up queues with Artisan

In Laravel 5 and 5.1, there is an enhanced queue system allowing you to use your database as your queue driver. There is no need to install something else such as Amazon SQS or Beanstalkd. Queues are really important. Let’s say for example that you need to send a confirmation email. If you don’t use the queue system available in Laravel and send an email when a user register, the email request can take up to 4-5 seconds which is too slow. Instead, with queues you can keep this task running in the background without any additional loading time delay.

Example :

Mail::queue('emails.welcome', $data, function($message)  
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
});

First of all, you need to setup the queue system. Go in yourlaravelpath/config/queue.php and select you prefered driver. In this tutorial I chose the database driver.

'default' => 'database',  

Then, you can change the default configuration like changing the table name but I decided to keep the default settings to keep it as simple as possible.

'database' => [  
            'driver' => 'database',
            'table' => 'jobs',
            'queue' => 'default',
            'expire' => 60,
        ],

In order to create the right database table (here “jobs”), there is a special artisan command for that :

php artisan queue:table  

Well done! Your queues are set up. However, you need to do one last thing to make them work as expected. Again, one simple artisan command :

php artisan queue:listen  

This command will always check for jobs and execute them immediately. It is obviously intended to always run in the background. So if you want to run it in the background on a Linux Server, you can use nohup for that.

sudo nohup php artisan queue:listen  

If you keep this command as it is, nohup will create a log file called nohup.log in the current directory. Instead if you want your log file to be placed elsewhere, you can do something like that :

nohup php artisan queue:listen > storage/logs/queues.log  2>&1 &  

Now you have a log file for your queues in the storage log directory of your laravel website called queues.log

Note : if you are not logged in as root this command will be killed as soon as you log out. Please log in as root with the command “su” before.

To see that you properly configured nohup, you can type “ps aux | grep php” and you will see three commands.

How to handle failed jobs

Now that you have set up your queues, what happens if there is a failed job ? For example, the smtp server for sending email does not respond. Well, the previous command will always try again and again to send your email without computing your other jobs. You do not want that to happen.

If you go back to your queues config file (config/queue.php) you will see at the bottom of the page the failed_jobs default table.

'failed' => [  
        'database' => 'mysql', 'table' => 'failed_jobs',
    ],

However, this table does not exist and you need to create it. Again there is an artisan command for that.

artisan queue:failed-table  

Now, with nohup you can set the number of tries that you want to allow before a job is considered as a failed job.

sudo nohup php artisan queue:listen --tries=2 > storage/logs/queues.log  2>&1 &  



You’re all set!

If you have any questions please let me know in the comments below.