Proc::tored::Manager
my $proctor = Proc::tored::Manager->new(dir => '/tmp', name => 'my-service'); # Call do_stuff while the service is running or until do_stuff returns false $proctor->service(\&do_stuff) or die sprintf('process %d is already running this service!', $proctor->running_pid); # Signal another process running this service to quit gracefully, throwing an # error if it does not self-terminate after 15 seconds. if (my $pid = $proctor->stop_running_process(15)) { die "process $pid is being stubborn!"; }
Objective interface for creating and managing a proctored service.
Creates a new service object, which can be used to run the service and/or signal another process to quit. The pid file is not created or accessed by this method.
The file name to be used when creating or accessing the service's associated pid file.
A valid directory path where the pid file is to be created or an existing pid file is to be found.
See "poll_wait_time" in Proc::tored::Role::Running.
Returns the file system path created by concatenating the values of dir and name that were passed to new.
dir
name
new
See "is_running" in Proc::tored::Role::Running.
Accepts a code ref which will be called repeatedly until it or "is_running" return false. While the service is running, handlers for SIGTERM, SIGINT, SIGPIPE, and SIGHUP are installed. When one of these signals are received, "is_running" will be set to false and service loop will self-terminate.
SIGTERM
SIGINT
SIGPIPE
SIGHUP
Note that it is possible for a signal to arrive between the "is_running" check and the execution of the code ref. If this is a concern for the caller, it is recommended that the code ref avoid blocking for long periods, such as extended sleep times or long-running database queries which perl cannot interrupt.
sleep
Example using a pool of forked workers, an imaginary task queue, and a secondary condition that decides whether to stop running (aside from the built-in signal handlers):
$proctor->service(sub { # Wait for an available worker, but with a timeout my $worker = $worker_pool->next_available(0.1); if ($worker) { # Pull next task from the queue with a 0.1s timeout my $task = poll_queue_with_timeout(0.1); if ($task) { $worker->assign($task); } } return unless touch_file_exists(); return 1; });
Returns the pid identified in the pid file. Returns 0 if the pid file does not exist or is empty.
See "stop_running_process" in Proc::tored::Role::Running. When called from this class, the $pid parameter is provided via "running_pid".
$pid
Attempts to atomically acquire the run lock. Once held, the pid file is created (if needed) and the current process' pid is written to it, "is_running" will return true and a signal handlers will be active. Existing handlers will be executed after the one assigned for the run lock.
If the lock is acquired, a Guard object is returned that will release the lock once out of scope. Returns undef otherwise.
"service" is preferred to this method for most uses.
To install Proc::tored, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Proc::tored
CPAN shell
perl -MCPAN -e shell install Proc::tored
For more information on module installation, please visit the detailed CPAN module installation guide.