mirror of
				https://github.com/pragma-/pbot.git
				synced 2025-11-04 08:37:24 +01:00 
			
		
		
		
	Refactor and simplify atexit routines
This commit is contained in:
		
							parent
							
								
									3fc39c9359
								
							
						
					
					
						commit
						defad20bfd
					
				@ -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;
 | 
			
		||||
        }
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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 {
 | 
			
		||||
 | 
			
		||||
@ -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 {
 | 
			
		||||
 | 
			
		||||
@ -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()
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -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",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user