NAME
Resque::Worker - Does the hard work of babysitting Resque::Job's
VERSION
version 0.42
ATTRIBUTES
resque
The Resque object running this worker.
queues
Queues this worker should fetch jobs from.
stat
See Resque::Stat.
id
Unique identifier for the running worker. Used to set process status all around.
The worker stringify to this attribute.
verbose
Set to a true value to make this worker report what's doing while on work().
cant_fork
Set it to a true value to stop this worker from fork jobs.
By default, the worker will fork the job out and control the children process. This make the worker more resilient to memory leaks.
cant_poll
Set it to a true value to stop this worker from polling for jobs and use experimental blocking pop instead.
See timeout().
child
PID of current running child.
shutdown
When true, this worker will shutdown after finishing current job.
paused
When true, this worker won't proccess more jobs till false.
interval
Float representing the polling frequency. The default is 5 seconds, but for a semi-active app you may want to use a smaller value.
timeout
Integer representing the blocking timeout. The default is not to block but to poll queues (see inverval), so this attribute will be completely ignored unless dont_poll(). The default is 30 seconds. Setting it to 0 will make reserve() to block until some job is assigned to this workers and will prevent autoconfig() to be called until it happen.
autoconfig
An optional callback to be called periodically while work()'ing. It's main purpose is to allow running auto-config code as this function will receive this worker as it's only argument and will be called before reserving the first job.
When this callback is provided, it will be called on every wheel iteration, so it's recommended to keep track of time to prevent running slow re-configuration code every time.
METHODS
pause
Stop processing jobs after the current one has completed (if we're currently running one).
$worker->pause();
unpause
Start processing jobs again after a pause
$worker->unpause();
shutdown_please
Schedule this worker for shutdown. Will finish processing the current job.
$worker->shutdown_please();
shutdown_now
Kill the child and shutdown immediately.
$worker->shutdown_now();
work
Calling this method will make this worker start pulling & running jobs from queues().
This is the main wheel and will run while shutdown() is false.
$worker->work();
work_tick
Perform() one job and wait till it finish.
$worker->work_tick();
perform
Call perform() on the given Resque::Job capturing and reporting any exception.
$worker->perform( $job );
kill_child
Kills the forked child immediately, without remorse. The job it is processing will not be completed.
$worker->kill_child();
add_queue
Add a queue this worker should listen to.
$worker->add_queue( "queuename" );
del_queue
Stop listening to the given queue.
$worker->del_queue( "queuename" );
reserve
Pull the next job to be precessed.
my $job = $worker->reserve();
working_on
Set worker and working status on the given Resque::Job.
$job->working_on( $resque_job );
done_working
Inform the backend this worker has done its current job
$job->done_working();
started
What time did this worker start? Returns an instance of DateTime.
my $datetime = $worker->started();
set_started
Tell Redis we've started
$worker->set_started();
processing
Returns a hash explaining the Job we're currently processing, if any.
$worker->processing();
processing_map
Returns a hashref of processing info for a given worker or worker ID list
$worker->processing( $worker1, $worker2, $worker3->id );
processing_started
What time did this worker started to work on current job? Returns an instance of DateTime or undef when it's not working.
my $datetime = $worker->processing_started();
state
Returns a string representing the current worker state, which can be either working or idle
my $state = $worker->state();
is_working
Boolean - true if working, false if not
my $working = $worker->is_working();
is_idle
Boolean - true if idle, false if not
my $idle = $worker->is_idle();
procline
Given a string, sets the procline ($0) and logs. Procline is always in the format of: resque-VERSION: STRING
$worker->procline( "string" );
startup
Helper method called by work() to:
1. register_signal_handlers()
2. prune_dead_workers();
3. register_worker();
$worker->startup();
register_signal_handlers
Registers the various signal handlers a worker responds to.
TERM: Shutdown immediately, stop processing jobs.
INT: Shutdown immediately, stop processing jobs.
QUIT: Shutdown after the current job has finished processing.
USR1: Kill the forked child immediately, continue processing jobs.
USR2: Don't process any new jobs
CONT: Start processing jobs again after a USR2
$worker->register_signal_handlers();
prune_dead_workers
Looks for any workers which should be running on this server and, if they're not, removes them from Redis.
This is a form of garbage collection. If a server is killed by a hard shutdown, power failure, or something else beyond our control, the Resque workers will not die gracefully and therefore will leave stale state information in Redis.
By checking the current Redis state against the actual environment, we can determine if Redis is old and clean it up a bit.
$worker->prune_dead_worker();
register_worker
Registers ourself as a worker. Useful when entering the worker lifecycle on startup.
$worker->register_worker();
refresh_id
Do the dirty work after changing the queues on an already register_worker().
This will update backend on a single transactionto reflex current queues by changing this worker ID which need to unregister_worker() and register_worker() again while keeping stat() and started().
Only useful when you dinamically update queues and want to watch it on the web interface.
unregister_worker
Unregisters ourself as a worker. Useful when shutting down.
$worker->unregister_worker();
worker_pids
Returns an Array of string pids of all the other workers on this machine. Useful when pruning dead workers on startup.
my @pids = $worker->worker_pids();
log
If verbose() is true, this will print to STDERR.
$worker->log( 'message here' );
processed
Retrieve from Resque::Stat many jobs has done this worker. Pass a true argument to increment by one before retrieval.
my $jobs_run = $worker->processed( $boolean );
failed
How many failed jobs has this worker seen. Pass a true argument to increment by one before retrieval.
my $jobs_run = $worker->failed( $boolean );
find
Returns a single worker object. Accepts a string id.
my $worker_object = $worker->find( $worker_id );
all
Returns a list of all worker registered on the backend, or an arrayref in scalar context;
my @workers = $worker->all();
exists
Returns true if the given worker id exists on redis() backend.
my $exists = $worker->exists( $worker_id );
AUTHOR
Diego Kuperman <diego@freekeylabs.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2021 by Diego Kuperman.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.