NAME
Cantella::Worker::Role::Worker - Polling POE worker
SYNOPSIS
package TestWorkerPool;
use Moose;
with 'Cantella::Worker::Role::Worker';
my @work_stack = ( 1 .. 10);
sub get_work {
my $self = shift;
if( @work_stack ){
return shift @work_stack;
} else {
$self->shutdown;
return;
}
}
sub work {
my ($self, $args) = @_;
sleep(1);
}
REQUIRED METHODS
get_work
Should return undef
if there's no work avaliable and a single defined scalar representing the inputs to the work if there is.
work
Do whatever!
ATTRIBUTES
logger
Read-only Log::Dispatch instance. Defaults to a device that logs to Null. This attribute can coerce from a hash or array reference, see MooseX::Types::Log::Dispatch for details.
interval
Read-write integer. The number of seconds to wait between poll
events. Will default to 2
seconds.
alias
Read-only string. This is the alias of the session managing the workers and will default to a UUID string by default.
METHODS
BUILD
after BUILD
Sets up the polling session and its event handlers.
start
Begin the event loop
pause
Stop polling for work.
resume
Resume polling for work.
shutdown
Stop polling for work and end the session after current jobs are done.
EVENT HANDLERS
The following methods are POE event handlers. They are not meant to be called directly and will not work if you do. When applicable, arguments will be passed into the methods in ARG0, ARG1, etc.
_poll
Poll for work. If we find work, schedule another poll event due to execute after the work event. If no jobs are found, schedule the next poll for interval
seconds in the future. This ensures that the worker will always be busy while there is work, but will control it's polling for work when there isn't any available.
_work
Do this one job.
_start
Start the polling process
_pause
Pause the polling process until $until
_resume
Resume the polling process
_shutdown
Remove all alarms and wait for the session to die
OTHER EVENTS
- _keep_alive - Used to keep the session alive while paused. Does nothing other than schedule the next keep alive 1000 seconds away.
- sig_int - mark sig INT as handled and yield to
shutdown
- sig_term - mark sig TERM as handled and yield to
shutdown
- sig_usr1 - mark sig USR1 as handled and yield to
_pause
- sig_usr2 - mark sig USR2 as handled and yield to
_resume
SEE ALSO
Cantella::Worker::Manager::Prefork, Cantella::Worker::Role::Beanstalk
AUTHOR
Guillermo Roditi (groditi) <groditi@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2009-2010 by Guillermo Roditi. This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.