Implement Task Scheduling using Job Queue in Zend Framework 2(PHP)
When talking about building a scalable and robust application there is a big concept out there that many PHP developers are not overly familiar with. That concept is queuing. We’ll Implement Task Scheduling using Job Queue in Zend Framework 2(PHP)
So, what is queuing? Many a time while developing any application we don’t want the application to perform the whole task at that time. Our priority at that time is to do only that part of the task which is most important and do rest after sometime and return response immediately which in turn reduces response time. e.g. Suppose you have an e-commerce site and someone buys any product. This task includes 3 sub tasks:
1 – Confirm payment status and save payment info in db.
2 – Generate invoice in pdf format.
3- Send email to the buyer.
So if your application is doing these 3 sub tasks synchronously then the buyer have to wait till all the process get completed, which is not a good UX. If we look at the tasks closely then we found that only first task of saving payment info is most important others can be done after some time.
Here comes the concept of queuing. In the language of queuing sub tasks 1,2, 3 are jobs. So in our case whenever there is any payment, the application will immediately do job 1 and add rest of the job viz 2 and 3 to the queue so that it can be done afterwards. The queue is processed by workers and pick jobs from queue on the basis of some algorithm.
In this tutorial I am going to use SlmQueueBeanstalkd which is a very good library for zf2. Here I am going to demonstrate how to integrate this library with your zf2 application step by step with code.
Step 1 :
First Install beanstalk with following commands
By default beanstalkd works on port 11300 , if your want to change it then it can be done by editing the following file :
Some of the beanstalkd commands which are useful
Step 2 :
Install SlmQueueBeanstalkd library in your zf2 application. Composer is needed to install the library, If hope everyone can install composer by their own. If anyone need help regarding installing composer then tell me in the comment section. Go to your application directory and run the following command to install library.
Step 3 :
In this step we will do the needful configurations. Lets say we have to send email using queue, so we nave our queue ’email’.
- Copy the vendor/slm/queue-beanstalkd/config/slm_queue_beanstalkd.local.php.dist file to your config/autoload folder and remove dist from file name. This file is for beanstalkd configurations if anything is changed in beanstalkd like port then it needs to be added here.
- Copy the vendor/slm/queue/config/slm_queue.global.php.dist file to your config/autoload file and remove dist from file name. This file is for adding queues. In this file add email in queues array.
- Add SlmQueue, SlmQueueBeanstalkd in modules array of config/application.config.php .
- just add the following line in your module.config.php file .
Step 4 :
All configurations are done, now in this step we create Job which will be added to the queue. I am doing everything in my Application module you can choose any module according to your need.
- Create a folder named Job inside src/Application .
- Create a file EmailJob.php inside Job folder. Add following code inside this file.
- Suppose we want to add this Email Job from our controller’s function.
- In this emal is sent after a delay of 60 seconds. Add a route to call this sendEmail action, suppose the route name is send_email.
Step 5 :
We have done all the coding part , now its time to run the application. Go to your application directory and run following command.
After that make a post call to the route created(send_email). You will see that it will return response immediately. Then go to terminal screen where you ran the above command. After 1 second you will see “mail sent” on the screen.
In your development environment you can start a worker manually and keep it running like we do in this step. For production environment you would use something like supervisor. Whatever command you gave to supervisor it makes sure that it should run all the time. It also give us a UI where we can see all the workers and logs . I’ll write a separate post on “supervisor” .
Feel free to comment in the comment box below if anyone face any error during implementation. I would love to help you folks.