summaryrefslogtreecommitdiff
path: root/lib/Multi/Core.pm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2009-10-25 11:44:42 +0100
committerYorhel <git@yorhel.nl>2009-11-02 22:16:23 +0100
commit77151c26876f1c9f6619df5c588499740407f1a8 (patch)
treec38eac9b402dac94f2e30f1645ad461fcb7c1f01 /lib/Multi/Core.pm
parentc7e110801c5de63d570b857f4bb3784f1ca5fbb3 (diff)
Multi::Core: Implemented daemonizing and PID file
The location of the PID file isn't configurable, so this change currently limits running only one Multi for each VNDB installation. (Not that you'd need more...)
Diffstat (limited to 'lib/Multi/Core.pm')
-rw-r--r--lib/Multi/Core.pm36
1 files changed, 33 insertions, 3 deletions
diff --git a/lib/Multi/Core.pm b/lib/Multi/Core.pm
index 830ca657..076edf51 100644
--- a/lib/Multi/Core.pm
+++ b/lib/Multi/Core.pm
@@ -10,11 +10,33 @@ use warnings;
use POE;
use POE::Component::Pg;
use DBI;
+use POSIX 'setsid', 'pause', 'SIGUSR1';
sub run {
my $p = shift;
+ die "PID file already exists\n" if -e "$VNDB::ROOT/data/multi.pid";
+
+ # fork
+ my $pid = fork();
+ die "fork(): $!" if !defined $pid or $pid < 0;
+
+ # parent process, log PID and wait for child to initialize
+ if($pid > 0) {
+ $SIG{CHLD} = sub { die "Initialization failed.\n"; };
+ $SIG{ALRM} = sub { kill $pid, 9; die "Initialization timeout.\n"; };
+ $SIG{USR1} = sub {
+ open my $P, '>', "$VNDB::ROOT/data/multi.pid" or kill($pid, 9) && die $!;
+ print $P $pid;
+ close $P;
+ exit;
+ };
+ alarm(10);
+ pause();
+ exit 1;
+ }
+
# spawn our SQL handling session
my @db = @{$VNDB::O{db_login}};
my(@dsn) = DBI->parse_dsn($db[0]);
@@ -29,9 +51,6 @@ sub run {
],
);
- # log warnings
- $SIG{__WARN__} = sub {(local$_=shift)=~s/\r?\n//;$poe_kernel->call(core=>log=>'__WARN__: '.$_)};
-
$poe_kernel->run();
}
@@ -53,6 +72,16 @@ sub _start {
# I'm surprised the strict pagma isn't complaining about this
"Multi::$mod"->spawn(%$args);
}
+
+ # finish daemonizing
+ kill SIGUSR1, getppid();
+ setsid();
+ chdir '/';
+ umask 0022;
+ $SIG{__WARN__} = sub {(local$_=shift)=~s/\r?\n//;$poe_kernel->call(core=>log=>'__WARN__: '.$_)};
+ close STDOUT;
+ close STDERR;
+ close STDIN;
}
@@ -87,6 +116,7 @@ sub shutdown {
$_[KERNEL]->call(core => log => 'Shutting down (%s)', $_[ARG1]);
$_[KERNEL]->post(pg => 'shutdown');
$_[KERNEL]->alias_remove('core');
+ unlink "$VNDB::ROOT/data/multi.pid";
}