Wednesday, March 15, 2017

Add queue in Laravel 5.4 using beanstalkd and keep it live with supervisor

Sometimes we need to add task that will take so much time. Like we want to convert a video to mp3 file. So this type of task we add to the queue. Then it converted on our server and after finishing the task we will show notification to the user. Today I will describe how to work on queue in laravel.

To add queue we can use different method like sync, database, beanstalkd, sqs, redis. For queue I like beanstalkd. It's made specially for queue. You can go your laravel project directory and open config/queue.php file. Here change this line -

'default' => env('QUEUE_DRIVER', 'beanstalkd')

Sometimes we declare queue driver on .env file. If you put this preference something like this please change it to 'beanstalkd'


we have to install benstalkd on your server. Please run this command to install on ubuntu server.

sudo apt-get install beanstalkd

Now we have to convert video to mp3 song. So we can add a job. Write command on your terminal -
php artisan make:job VideoToAudioSong

system will create VideoToAudioSong.php on apps/jobs/ directory. On this file you can see handle method. Generally you have to write you code here which will take so much time to process.

protected $video_id

public function __constructor($video_id){
 $this->video_id = $video_id

public function handle()
   $video_id = $this->video_id
   //write here video to audio convert code.
To start this task from the controller please write this code -

$this->dispatch(new VideoToAudioSong($video_id));
this dispatch method separate the process and send the task on the queue.  To run this task you have to run on terminal - 
php artisan queue:work
You can see "Job is processed successfully." But there should more queue task on your project. So we should run this command to listen latest queue at a time.
php artisan queue:listen
Suppose if we restart the server this command will no longer running. We have to monitor this and keep it live. So we can use supervisor. It's nice monitor tool. You can install supervisor using this command - 
sudo apt-get install supervisor

go to /etc/supervisor/conf.d directory and add laravel-worker.conf.

command=php /home/ubuntu/project_dir/artisan queue:listen --sleep=3 --tries=3
Then run these commands to active the laravel-worker
sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start laravel-worker:*

Hope now supervisor will monitor the queue process. If anyhow queue turn off, supervisor will turn it on. If you face any problem to understand this tutorial, don't hesitate comment here.

1 comment:

Note: Only a member of this blog may post a comment.