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;
MANAGER-WORKER COMMUNICATION
Signals
Great care has been taken to provide with basic means of communication between a manager and its workers. The manager process will pass-on INT
, TERM
, USR1
and USR2
signals to it's workers, which means that you only need to worry about interacting with one process.
IO Handles
To communicate with the manager, workers can write to STDOUT
and STDERR
, which the manager will log at the levels of worker_stderr_log_level
and worker_stdout_log_level
. Currently the worker process' STDIN
is reserved for internal communications, but this may change in the future.
Process Management
At start and resume time, as well as when a CHLD
signal is received, the manager process will check the number of workers and spawn new ones, if appropriate. This way, even if a worker unexpectedly dies, it will be promptly replaced. If workers have detached themselves from the manager session, they will outlive their manager, so if they detect the manager has unexpectedly exited, they will signal themselves with a TERM
signal and shut down gracefully to avoid unsupervised processes.
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.
program_name
Read-only non-blank string. If program_name
set, the manager process will be renamed to "${program_name}-pm" and the children to "${program_name}". Please see $0
in perlvar for more information.
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.
worker_detaches
Read-write boolean value option for declaring whether worker processes should setsid()
to detach themselves from the manager's session. Defaults to true.
worker_sets_process_group
Read-write boolean value option for declaring whether worker processes should setpgrp()
to match the process group of the parent process. Defaults to true.
worker_priority
Read-write integer. The priority or niceness that should be given to children processes expressed as a delta of the parent's. For example, to give a worker process a lesser priority, use 1
. To escalate the priority use a negative number. UNIX operating systems require elevated privileges to do this. Defaults to 0
.
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.