From defad20bfd12c6fd5ac704e41a61cb0159d50a06 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Fri, 6 Aug 2021 12:59:21 -0700 Subject: [PATCH] Refactor and simplify atexit routines --- lib/PBot/Core.pm | 1 - lib/PBot/Core/Logger.pm | 2 +- lib/PBot/Core/MessageHistory.pm | 2 +- lib/PBot/Core/MessageHistory/Storage/SQLite.pm | 7 ++++--- lib/PBot/Core/Registerable.pm | 6 ++---- lib/PBot/Core/Registry.pm | 2 +- lib/PBot/Core/Storage/DualIndexSQLiteObject.pm | 1 + lib/PBot/VERSION.pm | 2 +- 8 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/PBot/Core.pm b/lib/PBot/Core.pm index 32191a4b..c9c1444c 100644 --- a/lib/PBot/Core.pm +++ b/lib/PBot/Core.pm @@ -216,7 +216,6 @@ sub initialize { # fire all pending save events at exit $self->{atexit}->register(sub { $self->{event_queue}->execute_and_dequeue_event('save .*'); - return; } ); diff --git a/lib/PBot/Core/Logger.pm b/lib/PBot/Core/Logger.pm index c2e9b48c..52078dc8 100644 --- a/lib/PBot/Core/Logger.pm +++ b/lib/PBot/Core/Logger.pm @@ -49,7 +49,7 @@ sub initialize { LOGFILE->autoflush(1); # rename logfile to start-time at exit - $self->{pbot}->{atexit}->register(sub { $self->rotate_log; return; }); + $self->{pbot}->{atexit}->register(sub { $self->rotate_log }); } sub log { diff --git a/lib/PBot/Core/MessageHistory.pm b/lib/PBot/Core/MessageHistory.pm index c8ce4e6b..60c190da 100644 --- a/lib/PBot/Core/MessageHistory.pm +++ b/lib/PBot/Core/MessageHistory.pm @@ -32,7 +32,7 @@ sub initialize { $self->{pbot}->{registry}->add_default('text', 'messagehistory', 'max_recall_time', $conf{max_recall_time} // 0); - $self->{pbot}->{atexit}->register(sub { $self->{database}->end(); return; }); + $self->{pbot}->{atexit}->register(sub { $self->{database}->end }); } sub get_message_account { diff --git a/lib/PBot/Core/MessageHistory/Storage/SQLite.pm b/lib/PBot/Core/MessageHistory/Storage/SQLite.pm index 47e81cd3..39dd0961 100644 --- a/lib/PBot/Core/MessageHistory/Storage/SQLite.pm +++ b/lib/PBot/Core/MessageHistory/Storage/SQLite.pm @@ -28,6 +28,7 @@ use Time::Duration; sub initialize { my ($self, %conf) = @_; + $self->{filename} = $conf{filename} // $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/message_history.sqlite3'; $self->{new_entries} = 0; @@ -178,8 +179,8 @@ sub end { $self->{pbot}->{logger}->log("Closing message history SQLite database\n"); if (exists $self->{dbh} and defined $self->{dbh}) { - $self->{dbh}->commit() if $self->{new_entries}; - $self->{dbh}->disconnect(); + $self->{dbh}->commit; + $self->{dbh}->disconnect; close $self->{trace_layer} if $self->{trace_layer}; delete $self->{dbh}; } @@ -1925,7 +1926,7 @@ sub get_message_account_id { } sub commit_message_history { - my $self = shift; + my ($self) = @_; return if not $self->{dbh}; return if $self->{pbot}->{child}; # don't commit() as child of fork() diff --git a/lib/PBot/Core/Registerable.pm b/lib/PBot/Core/Registerable.pm index 62ccc3c2..a791feff 100644 --- a/lib/PBot/Core/Registerable.pm +++ b/lib/PBot/Core/Registerable.pm @@ -26,10 +26,8 @@ sub initialize { sub execute_all { my $self = shift; foreach my $func (@{$self->{handlers}}) { - my $result = &{$func->{subref}}(@_); - return $result if defined $result; + $func->{subref}->(@_); } - return undef; } sub execute { @@ -37,7 +35,7 @@ sub execute { my $ref = shift; Carp::croak("Missing reference parameter to Registerable::execute") if not defined $ref; foreach my $func (@{$self->{handlers}}) { - if ($ref == $func || $ref == $func->{subref}) { return &{$func->{subref}}(@_); } + if ($ref == $func || $ref == $func->{subref}) { return $func->{subref}->(@_); } } return undef; } diff --git a/lib/PBot/Core/Registry.pm b/lib/PBot/Core/Registry.pm index d6b9f48f..949120a2 100644 --- a/lib/PBot/Core/Registry.pm +++ b/lib/PBot/Core/Registry.pm @@ -28,7 +28,7 @@ sub initialize { $self->{triggers} = {}; # save registry data at bot exit - $self->{pbot}->{atexit}->register(sub { $self->save; return; }); + $self->{pbot}->{atexit}->register(sub { $self->save }); # load existing registry entries from file (if exists) if (-e $filename) { diff --git a/lib/PBot/Core/Storage/DualIndexSQLiteObject.pm b/lib/PBot/Core/Storage/DualIndexSQLiteObject.pm index bce2a6ec..575815c2 100644 --- a/lib/PBot/Core/Storage/DualIndexSQLiteObject.pm +++ b/lib/PBot/Core/Storage/DualIndexSQLiteObject.pm @@ -86,6 +86,7 @@ sub end { $self->{pbot}->{logger}->log("Closing $self->{name} database ($self->{filename})\n"); if (defined $self->{dbh}) { + $self->{dbh}->commit; $self->{dbh}->disconnect; close $self->{trace_layer}; $self->{dbh} = undef; diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index ba6f2b6f..73e2a4b2 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,7 +25,7 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4348, + BUILD_REVISION => 4349, BUILD_DATE => "2021-08-06", };