Hi everybody,

yesterday, when writing the core of another daemon intended to run
on one of the servers I administer, I caught myself copy-pasting
code from other daemons. Not some lines but more or less the whole
main part.

After a bit of thinking, I realized that there must be a better
solution. Probably hidden somewhere in CPAN. However, even after
searching quite a long time; I haven't found one.
None - or should I rather say that I found a whole bunch of them?

There are modules (App::Control, Proc::PID::File, File::PID etc.),
all of them doing just one thing, exactly in the standard Unix
manner.
But at least as far as I can see, in this specific case, it leads
into having quite some code a in every daemon which is more or
less redundant.

Thus my question: what about having a solution like this - or does
it exist (and I was just too blind to see it; in that case: sorry
for bothering you)?

A class combines the pid file, daemonize code etc in it (or glues
it together from some modules), so that I can use something like
the following when writing a daemon - I'll name it Proc::Daemonizer
in the examples.

Proc::Daemonzier would take care of
 - creating a pid file
 - checking if the daemon is really running if the pid file exists
   (detection of stale pid files)
 - starting only if no instance is running
 - restart the daemon|signal him to reconfigure itself
   (see example)

Here is how I think an application could use the interface:
   package MyDaemon;

   use base qw (Proc::Daemonizer);
       #  (or whatever an appropriate name would be)

   sub start {
      my $type = shift;
      $type->SUPER::start(@_);
      # do my own things
   };

   sub reconfigure {
      my $self = shift;

      $self->log("WARNING: reconfigure not implemented");
   };
   ...

   package main;

   MyDaemon -> start('only_if_not_running')
      if ($#ARGV = 0 && $ARGV[0] eq '--daemon');
      # start the daemon

   MyDaemon -> kill() if ($#ARGV = 0 && $ARGV[0] eq '--quit');
      # stop the daemon

   MyDaemon -> restart() if ($#ARGV = 0 && $ARGV[0] eq '--restart');
      # restart the daemon

   MyDaemon -> restart() if ($#ARGV = 0 && $ARGV[0] eq '--restart');
      # restart the daemon

   if ($#ARGV = 0 && $ARGV[0] eq '--status') {
      if (MyDaemon -> running ()) {
         print "daemon running as pid ".MyDaemon->running()."\n";
      } else {
         print "daemon not running\n";
      }
    }

Reply via email to