NAME

Cantella::Worker::Manager::Prefork - Preforking POE worker-pool manager

SYNOPSIS

my $manager = Cantella::Worker::Manager::Prefork->new(
  logger => $logger,
  workers => 5,
  worker_class => 'TestWorkerClass',
  max_worker_age => 3600,
  worker_args => {
    interval => 5,
    logger => $logger,
  }
);

$manager->start;

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.

worker_class

worker_class - reader

Required read-only class name of the worker class, which must consume the role Cantella::Worker::Manager::Prefork.

worker_args

worker_args - reader

A read-only hash ref of arguments to be passed to instantiate the worker class. Defaults to an empty hash ref.

workers

workers - accessor

Read-write integer. The maximum number of subprocesses to have alive at any one time. Defaults to 5.

max_worker_age

max_worker_age - accessor
has_max_worker_age - predicate
clear_max_worker_age - clearer

Optional read-write integer. if this value is set, the manager will retire workers when they reach a certain age and spawn a new worker. You may have seen this behavior in mailscanner. It is useful for times when children may, in rare cases, require large amounts of memory and you want to periodically replace them to free it again.

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.

close_on_call

close_on_call - accessor

Read-write boolean value option for declaring whether file descriptors should be closed in the forked process. Defaults to true.

pid_to_wheel_map

pid_to_wheel_map - accessor

A read-only HashRef used to map process IDs to POE::Wheel::Run objects

wid_to_wheel_map

wid_to_wheel_map - accessor

A read-only HashRef used to map Wheel IDs to POE::Wheel::Run objects

worker_stderr_log_level

worker_stderr_log_level - reader

Log level to use when logging stderr output that comes from the child processes.

worker_stdout_log_level

worker_stdout_log_level - reader

Log level to use when logging stdout output that comes from the child processes.

METHODS

BUILD

arguments: \%args
return value: none

Sets up the manager session and its event handlers.

current_worker_count

arguments: none
return value: $number_of_live_children

Returns the number of children workers that are still alive.

worker_wheels

arguments: none
<return value:> @worker_poe_wheel_run_objects

Returns all of the wheel objects for currently living children workers

signal_workers

arguments: $signal
<return value:> none

Sends a signal to all worker processes.

start

arguments: none
return value: none

Start the manager and begin spawning workers.

pause

arguments: $until
return value: none

Pause the manager and stop it from spawning any new workers.

resume

arguments: $when
return value: none

Resume the manager and resume spawning workers.

shutdown

arguments: none
return value: none

Stop the manager from spawning any new workers and end the session after all workers have finished working.

EVENT HANDLERS

The following methods are POE event handlers. They are not menat to be called directly and will not work if you do. When applicable, arguments will be passed into the methods in ARG0, ARG1, etc.

_spawn_workers

handles event: spawn_workers
arguments: none
return value: none

Spawn workers if current_worker_count is less than workers. This is also the place where an alarm to retire a worker will be set if workers have a maximum age.

The following events are set up to communicate with children processes:

worker_process_stdout - StdoutEvent
worker_process_stderr - StderrEvent
worker_process_close - CloseEvent
worker_process_sig_chld - SIGCHLD

_retire_worker

handles event: retire_worker
arguments: $wheel_id
return value: none

If the worker process working under $wheel_id is still alive, send a sig TERM

_worker_process_close

handles event: worker_process_close
arguments: none
return value: none

See POE::Wheel::Run

_worker_process_sig_chld

handles event: worker_process_sig_chld
arguments: $pid, $exit_status_code
return value: none

Once a sig CHLD comes back from a worker, this event will schedule a spawn_workers event to make sure there's enough workers alive.

_worker_process_stdout

handles event: worker_process_stdout
arguments: $line
return value: none

This event will be triggered for every line that the child process outputs to STDOUT. By default, these will be logged to the log-level selcted with worker_stdout_log_level. See POE::Wheel::Run for more.

_worker_process_stderr

handles event: worker_process_stderr
arguments: $line
return value: none

This event will be triggered for every line that the child process outputs to STDOUT. By default, these will be logged to the log-level selcted with worker_stdout_log_level. See POE::Wheel::Run for more.

_start

handles event: _start
arguments: none
return value: none

Start the manager session.

_pause

handles event: _pause
arguments: none
return value: none

Signal workers to stop polling until notified and suspend the spawning of new workers. The pause event can be triggered by sending signal USR1

_resume

handles event: _resume
arguments: none
return value: none

Signal workers to resume polling and resume the spawning of new workers. The resume event can be triggered sending signal USR2

_shutdown

handles event: shutdown
arguments: none
return value: none

Remove all alarms, signal the workers to shutdown and wait for the session to die The shutdown event can be triggered sending signal INT or TERM

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::Role::Worker, 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.