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
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
Required read-only class name of the worker class, which must consume the role Cantella::Worker::Manager::Prefork.
worker_args
A read-only hash ref of arguments to be passed to instantiate the worker class. Defaults to an empty hash ref.
workers
Read-write integer. The maximum number of subprocesses to have alive at any one time. Defaults to 5.
max_worker_age
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
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
Read-write boolean value option for declaring whether file descriptors should be closed in the forked process. Defaults to true.
pid_to_wheel_map
A read-only HashRef used to map process IDs to POE::Wheel::Run objects
wid_to_wheel_map
A read-only HashRef used to map Wheel IDs to POE::Wheel::Run objects
worker_stderr_log_level
Log level to use when logging stderr output that comes from the child processes.
worker_stdout_log_level
Log level to use when logging stdout output that comes from the child processes.
METHODS
BUILD
Sets up the manager session and its event handlers.
current_worker_count
Returns the number of children workers that are still alive.
worker_wheels
Returns all of the wheel objects for currently living children workers
signal_workers
Sends a signal to all worker processes.
start
Start the manager and begin spawning workers.
pause
Pause the manager and stop it from spawning any new workers.
resume
Resume the manager and resume spawning workers.
shutdown
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
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
- StdoutEventworker_process_stderr
- StderrEventworker_process_close
- CloseEventworker_process_sig_chld
- SIGCHLD
_retire_worker
If the worker process working under $wheel_id is still alive, send a sig TERM
_worker_process_close
See POE::Wheel::Run
_worker_process_sig_chld
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
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
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
Start the manager session.
_pause
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
Signal workers to resume polling and resume the spawning of new workers. The resume event can be triggered sending signal USR2
_shutdown
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.