From df401193dc6b8cf02b6224503a9e7d4c0b2c7017 Mon Sep 17 00:00:00 2001 From: Elijah Perrault Date: Tue, 15 Feb 2011 14:18:48 -0700 Subject: [PATCH] Clean up shutdown. --- bin/auto | 5 ----- doc/CHANGELOG | 1 + lib/Core/Cmd.pm | 6 ++---- lib/Lib/Auto.pm | 27 ++++++++++++++------------- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/bin/auto b/bin/auto index 75430eb..c33fc0e 100755 --- a/bin/auto +++ b/bin/auto @@ -438,9 +438,6 @@ API::Std::cmd_add('SHUTDOWN', 2, 'cmd.shutdown', \%Core::Cmd::HELP_SHUTDOWN, \&C API::Std::cmd_add('RESTART', 2, 'cmd.restart', \%Core::Cmd::HELP_RESTART, \&Core::Cmd::cmd_restart); API::Std::cmd_add('REHASH', 2, 'cmd.rehash', \%Core::Cmd::HELP_REHASH, \&Core::Cmd::cmd_rehash); API::Std::cmd_add('HELP', 2, 0, \%Core::Cmd::HELP_HELP, \&Core::Cmd::cmd_help); -# Core events. -API::Std::event_add('on_shutdown'); - # Infinite while loop. while (1) { @@ -481,9 +478,7 @@ while (1) { delete $SOCKET{$sockid}; if (!keys %SOCKET) { # No more connections, stop the program. - $Auto::DB->disconnect; API::Std::event_run('on_shutdown'); - if (-e "$Bin/auto.pid") { unlink "$Bin/auto.pid"; } dbug '* No more IRC connections, shutting down.'; alog '* No more IRC connections, shutting down.'; sleep 1; diff --git a/doc/CHANGELOG b/doc/CHANGELOG index bb2e514..6618054 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -3,6 +3,7 @@ Auto IRC Bot 3.0: Change Log 3.0 Indev =============================================================================== + * Clean up shutdown. * Created event on_shutdown. * Shutdown if there are no more IRC connections. * Added the ability to use printf(3) % variables in trans(). diff --git a/lib/Core/Cmd.pm b/lib/Core/Cmd.pm index 0875579..25c4ccf 100644 --- a/lib/Core/Cmd.pm +++ b/lib/Core/Cmd.pm @@ -142,8 +142,7 @@ sub cmd_shutdown dbug "Got SHUTDOWN from ".$data{nick}."!".$data{user}."@".$data{host}."/".$data{svr}."! Shutting down. . ."; alog "Got SHUTDOWN from ".$data{nick}."!".$data{user}."@".$data{host}."/".$data{svr}."! Shutting down. . ."; quit($_, "SHUTDOWN from ".$data{nick}."/".$data{svr}) foreach (keys %Auto::SOCKET); - $Auto::DB->disconnect; - system("rm $Auto::Bin/auto.pid"); + API::Std::event_run('on_shutdown'); exit; # To appease PerlCritic. @@ -164,8 +163,7 @@ sub cmd_restart dbug "Got RESTART from ".$data{nick}."!".$data{user}."@".$data{host}."/".$data{svr}."! Restarting. . ."; alog "Got RESTART from ".$data{nick}."!".$data{user}."@".$data{host}."/".$data{svr}."! Restarting. . ."; quit($_, "RESTART from ".$data{nick}."/".$data{svr}) foreach (keys %Auto::SOCKET); - $Auto::DB->disconnect; - system("rm $Auto::Bin/auto.pid") if -e "$Auto::Bin/auto.pid"; + API::Std::event_run('on_shutdown'); # Time to come back from the dead! if ($Auto::DEBUG) { diff --git a/lib/Lib/Auto.pm b/lib/Lib/Auto.pm index ed702f3..e63e024 100644 --- a/lib/Lib/Auto.pm +++ b/lib/Lib/Auto.pm @@ -7,10 +7,13 @@ use warnings; use English qw(-no_match_vars); use Sys::Hostname; use feature qw(switch); -use API::Std qw(conf_get err); +use API::Std qw(hook_add conf_get err); use API::Log qw(println dbug alog); our $VERSION = 3.000000; +# Core events. +API::Std::event_add('on_shutdown'); + # Update checker. sub checkver { @@ -203,6 +206,13 @@ sub rehash return 1; } +# Shutdown. +hook_add('on_shutdown', 'shutdown.core_cleanup', sub { + if (defined $Auto::DB) { $Auto::DB->disconnect; } + if (-e "$Auto::Bin/auto.pid") { unlink "$Auto::Bin/auto.pid"; } + return 1; +}); + ################### # Signal handlers # ################### @@ -211,13 +221,10 @@ sub rehash sub signal_term { API::Std::event_run('on_sigterm'); + API::Std::event_run('on_shutdown'); foreach (keys %Auto::SOCKET) { API::IRC::quit($_, 'Caught SIGTERM'); } - $Auto::DB->disconnect; dbug '!!! Caught SIGTERM; terminating...'; alog '!!! Caught SIGTERM; terminating...'; - if (-e "$Auto::Bin/auto.pid") { - unlink "$Auto::Bin/auto.pid"; - } sleep 1; exit; } @@ -226,13 +233,10 @@ sub signal_term sub signal_int { API::Std::event_run('on_sigint'); + API::Std::event_run('on_shutdown'); foreach (keys %Auto::SOCKET) { API::IRC::quit($_, 'Caught SIGINT'); } - $Auto::DB->disconnect; dbug '!!! Caught SIGINT; terminating...'; alog '!!! Caught SIGINT; terminating...'; - if (-e "$Auto::Bin/auto.pid") { - unlink "$Auto::Bin/auto.pid"; - } sleep 1; exit; } @@ -266,10 +270,7 @@ sub signal_perldie return if $EXCEPTIONS_BEING_CAUGHT; alog 'Perl Fatal: '.$diemsg.' -- Terminating program!'; foreach (keys %Auto::SOCKET) { API::IRC::quit($_, 'A fatal error occurred!'); } - $Auto::DB->disconnect; - if (-e "$Auto::Bin/auto.pid") { - unlink "$Auto::Bin/auto.pid"; - } + API::Std::event_run('on_shutdown'); sleep 1; println 'FATAL: '.$diemsg; exit;