PHP API reference - ZendHQ JobQueue
This topic lists the PHP API functions of the ZendHQ JobQueue.
On this page:
ZendHQ\JobQueue namespace
The Job class
The Job class is used to get information about jobs and queues.
Job methods
Methods |
Description |
---|---|
Returns the name of the queue |
|
Returns the ID value uniquely identifying the job |
|
Returns the job definition |
|
Returns the schedule of the job |
|
Returns job options |
|
Returns the status of the job |
|
Returns the retry count |
|
Returns the job output if the job finished and the output persisted |
|
Returns the time when the job was created |
|
Returns the next scheduled execution time |
|
Returns the last completion time if the job completed |
|
Refreshes the job by requesting updated information from the daemon |
Job constants
Constant |
Type |
Description |
---|---|---|
STATUS_CREATED |
int |
The job is created, but not scheduled yet |
STATUS_SCHEDULED |
int |
The job is scheduled |
STATUS_WAITING_ON_PARENT |
int |
The job is waiting for the parent to be completed |
STATUS_RUNNING |
int |
The job is running |
STATUS_SUSPENDED |
int |
The job is suspended |
STATUS_TIMEOUT |
int |
The job timed out |
STATUS_FAILED_NO_WORKER |
int |
The job failed because there is no worker |
STATUS_FAILED_WORKER_ERROR |
int |
The job failed because of a worker error |
STATUS_REMOVED |
int |
The job is canceled and deleted |
STATUS_COMPLETED |
int |
The job completed successfully |
STATUS_UNKNOWN |
int |
The job status is unknown due to an interruption |
getQueueName
Return value: string - queue name
getId
Return value: int - the ID value
getJobDefinition
Return value: JobDefinition - the job definition
getSchedule
Return value: Schedule - the schedule
getJobOptions
Return value: JobOptions - job options
getStatus
Return value: int - job status value
getRetryCount
Return value: int - retry count
getOutput
Return value: string|null - output
getCreationTime
Return value: DateTimeInterface - creation time
getScheduledTime
Return value: DateTimeInterface - scheduled execution time
getCompletionTime
Return value: DateTimeInterface|null - completion time if completed, otherwise null
refresh
Return value: void
@throws NetworkError if:
-
There is no connection to the ZendHQ daemon
-
There was a failure to communicate with the ZendHQ daemon
-
If errors occurred when parsing the message
@throws InvalidArgument if:
-
There is no such job.
The CLIJob class
Implements JobDefinition
CLIJob methods
Methods |
Description |
---|---|
Constructs CLI job definition |
|
Returns the optional name of the job |
|
Sets or unsets the name of the job |
|
Adds environment variables to the CLI job |
|
Returns the environment |
|
Returns the full command line |
__construct
Parameter |
Type |
Required |
Description |
---|---|---|---|
command |
string |
yes |
The full command line, including arguments and options |
Throws InvalidArgument if:
-
the $command is not valid (quotes not closed etc.)
The value of the $command argument is processed using the following rules:
-
Backslash (\) characters followed by one of the following characters: backslash (\), space ( ), double quote ("), and single quote ('), are removed, and the following character preserves its literal value; backslashes preceding characters without special meaning are left unmodified.
-
Enclosing characters in single quotes (') preserves the literal value of each character within the quotes; a single quote (') may not occur between single quotes.
-
Enclosing characters in double quotes (") preserves the literal value of each character within the quotes, with the exception of backslash (\); a double quote (") can be quoted within double quotes by preceding it with a backslash.
getName
Return value: string - optional name of the job if set, otherwise null.
setName
Parameter |
Type |
Required |
Description |
---|---|---|---|
name |
string |
yes |
The job name |
Unsets the name if $name is null or an empty string.
Return value: CLIJob - self
setEnv
Parameter |
Type |
Required |
Description |
---|---|---|---|
name |
string |
yes |
Name of the environment variable |
value |
string |
yes |
Value of the environment variable |
Note that the default environment for CLI jobs is empty and the PATH must be specified manually if needed.
Return value: CLIJob - self
getEnv
Return value: array<string, string> - an array of name/value pairs
getCommand
Return value: string - the full command line
The HTTPJob class
Implements JobDefinition.
HTTPJob methods
Methods |
Description |
---|---|
Constructs an HTTP job definition |
|
Returns the optional name of the job |
|
Sets or unsets the name of the job |
|
Sets the URL of the HTTP request |
|
Sets the HTTP request method |
|
Sets the HTTP body content type |
|
Adds request headers to the HTTP job definition |
|
Adds query string arguments to the HTTP job definition |
|
Adds body parameters to the HTTP job definition |
|
Sets the raw body for the HTTP job definition |
|
Returns the URL of the HTTP request |
|
Returns the HTTP request method |
|
Returns the HTTP body content type |
|
Returns all the HTTP request headers |
|
Returns the encoded HTTP request body |
HTTPJob constants
Constant |
Type |
Description |
---|---|---|
HTTP_METHOD_GET |
string |
HTTP GET request |
HTTP_METHOD_POST |
string |
HTTP POST request |
HTTP_METHOD_PUT |
string |
HTTP PUT request |
CONTENT_TYPE_JSON | int | HTTP content type application/json |
CONTENT_TYPE_URL_ENCODED | int | HTTP content type application/x-www-form-urlencoded |
CONTENT_TYPE_ZEND_SERVER | int | HTTP content type compatible with Zend Server |
__construct
Parameter |
Type |
Required |
Description |
---|---|---|---|
url |
string |
yes |
The URL of the HTTP request |
method |
string |
yes |
The HTTP request method |
contentType |
int |
yes |
The HTTP body content type |
Throws InvalidArgument if:
-
$url is not a valid URL
-
$method is not one of GET, POST, or PUT
-
$contentType is not a valid value
See the HTTPJob Constants table for method and contentType values.
getName
Return value: string - optional name of the job if set, otherwise null
setName
Parameter |
Type |
Required |
Description |
---|---|---|---|
name |
string |
yes |
The Job Name |
Unsets the name if $name is null or an empty string.
Return value: HTTPJob - self
setURL
Parameter |
Type |
Required |
Description |
---|---|---|---|
url |
string |
yes |
The URL of the HTTP request |
Throws InvalidArgument if:
-
$url is not a valid URL
Return value: HTTPJob - self
setMethod
Parameter |
Type |
Required |
Description |
---|---|---|---|
method |
string |
yes |
The HTTP request method |
Throws InvalidArgument if:
-
$method is not one of GET, POST, or PUT
Return value: HTTPJob - self
setContentType
Parameter |
Type |
Required |
Description |
---|---|---|---|
contentType |
int |
yes |
The HTTP body content type |
Throws InvalidArgument if:
-
$contentType is not a valid value
Return value: HTTPJob - self
addHeader
Parameter |
Type |
Required |
Description |
---|---|---|---|
headerName |
string |
yes |
Name of the header |
value |
string |
yes |
Request header value |
Return value: HTTPJob - self
addQueryStringArgument
Parameter |
Type |
Required |
Description |
---|---|---|---|
key |
string |
yes |
The key |
value |
string |
yes |
The value |
Return value: HTTPJob - self
addBodyParam
Parameter |
Type |
Required |
Description |
---|---|---|---|
key |
string |
yes |
The key |
value |
string |
yes |
The value |
Throws InvalidMethod if:
-
method is GET
-
addRawBody() is already called
Return value: HTTPJob - self
setRawBody
Parameter |
Type |
Required |
Description |
---|---|---|---|
content |
string |
yes |
The raw body content |
Throws InvalidMethod if:
-
method is GET
-
addBodyParam() is already called
Return value: HTTPJob - self
getUrl
Return value: string - the URL
getMethod
Return value: string - the HTTP request method
getContentType
Return value: int - the HTTP body content type
getHeaders
Return value: array<string, string> - an array of name/value pairs
getBody
You first queue the job and then check the body.
Throws InvalidMethod if:
-
job is not queued yet and the body is provided
Return value: string|null - The encoded HTTP request body. Returns null if the HTTP request is not a POST or PUT
The JobDefinition interface
JobDefinition methods
Methods |
Description |
---|---|
Returns the optional name of the job |
|
Sets or unsets the name of the job |
getName
Return value: string - optional name of the job if set, otherwise null
setName
Parameter |
Type |
Required |
Description |
---|---|---|---|
name |
string |
no |
Name of the job or null |
Unsets the name if $name is null or an empty string
Return value: JobDefinition - self
The JobOptions class
JobOptions methods
Methods |
Description |
---|---|
Constructs job options |
|
Returns the priority of the job |
|
Returns the timeout value in seconds |
|
Returns the number of allowed retries |
|
Returns the wait time in seconds between retries |
|
Returns the persist job output option value |
|
Returns true if SSL validation is enabled |
|
Sets the job priority |
|
Sets the timeout value in seconds |
|
Sets the number of allowed retries |
|
Sets the wait time in seconds between retries |
|
Sets the persist job output option |
|
Enables or disables SSL validation for HTTP jobs |
JobOptions constants
Constant |
Type |
Description |
---|---|---|
PRIORITY_LOW |
int |
Low priority |
PRIORITY_NORMAL |
int |
Normal priority |
PRIORITY_HIGH |
int |
High priority |
PRIORITY_URGENT |
int |
Urgent priority |
PERSIST_OUTPUT_NO |
int |
Do not persist job output |
PERSIST_OUTPUT_YES |
int |
Always persist job output |
PERSIST_OUTPUT_ERROR |
int |
Persist job output when failed |
__construct
Parameter |
Type |
Required |
Description |
---|---|---|---|
priority |
int|null |
yes |
Priority of the job |
timeout |
int|null |
yes |
Job timeout in seconds |
allowedRetries |
int|null |
yes |
Number of allowed retries |
retryWaitTime |
int|null |
yes |
Wait time in seconds between retries |
persistOutput |
int|null |
yes |
Persist job output option |
validateSsl |
bool|null |
yes |
Validate SSL option for HTTP jobs |
Throws InvalidArgument if:
-
$priority value is not valid
-
$timeout is <= 0 or $timeout > 2147483
-
$allowedRetries < 0
-
$retryWaitTime < 0 or $retryWaitTime > 2147483
-
$persistOutput value is not valid
Default job options from the queue are used for any job option values that are null.
getPriority
Return value: int|null - priority of the job
getTimeout
Return value: int|null - timeout value in seconds
getAllowedRetries
Return value: int|null - number of allowed retries
getRetryWaitTime
Return value: int|null - wait time in seconds
getPersistOutput
Return value: int|null - persist job output value
willValidateSsl
Return value: bool|null - true if SSL validation is enabled
setPriority
Parameter |
Type |
Required |
Description |
---|---|---|---|
priority |
int |
yes |
The priority value |
Throws InvalidArgument if:
-
the $priority value is invalid
Return value: JobOptions - self
setTimeout
Parameter |
Type |
Required |
Description |
---|---|---|---|
seconds |
int |
yes |
Timeout value |
Throws InvalidArgument if:
-
$seconds <= 0 or $seconds > 2147483
Return value: JobOptions - self
setAllowedRetries
Parameter |
Type |
Required |
Description |
---|---|---|---|
retries |
int |
yes |
Number of retries |
Throws InvalidArgument if:
-
$retries < 0
Return value: JobOptions - self
setRetryWaitTime
Parameter |
Type |
Required |
Description |
---|---|---|---|
seconds |
int |
yes |
Wait time |
Throws InvalidArgument if:
-
$seconds <= 0 or $seconds > 2147483
Return value: JobOptions - self
setPersistOutput
Parameter |
Type |
Required |
Description |
---|---|---|---|
persist |
int |
yes |
The persist job output value |
Throws InvalidArgument if:
-
the $persist value is invalid
Return value: JobOptions - self
validateSsl
Parameter |
Type |
Required |
Description |
---|---|---|---|
validate |
bool |
yes |
Validate SSL option |
Return value: JobOptions - self
The JobQueue class
JobQueue methods
Methods |
Description |
---|---|
__construct |
Constructs the JobQueue object and connects to the ZendHQ daemon |
getQueues |
Returns an array with all the JobQueue queues |
getDefaultQueue |
Returns the default JobQueue queue |
getQueue |
Returns the queue identified by $queueName |
hasQueue |
Returns true if a queue with the given name exists |
addQueue |
Adds a queue with the given name and definition |
modifyQueue |
Modifies the queue identified by the name |
deleteQueue |
Deletes the queue and all the jobs in the queue |
__construct
Parameter |
Type |
Required |
Description |
---|---|---|---|
endpoint |
string |
no |
0mq endpoint |
Throws InvalidArgument if:
-
the optional 0mq $endpoint is invalid
Throws NetworkError if:
-
failed to connect to the ZendHQ daemon
-
protocol mismatch (daemon and extension versions are different)
-
message parsing errors
Throws LicenseError if:
-
ZendHQ license is missing, invalid, or expired
If the $endpoint parameter is omitted, then the system connects to the default ZendHQ daemon specified in the INI file.
getQueues
Return value: array<int, string> - array of queue ID value and name pairs
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
getDefaultQueue
Return value: Queue - the default JobQueue queue
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
Throws ServerError if:
-
ZendHQ daemon did not respond with a valid queue
getQueue
Parameter |
Type |
Required |
Description |
---|---|---|---|
queueName |
string |
yes |
The name of the queue |
Return value: Queue - The queue
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
Throws InvalidArgument if:
-
no queue with the given $queueName
Throws ServerError if:
-
ZendHQ daemon did not respond with a valid queue
hasQueue
Parameter |
Type |
Required |
Description |
---|---|---|---|
queueName |
string |
yes |
The name of the queue |
Return value: bool - True if a queue with the given name exists, otherwise false
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
Throws ServerError if:
-
ZendHQ daemon did not respond with a valid queue
addQueue
Parameter |
Type |
Required |
Description |
---|---|---|---|
queueName |
string |
yes |
The name of the queue |
queueDefinition |
QueueDefinition |
no |
Optional queue definition |
Return value: Queue - The added queue instance
Throws InvalidArgument if:
-
queue name is empty or longer than 256 characters
-
a queue with the given name already exists
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
If the optional $queueDefinition argument is omitted, then the system uses default queue definition values configured in the ZendHQ daemon.
modifyQueue
Parameter |
Type |
Required |
Description |
---|---|---|---|
queue |
string|Queue |
yes |
The queue (queue name or queue instance) |
queueDefinition |
QueueDefinition |
yes |
Queue definition with changed attributes |
Return value: Queue - The modified queue instance
Throws InvalidArgument if:
-
the queue does not exist
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
deleteQueue
Parameter |
Type |
Required |
Description |
---|---|---|---|
queue |
string|Queue |
yes |
The queue to be deleted (queue name or instance) |
Throws InvalidArgument if:
-
the queue does not exist
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
Throws ServerError if:
-
the queue is not suspended
The queue MUST be suspended before it can be deleted.
The Queue class
Queue methods
Methods |
Description |
---|---|
getName |
Returns the name uniquely identifying the queue |
getId |
Returns the numeric ID value uniquely identifying the queue |
getDefinition |
Returns the queue definition |
getStatus |
Returns the current status of the queue |
refresh |
Refreshes the queue by requesting updated information from the daemon |
suspend |
Suspends the queue and optionally waits until the queue is suspended |
resume |
Resumes the queue and optionally waits until the queue is resumed |
getJobs |
Returns an array with all the scheduled and running jobs in this queue |
getJobsByName |
Returns an array with all the scheduled and running jobs in this queue that have the specified name |
getJob |
Returns a job identified by the $id value in this queue |
scheduleJob |
Schedules a job for execution in this queue |
cancelJob |
Cancels the job |
Queue constants
Constant |
Type |
Description |
---|---|---|
STATUS_RUNNING |
int |
Queue is running |
STATUS_SUSPENDED |
int |
Queue is suspended |
STATUS_DELETED |
int |
Queue is deleted |
getName
Return value: string - The name of the queue
getId
Return value: int - The ID value
getDefinition
Return value: QueueDefinition - The queue definition
getStatus
Return value: int - The queue status value
refresh
Return value: void
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
suspend
Parameter |
Type |
Required |
Description |
---|---|---|---|
timeout |
int |
no |
Time in seconds to wait |
Return value: void
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
Throws Timeout if:
-
the queue did not suspend within the specified time
If the $timeout argument is zero, then the system returns immediately after sending the request to the ZendHQ daemon without waiting.
resume
Parameter |
Type |
Required |
Description |
---|---|---|---|
timeout |
int |
no |
Time in seconds to wait |
Return value: void
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
Throws Timeout if:
-
the queue did not resume within the specified time
If the $timeout argument is zero, then the system returns immediately after sending the request to the ZendHQ daemon without waiting.
getJobs
Return value: array<int, Job> - Array of job instances
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
The returned array contains job instances in the "native" execution order
getJobsByName
Parameter |
Type |
Required |
Description |
---|---|---|---|
name |
string |
yes |
Job name |
Return value: array<int, Job> - Array of job instances
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
The returned array contains job instances in the "native" execution order
getJob
Parameter |
Type |
Required |
Description |
---|---|---|---|
id |
int |
yes |
The ID value of the job |
Return value: Job - The job instance
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
Throws InvalidArgument if:
-
no job with the given $id
Throws ServerError if:
-
ZendHQ daemon didn't respond with a valid job
scheduleJob
Parameter |
Type |
Required |
Description |
---|---|---|---|
job |
JobDefinition |
yes |
The definition of the job |
schedule |
Schedule |
no |
Execution schedule of the job |
jobOptions |
JobOptions |
no |
Options for this job |
Return value: Job - The scheduled job
Throws NotInitialized if:
-
the Queue object is not associated with any JobQueue objects
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
Throws ServerError if:
-
ZendHQ daemon didn't respond with a valid job
Schedules a job for execution in this queue and returns the Job instance updated by the ZendHQ daemon.
-
If the $schedule argument is null, then the system schedules the job for immediate execution.
-
If the $jobOptions argument is null, then the system uses default job options from the queue.
cancelJob
Parameter |
Type |
Required |
Description |
---|---|---|---|
job |
int|Job |
yes |
The job to be canceled (ID value or Job instance) |
Return value: void
Throws NetworkError if:
-
not connected to ZendHQ daemon
-
failed to communicate with the ZendHQ daemon
-
message parsing errors
Throws InvalidArgument if:
-
the $job object is not valid
-
no such job
Cancels the previously scheduled job and marks it as deleted (Job::STATUS_REMOVED)
If the job was running and the test result of the job is not known, then the system marks it as unknown (Job::STATUS_UNKNOWN)
The QueueDefinition class
QueueDefinition methods
Methods |
Description |
---|---|
__construct |
Constructs the queue definition instance |
getPriority |
Returns the priority of the queue |
getDefaultJobOptions |
Returns default job options for jobs scheduled in this queue |
setPriority |
Sets the priority of the queue |
setDefaultJobOptions |
Sets default job options for jobs scheduled in this queue |
__construct
Parameter |
Type |
Required |
Description |
---|---|---|---|
priority |
int |
yes |
Queue priority value |
defaultJobOptions |
JobOptions |
yes |
Default options for jobs scheduled in this queue |
Throws InvalidArgument if:
-
the $priority value is invalid
JobQueue default values are used for any values that are null including null values in the $defaultJobOptions instance.
getPriority
Return value: int|null - queue priority value
getDefaultJobOption
Return value: JobOptions|null - default job options
setPriority
Parameter |
Type |
Required |
Description |
---|---|---|---|
priority |
int |
yes |
The queue priority value |
Throws InvalidArgument if:
-
the $priority value is invalid
Return value: QueueDefinition - self
setDefaultJobOptions
Parameter |
Type |
Required |
Description |
---|---|---|---|
defaultJobOptions |
JobOptions |
yes |
Default job options |
Return value: QueueDefinition - self
The RecurringSchedule class
The RecurringSchedule class implements the Schedule interface
RecurringSchedule methods
Methods |
Description |
---|---|
__construct |
Constructs the recurring schedule instance |
getSchedule |
Returns the schedule |
__construct
Parameter |
Type |
Required |
Description |
---|---|---|---|
crontab |
string |
yes |
Cron expression with the recurring schedule |
Throws InvalidArgument if:
-
the $crontab string is invalid
getSchedule
Return value: string - Schedule
The Schedule interface
Schedule methods
Methods |
Description |
---|---|
getSchedule |
Returns the schedule |
getSchedule
Return value: mixed
The ScheduledTime class
The RecurringSchedule class implements the Schedule interface
ScheduledTime methods
Method |
Description |
---|---|
__construct |
Constructs the scheduled time instance |
getSchedule |
Returns the schedule |
__construct
Parameter |
Type |
Required |
Description |
---|---|---|---|
dateTime |
DateTimeInterface |
yes |
Date and time |
Throws InvalidArgument if:
-
the $dateTime object is not initialized
getSchedule
Return value: DateTimeImmutable -
ZendHQ\JobQueue\Exception namespace
The InvalidArgument class
The InvalidArgument class extends the InvalidArgumentException class
Invalid argument value error
The InvalidMethod class
The InvalidMethod class extends the RuntimeException class
Unexpected method call error
The NotInitialized class
The NotInitialized class extends the RuntimeException class
JobQueue is not initialized
The NetworkError class
The NetworkError class extends the RuntimeException class
Network error when communicating with the ZendHQ daemon
The Timeout class
The Timeout class extends the RuntimeException class
Operation timed out
The ServerError class
The ServerError class extends the RuntimeException class
Unclassified server error
The LicenseError class
The LicenceError class extends the RuntimeException class
No valid ZendHQ license or the license is expired
The Other class
The Other class extends the RuntimeException class
Other unclassified errors