Cantella::Worker::Manager::Prefork - Preforking POE worker-pool manager
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;
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.
INT
TERM
USR1
USR2
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.
STDOUT
STDERR
worker_stderr_log_level
worker_stdout_log_level
STDIN
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.
CHLD
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.
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.
program_name
$0
Required read-only class name of the worker class, which must consume the role Cantella::Worker::Manager::Prefork.
A read-only hash ref of arguments to be passed to instantiate the worker class. Defaults to an empty hash ref.
Read-write integer. The maximum number of subprocesses to have alive at any one time. Defaults to 5.
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.
Read-only string. This is the alias of the session managing the workers and will default to a UUID string by default.
Read-write boolean value option for declaring whether file descriptors should be closed in the forked process. Defaults to true.
Read-write boolean value option for declaring whether worker processes should setsid() to detach themselves from the manager's session. Defaults to true.
setsid()
Read-write boolean value option for declaring whether worker processes should setpgrp() to match the process group of the parent process. Defaults to true.
setpgrp()
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.
1
0
A read-only HashRef used to map process IDs to POE::Wheel::Run objects
A read-only HashRef used to map Wheel IDs to POE::Wheel::Run objects
Log level to use when logging stderr output that comes from the child processes.
Log level to use when logging stdout output that comes from the child processes.
\%args
Sets up the manager session and its event handlers.
$number_of_live_children
Returns the number of children workers that are still alive.
@worker_poe_wheel_run_objects
Returns all of the wheel objects for currently living children workers
$signal
Sends a signal to all worker processes.
Start the manager and begin spawning workers.
$until
Pause the manager and stop it from spawning any new workers.
$when
Resume the manager and resume spawning workers.
Stop the manager from spawning any new workers and end the session after all workers have finished working.
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.
current_worker_count
workers
The following events are set up to communicate with children processes:
worker_process_stdout
worker_process_stderr
worker_process_close
worker_process_sig_chld
retire_worker
$wheel_id
If the worker process working under $wheel_id is still alive, send a sig TERM
See POE::Wheel::Run
$pid, $exit_status_code
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.
$line
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
Cantella::Worker::Role::Worker, Cantella::Worker::Role::Beanstalk
Guillermo Roditi (groditi) <groditi@cpan.org>
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.
To install Cantella::Worker, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Cantella::Worker
CPAN shell
perl -MCPAN -e shell install Cantella::Worker
For more information on module installation, please visit the detailed CPAN module installation guide.