Daemonise
view release on metacpan or search on metacpan
examples/dungeonkeeper view on Meta::CPAN
"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;
}
return $amount;
}
sub start {
my (@daemons) = @_;
# TODO: handle starting of individual bunnies
$d->is_worker(1);
# create PID dir and ensure correct owner/group/permissions
mkdir($d->pid_dir, 0755) unless (-d $d->pid_dir);
my (undef, undef, $uid) = getpwnam($d->user);
my (undef, undef, $gid) = getgrnam($d->group);
chown($uid, $gid, $d->pid_dir) if ($uid or $gid);
die("not a directory: " . $d->bin_dir) unless (-d $d->bin_dir);
$d->start(\&main);
return;
}
sub stop {
my (@daemons) = @_;
$d->configure unless $d->mq->is_connected;
if (@daemons) {
$d->log('requesting to stop daemons: [' . join(' ', @daemons) . ']');
for (@daemons) {
delete $d->config->{bunnies}->{$_}
if exists $d->config->{bunnies}->{$_};
}
$d->queue(
$d->name, {
command => 'configure',
bunnies => $d->config->{bunnies},
});
return;
}
$d->phase('status');
$d->check_pid_file;
restart();
kill(15, $d->running) if $d->running;
return;
}
sub restart {
my (@daemons) = @_;
if (@daemons) {
$d->log('stopping daemons: [' . join(' ', @daemons) . ']');
$d->queue(
'admin.' . $d->hostname,
{ command => 'stop', daemons => \@daemons },
undef, 'amq.fanout'
);
return;
}
# re-read config to make sure all daemons get started again
$d->configure($d->mq->is_connected);
$d->log("stopping all daemons");
$d->queue(
'admin.' . $d->hostname,
{ command => 'stop' },
undef, 'amq.fanout'
( run in 0.944 second using v1.01-cache-2.11-cpan-97f6503c9c8 )