mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-26 22:09:26 +01:00
Add registerable atexit functionality and trap SIGINT
This commit is contained in:
parent
b1cb9fa200
commit
d8d26b1cea
@ -163,13 +163,7 @@ sub ack_die {
|
||||
my $self = shift;
|
||||
my ($from, $nick, $user, $host, $arguments) = @_;
|
||||
$self->{pbot}->logger->log("$nick!$user\@$host made me exit.\n");
|
||||
|
||||
# TODO: move all of those to an registerable atexit handler
|
||||
$self->{pbot}->factoids->save_factoids;
|
||||
$self->{pbot}->ignorelist->save_ignores;
|
||||
$self->{pbot}->{quotegrabs}->{database}->end();
|
||||
$self->{pbot}->{messagehistory}->{database}->end();
|
||||
|
||||
$self->{pbot}->atexit();
|
||||
$self->{pbot}->conn->privmsg($from, "Good-bye.") if defined $from;
|
||||
$self->{pbot}->conn->quit("Departure requested.");
|
||||
exit 0;
|
||||
|
@ -46,6 +46,8 @@ sub initialize {
|
||||
|
||||
$self->{pbot} = $pbot;
|
||||
$self->{factoidmodulelauncher} = PBot::FactoidModuleLauncher->new(pbot => $pbot);
|
||||
|
||||
$self->{pbot}->{atexit}->register(sub { $self->save_factoids; return; });
|
||||
}
|
||||
|
||||
sub load_factoids {
|
||||
|
@ -45,6 +45,8 @@ sub initialize {
|
||||
$self->{MSG_NICKCHANGE} = 3; # CHANGED NICK
|
||||
|
||||
$self->{pbot}->commands->register(sub { $self->recall_message(@_) }, "recall", 0);
|
||||
|
||||
$self->{pbot}->{atexit}->register(sub { $self->{database}->end(); return; });
|
||||
}
|
||||
|
||||
sub get_message_account {
|
||||
|
14
PBot/PBot.pm
14
PBot/PBot.pm
@ -60,9 +60,9 @@ sub new {
|
||||
}
|
||||
|
||||
my ($class, %conf) = @_;
|
||||
|
||||
my $self = bless {}, $class;
|
||||
$self->initialize(%conf);
|
||||
$self->register_signal_handlers;
|
||||
return $self;
|
||||
}
|
||||
|
||||
@ -108,6 +108,8 @@ sub initialize {
|
||||
$self->{commands} = PBot::Commands->new(pbot => $self);
|
||||
$self->{timer} = PBot::Timer->new(timeout => 10);
|
||||
|
||||
$self->{atexit} = PBot::Registerable->new();
|
||||
|
||||
$self->{select_handler} = PBot::SelectHandler->new(pbot => $self);
|
||||
$self->{stdin_reader} = PBot::StdinReader->new(pbot => $self);
|
||||
|
||||
@ -235,6 +237,16 @@ sub start {
|
||||
}
|
||||
}
|
||||
|
||||
sub register_signal_handlers {
|
||||
my $self = shift;
|
||||
$SIG{INT} = sub { $self->atexit; exit 0; };
|
||||
}
|
||||
|
||||
sub atexit {
|
||||
my $self = shift;
|
||||
$self->{atexit}->execute_all;
|
||||
}
|
||||
|
||||
#-----------------------------------------------------------------------------------
|
||||
# Getters/Setters
|
||||
#-----------------------------------------------------------------------------------
|
||||
|
@ -45,6 +45,8 @@ sub initialize {
|
||||
#$self->{database} = PBot::Quotegrabs_Hashtable->new(pbot => $self->{pbot}, filename => $self->{filename});
|
||||
$self->{database}->begin();
|
||||
|
||||
$self->{pbot}->{atexit}->register(sub { $self->{database}->end(); return; });
|
||||
|
||||
#-------------------------------------------------------------------------------------
|
||||
# The following could be in QuotegrabsCommands.pm, or they could be kept in here?
|
||||
#-------------------------------------------------------------------------------------
|
||||
|
@ -19,7 +19,6 @@ sub new {
|
||||
}
|
||||
|
||||
my ($class, %conf) = @_;
|
||||
|
||||
my $self = bless {}, $class;
|
||||
$self->initialize(%conf);
|
||||
return $self;
|
||||
@ -27,7 +26,6 @@ sub new {
|
||||
|
||||
sub initialize {
|
||||
my $self = shift;
|
||||
|
||||
$self->{handlers} = [];
|
||||
}
|
||||
|
||||
@ -67,7 +65,6 @@ sub register {
|
||||
Carp::croak("Must pass subroutine reference to register()");
|
||||
}
|
||||
|
||||
# TODO: Check if subref already exists in handlers?
|
||||
my $ref = { subref => $subref };
|
||||
push @{ $self->{handlers} }, $ref;
|
||||
|
||||
|
@ -13,7 +13,7 @@ use warnings;
|
||||
# These are set automatically by the build/commit script
|
||||
use constant {
|
||||
BUILD_NAME => "PBot",
|
||||
BUILD_REVISION => 580,
|
||||
BUILD_REVISION => 581,
|
||||
BUILD_DATE => "2014-05-16",
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user