A Queue of Job is described using the JobQueue class, when you want to manage a queue (or add more than one job to it) you should instantiate a JobQueue object.
The JobQueue object enables several job control functions such as: add/remove/suspend/resume, and some manage/info queue functions like: getJobsInQueue, getHistory, getStatistics etc.
A job is described by a Job class, whenever you want to add/update a job you can handle the Job object using the Job methods.
After a job is in the queue, you can retrieve it to change remove or suspend the Job by using the job id (the job id is assigned when a Job is added to the queue or by querying the jobs in the queue)
To add one job to a queue, for simplicity of usage, you can create the Job object (with it's required attributes) and then add the job directly from the Job object (without instantiating the JobQueue object), using the Job::addJob() function.
To change a Job's attributes, first get it from the queue (JobQueue::getJob() function), change the attributes and then update the queue with the changed Job object (JobQueue::updateJob() functions).
Constants for Job statuses
|
define('JOB_QUEUE_STATUS_SUCCESS', 1); |
Job was processed and succeeded |
|
define('JOB_QUEUE_STATUS_WAITING', 2);
|
Job is waiting to be processed (was not scheduled) |
|
define('JOB_QUEUE_STATUS_SUSPENDED', 3); |
Job was suspended |
|
define('JOB_QUEUE_STATUS_SCHEDULED', 4); |
Job is scheduled and waiting in queue |
|
define('JOB_QUEUE_STATUS_WAITING_PREDECESSOR', 5); |
Job is waiting for it's predecessor to be completed |
|
define('JOB_QUEUE_STATUS_IN_PROCESS', 6); |
Job is in process in Queue |
|
define('JOB_QUEUE_STATUS_EXECUTION_FAILED', 7); |
Job execution failed in the ZendEnabler |
|
define('JOB_QUEUE_STATUS_LOGICALLY_FAILED', 8); |
Job was processed and failed logically either because of job_fail command or script parse or fatal error |
Constants for different priorities of jobs
define('JOB_QUEUE_PRIORITY_LOW', 0);
define('JOB_QUEUE_PRIORITY_NORMAL', 1);
define('JOB_QUEUE_PRIORITY_HIGH', 2);
define('JOB_QUEUE_PRIORITY_URGENT', 3);
Constants for saving global variable's bit mask
define('JOB_QUEUE_SAVE_POST', 1);
define('JOB_QUEUE_SAVE_GET', 2);
define('JOB_QUEUE_SAVE_COOKIE', 4);
define('JOB_QUEUE_SAVE_SESSION', 8);
define('JOB_QUEUE_SAVE_RAW_POST', 16);
define('JOB_QUEUE_SAVE_SERVER', 32);
define('JOB_QUEUE_SAVE_FILES', 64);
define('JOB_QUEUE_SAVE_ENV', 128);
set_job_failed
set_job_failed( $error_string );
Description: Causes a job to fail logically. It can be used to indicate an error in the script logic (e.g. database connection problem).
Parameters: @param string $error_string the error string to display
jobqueue_license_info
jobqueue_license_info();
Description:
returns an array containing following fields:
"license_ok" - whether license allows use of JobQueue
"expires" - license expiration date
class ZendAPI_Queue {
var $_jobqueue_url;
Queue Class Functions
zendapi_queue($queue_url) {}
Constructor for a job queue connection
@param string $jobqueue_url - Full address where the queue is, in the form host:port
@return zendapi_queue - object
login($password, $application_id=null) {}
Opens a connection to a job queue
@param string $password - for authentication, password must be specified to connect to a queue
@param int $application_id - optional, if set, all subsequent calls to job related methods will use this application id (unless explicitly specified otherwise). I.e. When adding new job, unless this job already set an application id, the job will be assigned the queue application id
@return bool - success
addJob(&$job) {}
Insert a new job to the queue, the Job is passed by reference because its new job ID and status will be set in the Job object
@param Job $job - the Job we want to insert to the queue (by ref.)
@return int - the inserted job id
getJob($job_id) {}
Returns a Job object describing a job in the queue
@param int $job_id - the job id
@return Job Object - describing a job in the queue
updateJob(&$job) {}
Updates an existing job in the queue with it's new properties. If job doesn't exist, a new job will be added. Job is passed by reference and it's updated from the queue.
@param Job $job - the Job object, the ID of the given job is the id of the job we try to update. If the given Job doesn't have an assigned ID, a new job will be added
@return int - the id of the updated job
suspendJob($job_id) {}
Removes a job from the queue
@param int|array $job_id - the job id or array of job ids we want to remove from the queue
function removeJob($job_id) {} - suspend a job in the queue (without removing it)
@param int|array $job_id - the job id or array of job ids we want to suspend
@return bool - success/failure
resumeJob($job_id) {}
Resume a suspended job in the queue
@param int|array $job_id - the job id or array of job ids we want to resume
@return bool - success/failure (if the job wasn't suspended, the function will return false)
requeueJob($job) {}
Re-queue failed job back to the queue.
@param job $job - job object to re-queue
@return bool - true or false.
getStatistics() {}
returns job statistics
@return array with the following:
"total_complete_jobs"
"total_incomplete_jobs"
"average_time_in_queue" [msec]
"average_waiting_time" [sec]
"added_jobs_in_window"
"activated_jobs_in_window"
"completed_jobs_in_window"
moving window size can be set through the ini file
isScriptExists($path) {}
Returns whether a script exists in the document root
@param string $path - relative script path
@return bool - TRUE if script exists in the document root FALSE otherwise
isSuspend() {}
Returns whether the queue is suspended
@return bool - TRUE if job is suspended FALSE otherwise
getJobsInQueue($filter_options=null, $max_jobs=-1, $with_globals_and_output=false) {}
Returns a list of jobs in the queue according to the options given in the filter_options parameter, doesn't return jobs in "final states" (failed, complete). If the application id is set for this queue, only jobs with this application id will be returned.
@param array $filter_options - array of optional filter options to filter the jobs we want to get from the queue. If not set, all jobs will be returned. Options can be: priority, application_id, name, status, recurring.
@param int max_jobs - maximum jobs to retrieve. Default is -1, getting all jobs available.
@param bool with_globals_and_output - whether to get the global variables data and job output. Default is false.
@return array - Jobs that satisfies filter_options.
getNumOfJobsInQueue($filter_options=null) {}
Returns a list of jobs in the queue according to the options given in the filter_options parameter
If application id is set for this queue, only jobs with this application id will be returned
@param array $filter_options - array of optional filter options to filter the jobs we want to get from the queue. If not set, all jobs will be returned. Options can be: priority, application_id, host, name, status, recurring.
@return int - number of jobs that satisfies filter_options.
getAllhosts() {}
Return all the hosts that jobs were submitted from @return array.
getAllApplicationIDs() {}
Return all the application ids exists in queue @return array.
getHistoricJobs($status, $start_time, $end_time, $index, $count, &$total) {}
Return finished jobs (either failed or successes) between time range allowing paging.
Jobs are sorted by job id descending.
@param int $status - filter to jobs by status, 1-success, 0-failed either logical or execution.
@param UNIX timestamp $start_time - get only jobs finished after $start_time.
@param UNIX timestamp $end_time - get only jobs finished before $end_time.
@param int $index - get jobs starting from the $index-th place.
@param int $count - get only $count jobs.
@param int $total - pass by reference. Return the total number of jobs satisfied the query criteria.
@return array of jobs.
suspendQueue() {}
Suspends queue operation
@return bool - TRUE if successful FALSE otherwise
resumeQueue() {}
Resumes queue operation
@return bool - TRUE if successful FALSE otherwise.
getLastError() {}
Returns a description of the last error that occurred in the queue object. After every method invoked an error string describing the error is stored in the queue object.
@return string.
setMaxHistoryTime() {}
Sets a new maximum time for keeping historic jobs.
@return bool - TRUE if successful FALSE otherwise
This class describes a job in a queue
In order to add/modify a job in the queue, a Job class must be created, retrieved and than saved in a queue or, a job can be added directly to a queue without creating an instant of a Queue object.
class ZendAPI_Job {
var $_id;
Description: Unique id of the Job in the job queue.
@var int
var $_script;
Description: Full path of the script that this job calls when it's processed.
@var string
var $_host;
Description: The host from where the job was submitted.
@var string
var $_name;
Description: A short string describing the job.
@var string
var $_output;
Description: The job output after executing.
@var string
var $_status = JOB_QUEUE_STATUS_WAITING;
Description: The status of the job, by default, the job status is waiting to being executed. The status is determined by the queue and can not be modified by the user.
@var int
var $_application_id = null;
Description: The application id of the job. If the application id is not set, this job may get an application id automatically from the queue (if the queue was assigned one). By default it is null (which indicates no application id is assigned).
@var string
var $_priority = JOB_QUEUE_PRIORITY_NORMAL;
Description: The priority of the job, options are the priority constants. By default the priority is set to normal (JOB_QUEUE_PRIORITY_NORMAL).
@var int
var $_user_variables = array();
Description: An array holding all the variables that the user wants the job's script to have when it's called.
The structure is variable_name => variable_value i.e. if the user_variables array is array('my_var' => 8), when the script is called, a global variable called $my_var will have the int value of 8. By default, there are no variables that we want to add to the job's script.
@var array
var $_global_variables = 0;
Description: A bit mask holding the global variables that the user wants the job's script to have when it's called.
Options are prefixed with "JOB_QUEUE_SAVE_" and may be: POST|GET|COOKIE|SESSION|RAW_POST|SERVER|FILES|ENV. By default there are no global variables we want to add to the job's script i.e. In order to save the current GET and COOKIE global variables, this property should be JOB_QUEUE_SAVE_GET|JOB_QUEUE_SAVE_COOKIE (or the integer 6). In that case (of GET and COOKIE), when the job is added, the current $_GET and $_COOKIE variables should be saved, and when the job's script is called, these global variables should be populated.
@var int
var $_predecessor = null;
Description: The job may have a dependency (another job that must be performed before this job). This property holds the id of the job that must be performed. If this variable is an array of integers, it means there are several jobs that must be performed before this job. By default there are no dependencies.
@var mixed (int|array)
var $_scheduled_time = 0;
Description: The time that this job should be performed, this variables is the UNIX timestamp. If set to 0, it means that the job should be performed now (or at least as soon as possible). By default there is no scheduled time, which means we want to perform the job as soon as possible.
@var int
var $_interval = 0;
Description: The job running frequency in seconds. The job should run every _internal seconds. This property only applies to recurrent jobs. By default, its value is 0 e.g. run it only once.
@var int
var $_end_time = null;
Description: A UNIX timestamp of the last time this job should occur. If _interval was set, and _end_time was not, then this job will run forever. By default there is no end_time, so recurrent jobs run forever. If the job is not recurrent (Occurs only once) then the job will run at most once. If the end_time has reached and the job was not yet executed, it will not run.
@var int
var $_preserved = 0;
Description: A bit that determines if the job can be deleted from history. When set, removeJob will not delete the job from history.
@var int
function ZendAPI_Job($script) {}
Description: Instantiates a Job object, describing all the information and properties of a job.
@param script $script relative path (relative to document root supplied in the ini file) of the script this job should call when it's executing.
@return Job
function addJobToQueue($jobqueue_url, $password) {}
Description: Adds the job to the specified queue (without instantiating a JobQueue object). This function should be used only when adding a single job, to insert more than one job and/or manipulate other jobs (or job tasks) create and use the Job Queue object. This function creates a new Job Queue and logs in to it (with the given parameters), adds this job and logs out
@param string $jobqueue_url - the full address of the queue we want to connect to.
@param string $password - for authenticating, the queue password.
@return int - the added job id or false on failure.
function setJobPriority($priority) {}
Description: Set a new priority to the job.
@param int $priority, priority options are constants with the "JOB_QUEUE_PRIORITY_" prefix
All properties SET functions
function setJobName($name) {}
function setScript($script) {}
function setApplicationID($app_id) {}
function setUserVariables($vars) {}
function setGlobalVariables($vars) {}
function setJobDependency($job_id) {}
function setScheduledTime($timestamp) {}
function setRecurrenceData($interval, $end_time=null) {}
function setPreserved($preserved)
function getProperties() {}
Description: Get the job properties.
@return array The same format of job options array as in the Job constructor.
function getOutput() {}
Description: Get the job output.
@return An HTML representing the job output.
All properties GET functions
function getID() {}
function getHost() {}
function getScript() {}
function getJobPriority() {}
function getJobName() {}
function getApplicationID() {}
function getUserVariables() {}
function getGlobalVariables() {}
function getJobDependency() {}
function getScheduledTime() {}
function getInterval() {}
function getEndTime() {}
function getPreserved() {}
function getJobStatus() {}
Description: Get the job’s current status. If the job was created and not returned from a queue (using the JobQueue::GetJob() function), the function will return false. The status is one of the constants with the "JOB_QUEUE_STATUS_" prefix.E.g. the job was performed and failed, job is waiting etc.
@return int
function getTimeToNextRepeat() {}
Description: Get how many seconds until the next time the job will run. If the job is not recurrence or it past its end time, then return false@return int
function getLastPerformedStatus() {}
Description: For recurring jobs get the status of the last execution. For simple jobs, getLastPerformedStatus is equivalent to getJobStatus. Jobs that haven't been executed will return STATUS_WAITING.
@return int
|
|
|
|
|
Related Links: |
|
|
|