Skip to content

Queue ​

Type
Class
Namespace
craft\queue
Inherits
craft\queue\Queue » yii\queue\cli\Queue » yii\queue\Queue » yii\base\Component » yii\base\BaseObject
Implements
craft\queue\QueueInterface, yii\base\BootstrapInterface, yii\base\Configurable
Since
3.0.0

Craft Queue

View source

Public Properties ​

PropertyDescription
attemptsinteger – Default attempt count
behaviorsyii\base\Behavior – List of behaviors attached to this component.
channelstring, null – The channel column value to the queue should use.
commandClassstring – Command class name
commandOptionsarray – Of additional options of command
dbcraft\db\Connection, array, string – The database connection to use
hasReservedJobsboolean
hasWaitingJobsboolean
jobInfoarray
loopConfigarray, string
mutexyii\mutex\Mutex, array, string – The mutex component to use
mutexTimeoutinteger – The time (in seconds) to wait for mutex locks to be released when attempting to reserve new jobs.
progressinteger – The job progress (1-100)
proxyQueueyii\queue\Queue, array, string, null – An external queue that proxy jobs should be sent to.
serializeryii\queue\serializers\SerializerInterface, array
strictJobTypeboolean – Whether to enable strict job type control.
tableNamestring – The table name the queue is stored in.
totalDelayedinteger
totalFailedinteger
totalJobsinteger
totalReservedinteger
totalWaitinginteger
ttrinteger – Default time to reserve a job
workerPidnull, integer

channel ​

Type
string, null
Default value
null
Since
3.4.0

The channel column value to the queue should use. If null, the queue’s application component ID will be used.

View source

commandClass ​

Type
string
Default value
\craft\queue\Command::class

Command class name

View source

db ​

Type
craft\db\Connection, array, string
Default value
'db'
Since
3.4.0

The database connection to use

View source

hasReservedJobs ​

Type
boolean
Default value
null
Access
Read-only

View source

hasWaitingJobs ​

Type
boolean
Default value
null
Access
Read-only

View source

jobInfo ​

Type
array
Default value
null
Access
Read-only

View source

mutex ​

Type
yii\mutex\Mutex, array, string
Default value
'mutex'
Since
3.4.0

The mutex component to use

View source

mutexTimeout ​

Type
integer
Default value
5

The time (in seconds) to wait for mutex locks to be released when attempting to reserve new jobs.

View source

progress ​

Type
integer
Default value
null
Access
Write-only

The job progress (1-100)

View source

proxyQueue ​

Type
yii\queue\Queue, array, string, null
Default value
null
Since
4.0.0

An external queue that proxy jobs should be sent to.

If this is set, push() will send craft\queue\jobs\Proxy jobs to it that reference the internal job IDs. When executed, those jobs will cause the referenced internal jobs to be executed, unless they’ve already been run directly.

View source

tableName ​

Type
string
Default value
\craft\db\Table::QUEUE
Since
3.4.0

The table name the queue is stored in.

View source

totalDelayed ​

Type
integer
Default value
null
Access
Read-only

View source

totalFailed ​

Type
integer
Default value
null
Access
Read-only

View source

totalJobs ​

Type
integer
Default value
null
Access
Read-only
Since
3.4.0

View source

totalReserved ​

Type
integer
Default value
null
Access
Read-only

View source

totalWaiting ​

Type
integer
Default value
null
Access
Read-only

View source

Public Methods ​

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of a component property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
bootstrap()Bootstrap method to be called during application bootstrap stage.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
delay()Sets delay for later execute.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
executeJob()Executes a single job.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getHasReservedJobs()Returns whether there are any reserved jobs.
getHasWaitingJobs()Returns whether there are any waiting jobs.
getJobDetails()Returns detailed info about a single job.
getJobInfo()Returns info about the jobs in the queue.
getTotalDelayed()Returns the total number of delayed jobs
getTotalFailed()Returns the total number of failed jobs
getTotalJobs()Returns the total number of jobs in the queue.
getTotalReserved()Returns the total number of reserved jobs
getTotalWaiting()Returns the total number of waiting jobs
getWorkerPid()Gets process ID of a worker.
handleError()
handleResponse()Figure out how to initiate a new worker.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined for this component.
init()Initializes the object.
isDone()
isFailed()
isReserved()
isWaiting()
listen()Listens to the queue and runs new jobs.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
priority()Sets job priority.
push()Pushes job into queue.
release()Releases a job from the queue.
releaseAll()Releases all jobs.
retry()Re-adds a failed job to the queue.
retryAll()Retries all failed jobs.
run()Runs all the queued-up jobs.
setProgress()Sets the progress for the currently reserved job.
status()
trigger()Triggers an event.
ttr()Sets TTR for job execute.
unserializeMessage()Unserializes.

executeJob() ​

Executes a single job.

View source

Arguments ​

  • $id (string, null) – The job ID, if a specific job should be run

Returns ​

boolean – Whether a job was found

getHasReservedJobs() ​

Returns whether there are any reserved jobs.

View source

Returns ​

boolean

getHasWaitingJobs() ​

Returns whether there are any waiting jobs.

View source

Returns ​

boolean

getJobDetails() ​

Since
3.4.0

Returns detailed info about a single job.

The response array can contain the following keys:

  • delay – the number of seconds remaining before the job will start
  • status – the job status (1 = waiting, 2 = reserved, 3 = done, 4 = failed)
  • progress – the job progress (0-100)
  • progressLabel – the progress label (lazy-translated with craft\i18n\Translation::translate())
  • description – the job description (lazy-translated with craft\i18n\Translation::translate())
  • ttr – the job’s time-to-reserve, in seconds
  • error – the error message (if the job failed)
  • job – the deserialized job

Any other key/value pairs are allowed and will be displayed on the details page.

View source

Arguments ​

Returns ​

array

Throws ​

getJobInfo() ​

Returns info about the jobs in the queue.

The response array should have sub-arrays with the following keys:

  • id – the job ID
  • delay – the number of seconds remaining before the job will start
  • status – the job status (1 = waiting, 2 = reserved, 3 = done, 4 = failed)
  • progress – the job progress (0-100)
  • progressLabel – the progress label (lazy-translated with craft\i18n\Translation::translate())
  • description – the job description (lazy-translated with craft\i18n\Translation::translate())
  • error – the error message (if the job failed)

View source

Arguments ​

Returns ​

array

getTotalDelayed() ​

Returns the total number of delayed jobs

View source

Returns ​

integer

getTotalFailed() ​

Returns the total number of failed jobs

View source

Returns ​

integer

getTotalJobs() ​

Since
3.4.0

Returns the total number of jobs in the queue.

View source

Returns ​

integer

getTotalReserved() ​

Returns the total number of reserved jobs

View source

Returns ​

integer

getTotalWaiting() ​

Returns the total number of waiting jobs

View source

Returns ​

integer

handleError() ​

View source

Arguments ​

  • $event

handleResponse() ​

Figure out how to initiate a new worker.

View source

init() ​

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

View source

isFailed() ​

View source

Arguments ​

  • $id (string) – The job ID.

Returns ​

boolean

listen() ​

DEPRECATED

Deprecated in 3.6.11. Use run() instead.

Listens to the queue and runs new jobs.

View source

Arguments ​

  • $timeout (integer) – The number of seconds to wait between cycles

Returns ​

integer, null – The exit code

push() ​

Pushes job into queue.

View source

Arguments ​

Returns ​

string, null – Id of a job message

release() ​

Releases a job from the queue.

View source

Arguments ​

releaseAll() ​

Since
3.4.0

Releases all jobs.

View source

retry() ​

Re-adds a failed job to the queue.

View source

Arguments ​

retryAll() ​

Since
3.4.0

Retries all failed jobs.

View source

run() ​

Runs all the queued-up jobs.

View source

Arguments ​

  • $repeat (boolean) – Whether to continue listening when the queue is empty.
  • $timeout (integer) – The number of seconds to wait between cycles

Returns ​

integer, null – The exit code

setProgress() ​

Sets the progress for the currently reserved job.

View source

Arguments ​

  • $progress (integer) – The job progress (1-100)
  • $label (string, null) – The progress label

status() ​

View source

Arguments ​

  • $id (string) – Of a job message

Returns ​

integer – Status code

Protected Methods ​

MethodDescription
getCommandId()
handleMessage()
pushMessage()
reserve()
runWorker()Runs worker.

pushMessage() ​

View source

Arguments ​

  • $message (string)
  • $ttr (integer) – Time to reserve in seconds
  • $delay (integer)
  • $priority (mixed)

Returns ​

string – Id of a job message

Throws ​

reserve() ​

View source

Arguments ​

Returns ​

array, null – The payload, or null if there aren't any jobs to reserve

Throws ​

Constants ​

ConstantDescription
STATUS_DONE
STATUS_FAILED
STATUS_RESERVED
STATUS_WAITING

Events ​

EVENT_AFTER_EXEC_AND_RELEASE ​

Type
yii\queue\ExecEvent
Since
4.4.8

The event that is triggered after a job is executed and released.

See also executeJob()