diff options
author | Yorhel <git@yorhel.nl> | 2009-07-17 11:38:24 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2009-07-17 11:44:15 +0200 |
commit | 64242a632bde81f6af522b528a21a456467c1f61 (patch) | |
tree | 2c034f2f8d8d26b8d35e68d58f22fad7e47a19f7 /lib/Multi | |
parent | 42734f7fb1b500c09745d45708630803a82ae2ad (diff) |
Multi::Core: Handle SIGTERM and SIGINT
These events will be signalled to Multi's modules as a 'shutdown'
signal.
Diffstat (limited to 'lib/Multi')
-rw-r--r-- | lib/Multi/Core.pm | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/lib/Multi/Core.pm b/lib/Multi/Core.pm index 0e17f894..73f698bb 100644 --- a/lib/Multi/Core.pm +++ b/lib/Multi/Core.pm @@ -22,22 +22,13 @@ sub run { $db[0] = "$dsn[0]:$dsn[1]($dsn[2]):$dsn[4]"; POE::Component::Pg->spawn(alias => 'pg', dsn => $db[0], user => $db[1], password => $db[2]); - # spawn the core session (which only handles logging at this point) + # spawn the core session (which handles logging & external signals) POE::Session->create( package_states => [ - $p => [qw| _start log pg_error |], + $p => [qw| _start log pg_error sig_shutdown shutdown |], ], ); - # dynamically load and spawn modules - for (keys %{$VNDB::M{modules}}) { - my($mod, $args) = ($_, $VNDB::M{modules}{$_}); - next if !$args || ref($args) ne 'HASH'; - require "Multi/$mod.pm"; - # I'm surprised the strict pagma isn't complaining about this - "Multi::$mod"->spawn(%$args); - } - # log warnings $SIG{__WARN__} = sub {(local$_=shift)=~s/\r?\n//;$poe_kernel->call(core=>log=>'__WARN__: '.$_)}; @@ -47,9 +38,21 @@ sub run { sub _start { $_[KERNEL]->alias_set('core'); + $_[KERNEL]->call(core => log => 'Starting Multi '.$VNDB::S{version}); $_[KERNEL]->post(pg => register => error => 'pg_error'); $_[KERNEL]->post(pg => 'connect'); - $_[KERNEL]->call(core => log => 'Starting Multi '.$VNDB::S{version}); + $_[KERNEL]->sig(INT => 'sig_shutdown'); + $_[KERNEL]->sig(TERM => 'sig_shutdown'); + $_[KERNEL]->sig('shutdown', 'shutdown'); + + # dynamically load and spawn modules + for (keys %{$VNDB::M{modules}}) { + my($mod, $args) = ($_, $VNDB::M{modules}{$_}); + next if !$args || ref($args) ne 'HASH'; + require "Multi/$mod.pm"; + # I'm surprised the strict pagma isn't complaining about this + "Multi::$mod"->spawn(%$args); + } } @@ -57,7 +60,7 @@ sub log { # level, msg (my $p = eval { $_[SENDER][2]{$_[CALLER_STATE]}[0] } || '') =~ s/^Multi:://; my $msg = sprintf '%s::%s: %s', $p, $_[CALLER_STATE], $_[ARG1] ? sprintf($_[ARG0], @_[ARG1..$#_]) : $_[ARG0]; - + open(my $F, '>>', $VNDB::M{log_dir}.'/multi.log'); printf "[%s] %s\n", scalar localtime, $msg; close $F; @@ -72,5 +75,19 @@ sub pg_error { # ARG: command, errmsg, [ query, params, orig_session, event-args } +sub sig_shutdown { + # Multi modules should listen to the shutdown signal (but should never call sig_handled() on it!) + $_[KERNEL]->signal($_[SESSION], 'shutdown', 'SIG'.$_[ARG0]); + # consider this event as handled, so our process won't be killed directly + $_[KERNEL]->sig_handled(); +} + + +sub shutdown { + $_[KERNEL]->call(core => log => 'Shutting down (%s)', $_[ARG1]); + $_[KERNEL]->post(pg => 'shutdown'); +} + + 1; |