Daemonise

 view release on metacpan or  search on metacpan

examples/dungeonkeeper  view on Meta::CPAN

#!/usr/bin/env perl

# PODNAME: dungeonkeeper

# ABSTRACT: dungeonkeeper - hase backend supervisor

# VERSION

use Modern::Perl;
use experimental 'smartmatch';
use Daemonise;
use Getopt::Long;
use File::Find::Rule;
use JSON;
use Proc::ProcessTable;

my $debug;
my $conf      = '/etc/daemonise/dungeonkeeper.conf';
my $hase_conf = '/etc/daemonise/hase.conf';
my $foreground;

GetOptions(
    "debug|d"         => \$debug,
    "config|c=s"      => \$conf,
    "rabbit_conf|r=s" => \$hase_conf,
    "foreground|f"    => \$foreground,
) or die;

my $command = shift @ARGV;
die "$0 (start|onestart|stop|restart|status) [daemon1 daemon2 ...]"
    unless $command;

my $d = Daemonise->new();
$d->debug(1)      if $debug;
$d->debug(1)      if ($d->hostname =~ m/devel/);
$d->foreground(1) if $foreground;
$d->pid_file('/var/run/' . $d->name . '.pid');
$d->config_file($conf);
$d->configure;
$d->load_plugin('Riemann');
$d->load_plugin('RabbitMQ');
$d->admin_queue(0);

given ($command) {
    when ("start")    { start(@ARGV); }
    when ("onestart") { start(@ARGV); }
    when ("stop")     { stop(@ARGV); }
    when ("restart")  { restart(@ARGV); }
    when ("status")   { status(@ARGV); }
    default           { die "unknown command: $command"; }
}

sub main {
    _handle_admin_message();
    _monitor_rabbits();
    sleep($d->interval || 2);
}

sub rabbits {
    my $amount = {};

    my $t = new Proc::ProcessTable;
    foreach my $p (@{ $t->table }) {
        next unless $p->cmndline =~ m/^.*perl bin\/(.*).rabbit .*$/gi;

        # print "$1: " . $p->pid . "\n";
        $amount->{$1} += 1;
    }



( run in 1.204 second using v1.01-cache-2.11-cpan-0d23b851a93 )