PHP Class Resque\Worker

Author: Michael Haynes ([email protected])
ファイルを表示 Open project: mjphaynes/php-resque Class Usage Examples

Public Properties

Property Type Description
$statusText Worker status constants as text

Protected Properties

Property Type Description
$blocking True if uses Redis pop blocking
$child Process ID of child worker processes.
$host The host of this worker.
$id String identifying this worker.
$interval Clock speed
$job Current job, if any, being processed by this worker.
$logger logger instance
$memoryLimit Memory limit of worker, if exceeded worker will stop
$pid Process id of this worker
$pidFile File to store process id in
$queues Array of all associated queues for this worker.
$redis Redis The Redis instance.
$shutdown True if on the next iteration, the worker should shutdown.
$shutdownErrors List of shutdown errors to catch
$status Status of the worker.
$timeout Max execution time of job

Public Methods

Method Description
__construct ( mixed $queues = '*', boolean $blocking = true ) Create a new worker.
__toString ( ) : string Generate a string representation of this worker.
allWorkers ( Logger $logger = null ) : array Return all known workers
cancelJob ( ) Cancel the currently running job
cleanup ( ) Look for any workers which should be running on this server and if they're not, remove them from Redis.
doneWorking ( ) Notify Redis that we've finished working on a job, clearing the working state and incrementing the job stats.
forceShutdown ( ) Force an immediate shutdown of the worker, killing any child jobs currently running.
fromId ( string $id, Logger $logger = null ) : Worker Return a worker from it's ID
getBlocking ( ) : boolean Get the queue blocking.
getHost ( ) : Host Get the queue host.
getId ( ) : string Get the worker id.
getInterval ( ) : integer Get the worker interval.
getLogger ( ) : Logger Get the logger instance
getMemoryLimit ( ) : integer Get the queue memory limit.
getPacket ( ) : array | false Fetch the packet for the worker
getPid ( ) : integer Get the worker process id.
getPidFile ( ) : string Get the worker pid file.
getQueues ( ) : string Get the worker queues.
getTimeout ( ) : integer Get the worker queue timeout.
hostWorker ( string $id, string $host = null, Logger $logger = null ) : array | false Return host worker by id
hostWorkers ( string $host = null, Logger $logger = null ) : array Return all known workers
killChild ( ) Kill a forked child job immediately. The job it is processing will not be completed.
log ( ) : mixed Helper function that passes through to logger instance
perform ( Job $job ) Process a single job
queueDelayed ( integer $endTime = null, integer $startTime ) Find any delayed jobs and add them to the queue if found
redisKey ( Worker $worker = null, string $suffix = null ) : string Get the Redis key
register ( ) Register this worker in Redis and signal handlers that a worker should respond to
resolveQueues ( ) : array Return an array containing all of the queues that this worker should use when searching for jobs.
setBlocking ( boolean $blocking ) Set the queue blocking
setHost ( Host $host ) Set the queue host
setId ( string $id ) Set the worker id
setInterval ( integer $interval ) Set the worker interval
setLogger ( Logger $logger = null ) Set the logger instance
setMemoryLimit ( integer $memoryLimit ) Set the queue memory limit
setPid ( integer $pid ) Set the worker process id - this is done when worker is loaded from memory
setPidFile ( string $pidFile ) Set the worker pid file
setQueues ( string $queues ) : array Set the worker queues
setStatus ( integer $status ) Update the status indicator for the current worker with a new status.
setTimeout ( string $timeout ) : string Set the worker queue timeout
shutdown ( ) Schedule a worker for shutdown. Will finish processing the current job and when the timeout interval is reached, the worker will shut down.
sigCancelJob ( ) Signal handler callback for USR1, cancel current job.
sigForceShutdown ( integer $sig ) Signal handler callback for TERM or INT, forces shutdown of worker
sigPause ( ) Signal handler callback for USR2, pauses processing of new jobs.
sigResume ( ) Signal handler callback for CONT, resumes worker allowing it to pick up new jobs.
sigShutdown ( ) Signal handler callback for QUIT, shutdown the worker.
sigWakeUp ( ) Signal handler for SIGPIPE, in the event the Redis connection has gone away.
toArray ( ) : array Return array representation of this job
unregister ( ) Unregister this worker in Redis
work ( ) The primary loop for a worker which when called on an instance starts the worker's life cycle.
workingOn ( Job &$job ) Tell Redis which job we're currently working on.

Protected Methods

Method Description
getProcessTitle ( $status ) Creates process title string from current version and status of worker
interval_string ( ) : string Returns formatted interval string
memoryExceeded ( ) : boolean Determine if the memory limit has been exceeded.
startup ( ) Perform necessary actions to start a worker
updateProcLine ( string $status ) On supported systems, update the name of the currently running process to indicate the current state of a worker.

Method Details

__construct() public method

Create a new worker.
public __construct ( mixed $queues = '*', boolean $blocking = true )
$queues mixed Queues for the worker to watch
$blocking boolean Use Redis blocking

__toString() public method

Generate a string representation of this worker.
public __toString ( ) : string
return string String identifier for this worker instance.

allWorkers() public static method

Return all known workers
public static allWorkers ( Logger $logger = null ) : array
$logger Logger
return array

cancelJob() public method

Cancel the currently running job
public cancelJob ( )

cleanup() public method

This is a form of garbage collection to handle cases where the server may have been killed and the workers did not die gracefully and therefore leave state information in Redis.
public cleanup ( )

doneWorking() public method

Notify Redis that we've finished working on a job, clearing the working state and incrementing the job stats.
public doneWorking ( )

forceShutdown() public method

Force an immediate shutdown of the worker, killing any child jobs currently running.
public forceShutdown ( )

fromId() public static method

Return a worker from it's ID
public static fromId ( string $id, Logger $logger = null ) : Worker
$id string Worker id
$logger Logger Logger for the worker to use
return Worker

getBlocking() public method

Get the queue blocking.
public getBlocking ( ) : boolean
return boolean

getHost() public method

Get the queue host.
public getHost ( ) : Host
return Host

getId() public method

Get the worker id.
public getId ( ) : string
return string

getInterval() public method

Get the worker interval.
public getInterval ( ) : integer
return integer

getLogger() public method

Get the logger instance
public getLogger ( ) : Logger
return Logger

getMemoryLimit() public method

Get the queue memory limit.
public getMemoryLimit ( ) : integer
return integer Memory limit

getPacket() public method

Fetch the packet for the worker
public getPacket ( ) : array | false
return array | false The worker packet or false

getPid() public method

Get the worker process id.
public getPid ( ) : integer
return integer

getPidFile() public method

Get the worker pid file.
public getPidFile ( ) : string
return string

getProcessTitle() protected method

Creates process title string from current version and status of worker
protected getProcessTitle ( $status )

getQueues() public method

Get the worker queues.
public getQueues ( ) : string
return string

getTimeout() public method

Get the worker queue timeout.
public getTimeout ( ) : integer
return integer Worker queue timeout

hostWorker() public static method

Return host worker by id
public static hostWorker ( string $id, string $host = null, Logger $logger = null ) : array | false
$id string Worker id
$host string Hostname
$logger Logger Logger
return array | false

hostWorkers() public static method

Return all known workers
public static hostWorkers ( string $host = null, Logger $logger = null ) : array
$host string Hostname
$logger Logger Logger
return array

interval_string() protected method

Returns formatted interval string
protected interval_string ( ) : string
return string

killChild() public method

Kill a forked child job immediately. The job it is processing will not be completed.
public killChild ( )

log() public method

Helper function that passes through to logger instance
See also: Logger::log For more documentation
public log ( ) : mixed
return mixed

memoryExceeded() protected method

Determine if the memory limit has been exceeded.
protected memoryExceeded ( ) : boolean
return boolean

perform() public method

Process a single job
public perform ( Job $job )
$job Job The job to be processed.

queueDelayed() public method

Find any delayed jobs and add them to the queue if found
public queueDelayed ( integer $endTime = null, integer $startTime )
$endTime integer optional end time for range
$startTime integer optional start time for range

redisKey() public static method

Get the Redis key
public static redisKey ( Worker $worker = null, string $suffix = null ) : string
$worker Worker the worker to get the key for
$suffix string to be appended to key
return string

register() public method

- TERM: Shutdown immediately and stop processing jobs - INT: Shutdown immediately and stop processing jobs - QUIT: Shutdown after the current job finishes processing - USR1: Kill the forked child immediately and continue processing jobs
public register ( )

resolveQueues() public method

If * is found in the list of queues, every queue will be searched in alphabetic order.
public resolveQueues ( ) : array
return array Array of associated queues.

setBlocking() public method

Set the queue blocking
public setBlocking ( boolean $blocking )
$blocking boolean Should worker use Redis blocking

setHost() public method

Set the queue host
public setHost ( Host $host )
$host Host The host to set for this worker

setId() public method

Set the worker id
public setId ( string $id )
$id string Id to set to

setInterval() public method

Set the worker interval
public setInterval ( integer $interval )
$interval integer The worker interval

setLogger() public method

Set the logger instance
public setLogger ( Logger $logger = null )
$logger Logger The logger for this worker

setMemoryLimit() public method

Set the queue memory limit
public setMemoryLimit ( integer $memoryLimit )
$memoryLimit integer Memory limit

setPid() public method

Set the worker process id - this is done when worker is loaded from memory
public setPid ( integer $pid )
$pid integer Set worker pid

setPidFile() public method

Set the worker pid file
public setPidFile ( string $pidFile )
$pidFile string Filename to store pid in

setQueues() public method

Set the worker queues
public setQueues ( string $queues ) : array
$queues string Queues for worker to watch
return array

setStatus() public method

Update the status indicator for the current worker with a new status.
public setStatus ( integer $status )
$status integer The status of the worker

setTimeout() public method

Set the worker queue timeout
public setTimeout ( string $timeout ) : string
$timeout string Worker queue timeout
return string

shutdown() public method

Schedule a worker for shutdown. Will finish processing the current job and when the timeout interval is reached, the worker will shut down.
public shutdown ( )

sigCancelJob() public method

Signal handler callback for USR1, cancel current job.
public sigCancelJob ( )

sigForceShutdown() public method

Signal handler callback for TERM or INT, forces shutdown of worker
public sigForceShutdown ( integer $sig )
$sig integer Signal that was sent

sigPause() public method

Signal handler callback for USR2, pauses processing of new jobs.
public sigPause ( )

sigResume() public method

Signal handler callback for CONT, resumes worker allowing it to pick up new jobs.
public sigResume ( )

sigShutdown() public method

Signal handler callback for QUIT, shutdown the worker.
public sigShutdown ( )

sigWakeUp() public method

Attempts to reconnect to Redis, or raises an Exception.
public sigWakeUp ( )

startup() protected method

Perform necessary actions to start a worker
protected startup ( )

toArray() public method

Return array representation of this job
public toArray ( ) : array
return array

unregister() public method

Unregister this worker in Redis
public unregister ( )

updateProcLine() protected method

supported systems are - PHP Version < 5.5.0 with the PECL proctitle module installed - PHP Version >= 5.5.0 using built in method
protected updateProcLine ( string $status )
$status string The updated process title.

work() public method

Queues are checked every $interval (seconds) for new jobs.
public work ( )

workingOn() public method

Tell Redis which job we're currently working on.
public workingOn ( Job &$job )
$job Job Job instance containing the job we're working on.

Property Details

$blocking protected_oe property

True if uses Redis pop blocking
protected $blocking

$child protected_oe property

Process ID of child worker processes.
protected $child

$host protected_oe property

The host of this worker.
protected $host

$id protected_oe property

String identifying this worker.
protected $id

$interval protected_oe property

Clock speed
protected $interval

$job protected_oe property

Current job, if any, being processed by this worker.
protected $job

$logger protected_oe property

logger instance
protected $logger

$memoryLimit protected_oe property

Memory limit of worker, if exceeded worker will stop
protected $memoryLimit

$pid protected_oe property

Process id of this worker
protected $pid

$pidFile protected_oe property

File to store process id in
protected $pidFile

$queues protected_oe property

Array of all associated queues for this worker.
protected $queues

$redis protected_oe property

The Redis instance.
protected Redis,resque $redis
return Redis

$shutdown protected_oe property

True if on the next iteration, the worker should shutdown.
protected $shutdown

$shutdownErrors protected_oe property

List of shutdown errors to catch
protected $shutdownErrors

$status protected_oe property

Status of the worker.
protected $status

$statusText public_oe static_oe property

Worker status constants as text
public static $statusText

$timeout protected_oe property

Max execution time of job
protected $timeout