In this Topic Hide
Working with The Job Queue API
One-Time Deferred Tasks (jobs)
Using the Job Queue from the Zend Server Administration Interface
The Zend Server Job Queue component provides the ability to off-load script execution using Jobs . A Job is in essence a command to execute a PHP script according to the specific parameters you give it. The Job Queue component manages the timing and execution of jobs in the system.
Jobs can be created to facilitate a wide range of situations that require you to asynchronously run PHP code. Here are a few examples:
The Job Queue API is a set of functions that allow you to create, define and manage the execution of PHP code using a job.
There are three types of Jobs you can create with the Job Queue API
This procedure describes how to create a Job that will be triggered as a result of end-user activity using the Job Queue API.
|
|
To create a Job that will be triggered as a result of end-user activity:
|
|
The next time the code is used the job will be triggered inside the code. To find out how the job ran go to Monitor | Jobs. To see what jobs are currently running go to Monitor | Queue Statistics. For additional information on handling jobs see Managing Jobs. |
Example: The following example shows what the original code looks like. How it will look like using the Job Queue API, and what the new Job file looks like. Before converting to a job:
Instead of calling the function from inside the script we will create a job that will be executed when we specify it to run. The script will be replaced with a call to the Job Queue API as follows: The application script after modification:
The new job script:
|
|
|
This procedure describes how to create a recurring Job. A recurring job will be executed periodically based on a defined schedule.
Creating recurring jobs from API is usually useful when the application’s workflow requires the creation (and usually subsequent deletion) of recurring tasks following some user interaction. For example, in a feed aggregator, it might make sense to create a recurring job that hourly pulls updates for each new feed added by the user. Another example might be a reporting system in which users can create and delete daily, weekly or monthly reports.
|
|
|
To create a recurring job:
|
The schedule option is a CRON-like expression that consists of string with 5 fields separated by spaces. The fields define the minute, hour, day of month, month and day of week (in this order) in which the job will run. |
In each field, you can use one of the following notations:
|
Example: The following example represents a job that will run every day at 3:15am: $job = $jq->createHttpJob('http://localhost/jobs/feed/405', null, array( 'schedule' => '15 3 * * *' |
|
Ranges for each field:
Field |
Range |
Comments |
Minute |
0 - 59 |
|
Hour |
0 - 23 |
In 24 hour format, where 0 is midnight |
Day of Month |
1 - 31 |
29,30 and 31 will only work for months of that length |
Month |
1 - 12 |
|
Day of Week |
0 - 7 |
Sunday is either 0 or 7 |
This procedure describes how to create a one-time deferred task. In some use cases, it makes sense to defer a certain task to a later time. For example, many applications have clear peak hours (e.g. between 8am and 11pm, or like in many Intranet applications, during office work hours).
If these applications need to perform some off-line processing, it might make sense to defer some of the heavy processing tasks to off hours (in our examples to late night or early morning hours), in order to maximize the efficiency of hardware utilization.
|
|
|
To create a time deferred task:
|
Note The format used in date() to pass the execution time – this is an SQL-like ‘YYYY-MM-DD hh:mm:ss’format (e.g. “2009-06-25 23:45:00” for June 25th 2009 at 45 minutes past 11pm). Zend Job Queue is not designed to execute jobs exactly on the specified time. For example, if the queue is limited to execute 10 jobs concurrently (more on that later on), and 1,000 jobs are scheduled for the exact same time – jobs will have to wait until other jobs finish. You should consider the schedule_time option as a request not to run a job before this time. |
|
Example: The following example shows a time-deferred task that has been scheduled to run a process at 2:00am. $options = array( 'schedule_time' => date('Y-m-d h:i:s', strtotime('tomorrow 2am')) ); $jq->createHttpJob('http://localhost/jobs/formproc.php', $_POST, $options); |
Note: the format used in date() to pass the execution time – this is an SQL-like ‘YYYY-MM-DD hh:mm:ss’ format (e.g. "2009-.6-25 23:45:00" for June 25th 2009 at 45 minutes past 11pm). |
The Zend Server administration interface provides tools for creating recurring jobs. These jobs have a set schedule and will be run at specific times.
This procedure describes how to create a recurring job.
|
|
To create a recurring job:
|
|
As soon as the new Recurring Job is saved, it will be put in the Job Queue at the time specified in the Rule. To find out how the job ran go to Monitor | Jobs. To see what jobs are currently running go to Monitor | Queue Statistics. For additional information on handling jobs see Managing Jobs. Note: The Jobs and Queues Statistics pages display information about all the Jobs in your system, including Jobs triggered by the Job Queue API. |
|
|
|
Related Links: Job DetailsManaging Jobs Filtering Jobs Job Queue Component |
|
|