PHP Class Resque\Worker

Author: Michael Haynes ([email protected])
Afficher le fichier Open project: mjphaynes/php-resque Class Usage Examples

Méthodes publiques

Свойство Type Description
$statusText Worker status constants as text

Protected Properties

Свойство 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

Méthodes publiques

Méthode 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.

Méthodes protégées

Méthode 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 méthode

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 méthode

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

allWorkers() public static méthode

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

cancelJob() public méthode

Cancel the currently running job
public cancelJob ( )

cleanup() public méthode

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 méthode

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

forceShutdown() public méthode

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

fromId() public static méthode

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
Résultat Worker

getBlocking() public méthode

Get the queue blocking.
public getBlocking ( ) : boolean
Résultat boolean

getHost() public méthode

Get the queue host.
public getHost ( ) : Host
Résultat Host

getId() public méthode

Get the worker id.
public getId ( ) : string
Résultat string

getInterval() public méthode

Get the worker interval.
public getInterval ( ) : integer
Résultat integer

getLogger() public méthode

Get the logger instance
public getLogger ( ) : Logger
Résultat Logger

getMemoryLimit() public méthode

Get the queue memory limit.
public getMemoryLimit ( ) : integer
Résultat integer Memory limit

getPacket() public méthode

Fetch the packet for the worker
public getPacket ( ) : array | false
Résultat array | false The worker packet or false

getPid() public méthode

Get the worker process id.
public getPid ( ) : integer
Résultat integer

getPidFile() public méthode

Get the worker pid file.
public getPidFile ( ) : string
Résultat string

getProcessTitle() protected méthode

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

getQueues() public méthode

Get the worker queues.
public getQueues ( ) : string
Résultat string

getTimeout() public méthode

Get the worker queue timeout.
public getTimeout ( ) : integer
Résultat integer Worker queue timeout

hostWorker() public static méthode

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
Résultat array | false

hostWorkers() public static méthode

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

interval_string() protected méthode

Returns formatted interval string
protected interval_string ( ) : string
Résultat string

killChild() public méthode

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

log() public méthode

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

memoryExceeded() protected méthode

Determine if the memory limit has been exceeded.
protected memoryExceeded ( ) : boolean
Résultat boolean

perform() public méthode

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

queueDelayed() public méthode

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 méthode

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
Résultat string

register() public méthode

- 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 méthode

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

setBlocking() public méthode

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

setHost() public méthode

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

setId() public méthode

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

setInterval() public méthode

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

setLogger() public méthode

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

setMemoryLimit() public méthode

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

setPid() public méthode

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 méthode

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

setQueues() public méthode

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

setStatus() public méthode

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 méthode

Set the worker queue timeout
public setTimeout ( string $timeout ) : string
$timeout string Worker queue timeout
Résultat string

shutdown() public méthode

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 méthode

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

sigForceShutdown() public méthode

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

sigPause() public méthode

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

sigResume() public méthode

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

sigShutdown() public méthode

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

sigWakeUp() public méthode

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

startup() protected méthode

Perform necessary actions to start a worker
protected startup ( )

toArray() public méthode

Return array representation of this job
public toArray ( ) : array
Résultat array

unregister() public méthode

Unregister this worker in Redis
public unregister ( )

updateProcLine() protected méthode

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 méthode

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

workingOn() public méthode

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
Résultat 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