The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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

arguments: none
return value: $work_data

Should return undef if there's no work avaliable and a single defined scalar representing the inputs to the work if there is.

work

arguments: $work_data
return value: none

Do whatever!

ATTRIBUTES

logger

logger - reader

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

interval - accessor

Read-write integer. The number of seconds to wait between poll events. Will default to 2 seconds.

alias

alias - accessor

Read-only string. This is the alias of the session managing the workers and will default to a UUID string by default.

METHODS

BUILD

arguments: \%args
return value: none

after BUILD Sets up the polling session and its event handlers.

start

arguments: none
return value: none

Begin the event loop

pause

arguments: none
return value: none

Stop polling for work.

resume

arguments: none
return value: none

Resume polling for work.

shutdown

arguments: none
return value: none

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

handles event: poll
arguments: none
return value: none

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

handles event: work
arguments: $work_info
return value: none

Do this one job.

_start

handles event: _start
arguments: none
return value: none

Start the polling process

_pause

handles event: _pause
arguments: $until
return value: none

Pause the polling process until $until

_resume

handles event: _resume
arguments: none
return value: none

Resume the polling process

_shutdown

handles event: shutdown
arguments: none
return value: none

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.