NAME

Proc::tored::Manager - OO interface to creating a proctored service

VERSION

version 0.08

SYNOPSIS

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_wait(15)) {
  die "process $pid is being stubborn!";
}

DESCRIPTION

Objective interface for creating and managing a proctored service.

METHODS

new

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.

name

The file name to be used when creating or accessing the service's associated pid file.

dir

A valid directory path where the pid file is to be created or an existing pid file is to be found.

lock_file

Before writing the pid file, a lock is secured through the atomic creation of a lock file. If the file fails to be created (with O_EXCL), the lock fails.

pid_file

Unless manually specified, the pid file's pid_file is constructed from "name" in "dir".

METHODS

stop

Sets the "stopped" flag for the service.

start

Clears the "stopped" flag for the service.

is_stopped

Returns true if the "stopped" flag has been set.

pause

Sets the "paused" flag for the service.

resume

Clears the "paused" flag for the service.

is_paused

Returns true if the "paused" flag has been set.

clear_flags

Clears both the "stopped" and "paused" flags.

is_running

Returns true if the current process is the active, running process.

service

Accepts a code ref which will be called repeatedly until it returns false or the "stopped" flag is set. If the "paused" flag is set, will continue to rune but will not execute the code block until the "paused" flag has been cleared.

Example using a pool of forked workers, an imaginary task queue, and a secondary condition that decides whether to stop running.

$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;
});

read_pid

Returns the pid identified in the pid file. Returns 0 if the pid file does not exist or is empty.

running_pid

Returns the pid of an already-running process or 0 if the pid file does not exist, is empty, or the process identified by the pid does not exist or is not visible.

stop_wait

Sets the "stopped" flag and blocks until the running_pid exits or the $timeout is reached.

$service->stop_wait(30); # stop and block for up to 30 seconds

AUTHOR

Jeff Ober <jeffober@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Jeff Ober.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.