Using the Job Queue API of the Zend Platform
Posted: April 25th, 2009 | Author: enrico | Filed under: Zend | Tags: job, PHP, platform, queue, zend | 6 Comments »One of the features of the Zend Platform is the Job Queue, a server application that is able to schedule and manage the execution of php scripts (jobs).
The Job Queue can be used to create asyncronous execution of php script and provide, for instance, the scalability of a server application (for more info read the White Paper “Scalability and Responsiveness with Zend Platform’s Job Queue” by Dotan Perry and Shie Erlich).
You can manage the Job Queue feature of the Zend Platform with a web interface (see the figure below) or directly in PHP with an API system.

Using the web interface you can schedule the execution of a job (a php script) that is located into the JobQueue directory of the Zend Platform (the default is /usr/local/Zend/Platform/JobQueue in a Linux environment). You can also suspend and resume jobs from the queue, view the statistic of the job queue, view the history of each job execution and setting the parameter of the queue (maximal queue depth, maximal re-queue times, queue alias, scripts folder, etc).
As I said you can use the job queue also from PHP with the use of a API system. Basically the Job Queue Zend API contains two main classes to manage the connection to a queue server and to schedule the execution of jobs. This classes are ZendAPI_Queue and ZendAPI_Job. Is very easy to use this API and you can schedule the execution of your php script in a few lines of code. Let me show this introducing some examples.
In order to connect to a queue server you have to specify the host:port address and the password of the queue server (the default port of the queue server is 10003). Here there is an example:
1 2 3 4 | $queue= new ZendAPI_Queue('192.168.1.10:10003'); if ((empty($queue)) || (!$queue->login('password'))) { throw new Exception('Error on queue connection'); } |
When you have established a connection you can manage the scheduling of a job. For instance here we scheduled the execution of a job named send_email.php for the next 4 hours.
1 2 3 | $job = new ZendApi_Job("send_email.php"); $job->setScheduledTime(time() + 4*3600); $id = $queue->addJob($job); |
If you want to search for a job into a queue server you can use the getJobsInQueue method of the ZendAPI_Queue class.
For instance you can remove the failed jobs from a queue server with the following code:
1 2 3 4 5 6 7 8 9 10 | $queue= new ZendAPI_Queue('192.168.1.10:10003'); if ((empty($queue)) || (!$queue->login('password'))) { throw new Exception('Error on queue connection'); } $jobs_status = array(status=>JOB_QUEUE_STATUS_LOGICALLY_FAILED); $all_failed_jobs = $queue->getJobsInQueue($jobs_status); foreach ($all_failed_jobs as $failed_job) { echo "removing job $failed_job from queue..."; $queue->removeJob($failed_job); } |
In this post i introduced only the main functions of the Job Queue Zend API, for the complete list you can read the User Guide of Zend Platform 3.6 at pag. 295-305.
In my opinion using the Job Queue feature of the Zend Platform you can explore a new way to think about web solutions with asyncronous execution of PHP code.







