Beanstalk is a simple, fast work queue.Its interface is generic,but was originally designed for reducing the latency of page views in high-volume web applications by running time-consuming tasks asynchronously.chronously.
Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.
Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time. Deferring these time consuming tasks drastically speeds up web requests to your application.
In this article we learn how use laravel queue with beanstalkd and supervisor in ubuntu.For that first we need to configure laravel queue.The queue configuration file is stored on
config/queue.php directory.In this file there has a variable
default on here we set our queue driver as beanstalkd and on
connection array has the configuration of queue driver
'default' => 'beanstalkd',
Then download beanstalkd dependency package using composer like below
composer require pda/pheanstalk
And then run
composer update.And dependency package will download when complete.
After that our queue driver is set,package is downloaded now we create job for queue.To create job we can create job class and fire job for queue.For create job follow here https://laravel.com/docs/5.3/queues#creating-jobs To know how to work queue in laravel follow here https://laravel.com/docs/5.3/queues
In this article we didn’t discuss how to work queue in laravel.We know how to work beanstalkd,supervisor with laravel queue.Lets think we are ready to fire queue.So our next job is install and configure beanstalkd.
Install and configure beanstalkd
I am using ubuntu 16.04 LTS.Run below command to install beanstalkd
sudo apt-get install beanstalkd
We need to enable public access in order to access the beanstalkd queue from host PC.This is only for development, because we will be using beanstalk console on the host PC to watch the queue of Beanstalkd running in a virtual machine.Go to sudo nano
/etc/default/beanstalkd and change
BEANSTALKD_LISTEN_ADDR=127.0.0.1 to BEANSTALKD_LISTEN_ADDR=0.0.0.0
Don’t do this on production server.Now restart the beanstalkd service as below
sudo service beanstalkd restart
Beanstalkd installed.Now we install beanstalkd console to monitor beanstalkd admin panel that how many queue is now serving and how many already served.
Install beanstalkd console
Run below command
composer create-project ptrofimov/beanstalk_console -s dev
It will create a folder name
beanstalk_console.Go to this folder and then start php server on that folder public directory like below.
php -S localhost:7654 -t public
Then browse http://localhost:7654 in your browser.Will get an screen below
Then add remote server to check beanstalkd queues. Leave the port to default and change the server name. You can use the IP address or the server name where you have install beanstalkd.
Now beanstalkd is ready for queue.Now when we fire queue from our project we can see on beanstalkd console that this queue ready on here and total job is 1.That means our queue is working with beanstalkd.Now if we run php “artisan queue:work” our job will server and we got the result in beanstalkd console.
Now the next challenge is that we need to run
queue:work always and restart queue when queue fails.In that situation supervisor will help to do this types of staff.
Install and configure supervisor
sudo apt-get install supervisor
Run this command to install supervisor.After install supervisor we crate a log file for supervisor in project like
/var/www/html/project/storage/logs/supervisor.logThen we need to write configuration for supervisor.Supervisor configuration files are typically stored in the
/etc/supervisor/conf.d directory.Within this directory we need to create a configuration file.Let’s create a configuration file name is
laravel-queue.conf as below configuration
Then we need to start supervisor,using below command
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start laravel-queue:*
It’s already done.Now you can fire a queue and your queue will automatically server and you will monitor your queue from beanstalkd console.For further query or any question,please comment on here.