Class Resque_Worker

InheritanceResque_Worker

Resque worker that handles checking queues for jobs, fetching them off the queues, running them and handling the result.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$logLevel int Current log level of this worker. Resque_Worker

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__construct() Instantiate a new worker, given a list of queues that it should be working on. The list of queues should be supplied in the priority that they should be checked for jobs (first come, first served) Resque_Worker
__toString() Generate a string representation of this worker. Resque_Worker
all() Return all workers known to Resque as instantiated instances. Resque_Worker
doneWorking() Notify Redis that we've finished working on a job, clearing the working state and incrementing the job stats. Resque_Worker
exists() Given a worker ID, check if it is registered/valid. Resque_Worker
find() Given a worker ID, find it and return an instantiated worker class for it. Resque_Worker
getStat() Get a statistic belonging to this worker. Resque_Worker
job() Return an object describing the job this worker is currently working on. Resque_Worker
killChild() Kill a forked child job immediately. The job it is processing will not be completed. Resque_Worker
log() Output a given log message to STDOUT. Resque_Worker
pauseProcessing() Signal handler callback for USR2, pauses processing of new jobs. Resque_Worker
perform() Process a single job. Resque_Worker
pruneDeadWorkers() Look for any workers which should be running on this server and if they're not, remove them from Redis. Resque_Worker
queues() Return an array containing all of the queues that this worker should use when searching for jobs. Resque_Worker
reestablishRedisConnection() Signal handler for SIGPIPE, in the event the redis connection has gone away. Resque_Worker
registerWorker() Register this worker in Redis. Resque_Worker
reserve() Attempt to find a job from the top of one of the queues for this worker. Resque_Worker
setId() Set the ID of this worker to a given ID string. Resque_Worker
shutdown() Schedule a worker for shutdown. Will finish processing the current job and when the timeout interval is reached, the worker will shut down. Resque_Worker
shutdownNow() Force an immediate shutdown of the worker, killing any child jobs currently running. Resque_Worker
unPauseProcessing() Signal handler callback for CONT, resumes worker allowing it to pick up new jobs. Resque_Worker
unregisterWorker() Unregister this worker in Redis. (shutdown etc) Resque_Worker
work() The primary loop for a worker which when called on an instance starts the worker's life cycle. Resque_Worker
workerPids() Return an array of process IDs for all of the Resque workers currently running on this machine. Resque_Worker
workingOn() Tell Redis which job we're currently working on. Resque_Worker

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
LOG_NONE 0 Resque_Worker
LOG_NORMAL 1 Resque_Worker
LOG_VERBOSE 2 Resque_Worker

Property Details

$logLevel public property

Current log level of this worker.

public int $logLevel 0

Method Details

__construct() public method

Instantiate a new worker, given a list of queues that it should be working on. The list of queues should be supplied in the priority that they should be checked for jobs (first come, first served)

Passing a single '*' allows the worker to work on all queues in alphabetical order. You can easily add new queues dynamically and have them worked on using this method.

public void __construct ( $queues )
$queues string|array

String with a single queue name, array with multiple.

__toString() public method

Generate a string representation of this worker.

public string __toString ( )
return string

String identifier for this worker instance.

all() public static method

Return all workers known to Resque as instantiated instances.

public static array all ( )
doneWorking() public method

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

public void doneWorking ( )
exists() public static method

Given a worker ID, check if it is registered/valid.

public static boolean exists ( $workerId )
$workerId string

ID of the worker.

return boolean

True if the worker exists, false if not.

find() public static method

Given a worker ID, find it and return an instantiated worker class for it.

public static Resque_Worker find ( $workerId )
$workerId string

The ID of the worker.

return Resque_Worker

Instance of the worker. False if the worker does not exist.

getStat() public method

Get a statistic belonging to this worker.

public int getStat ( $stat )
$stat string

Statistic to fetch.

return int

Statistic value.

job() public method

Return an object describing the job this worker is currently working on.

public object job ( )
return object

Object with details of current job.

killChild() public method

Kill a forked child job immediately. The job it is processing will not be completed.

public void killChild ( )
log() public method

Output a given log message to STDOUT.

public void log ( $message )
$message string

Message to output.

pauseProcessing() public method

Signal handler callback for USR2, pauses processing of new jobs.

public void pauseProcessing ( )
perform() public method

Process a single job.

public void perform ( Resque_Job $job )
$job Resque_Job

The job to be processed.

pruneDeadWorkers() public method

Look for any workers which should be running on this server and if they're not, remove them from Redis.

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

public void pruneDeadWorkers ( )
queues() public method

Return an array containing all of the queues that this worker should use when searching for jobs.

If * is found in the list of queues, every queue will be searched in alphabetic order. (@see $fetch)

public array queues ( $fetch true )
$fetch boolean

If true, and the queue is set to *, will fetch all queue names from redis.

return array

Array of associated queues.

reestablishRedisConnection() public method

Signal handler for SIGPIPE, in the event the redis connection has gone away.

Attempts to reconnect to redis, or raises an Exception.

public void reestablishRedisConnection ( )
registerWorker() public method

Register this worker in Redis.

public void registerWorker ( )
reserve() public method

Attempt to find a job from the top of one of the queues for this worker.

public object|boolean reserve ( )
return object|boolean

Instance of Resque_Job if a job is found, false if not.

setId() public method

Set the ID of this worker to a given ID string.

public void setId ( $workerId )
$workerId string

ID for the worker.

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 void shutdown ( )
shutdownNow() public method

Force an immediate shutdown of the worker, killing any child jobs currently running.

public void shutdownNow ( )
unPauseProcessing() public method

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

public void unPauseProcessing ( )
unregisterWorker() public method

Unregister this worker in Redis. (shutdown etc)

public void unregisterWorker ( )
work() public method

The primary loop for a worker which when called on an instance starts the worker's life cycle.

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

public void work ( $interval 5 )
$interval int

How often to check for new jobs across the queues.

workerPids() public method

Return an array of process IDs for all of the Resque workers currently running on this machine.

public array workerPids ( )
return array

Array of Resque worker process IDs.

workingOn() public method

Tell Redis which job we're currently working on.

public void workingOn ( Resque_Job $job )
$job object

Resque_Job instance containing the job we're working on.