NAME

Proc::tored::Role::Running

SYNOPSIS

package Some::Thing;
use Moo;

with 'Proc::tored::Running';

sub run {
  my $self = shift;

  $self->start;

  while ($self->is_running) {
    do_stuff(...);
  }
}

DESCRIPTION

Classes consuming this role are provided with controls to "start" and "stop" voluntarily, along with a SIGTERM handler that is active while the class "is_running". If a SIGTERM is received via another process (by calling "stop_running_process"), the class will voluntarily "stop" itself.

ATTRIBUTES

poll_wait_time

Optionally specifies the length of time (in fractional seconds) during which the process will sleep when calling "stop_running_service" with a timeout. Defaults to 0.2 seconds.

is_running

Returns true while the service is running in the current process.

sigterm_handler

Used internally to store a previously set $SIG{TERM} handler while "is_running" is true.

METHODS

start

Flags the current process as running. While running, a SIGTERM handler is installed that will "stop" the current process. After calling this method, "is_running" will return true.

stop

Flags the current process as not running and restores any previously configured SIGTERM handlers. Once this method has been called, "is_running" will return false.

stop_running_process

Sends a SIGTERM to the active process. Returns 0 immediately if the pid file does not exist or is empty. Otherwise, polls the running process until the OS reports that it is no longer able to receive signals (using `kill(0, $pid)`).

Accepts a $timeout in fractional seconds, causing the function to return 0 if the process takes longer than $timeout seconds to complete.

Returns the pid of the completed process otherwise.