3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-02 01:18:40 +02:00

DualIndexHashObject can now enqueue saves to prevent repeated-save thrashing

This commit is contained in:
Pragmatic Software 2020-05-14 16:57:34 -07:00
parent a91c2cd71e
commit 506ebf13c6
2 changed files with 34 additions and 12 deletions

View File

@ -36,6 +36,7 @@ sub initialize {
my ($self, %conf) = @_;
$self->{name} = $conf{name} // 'Dual Index hash object';
$self->{filename} = $conf{filename} // Carp::carp("Missing filename to DualIndexHashObject, will not be able to save to or load from file.");
$self->{save_queue_timeout} = $conf{save_queue_timeout} // 0;
$self->{hash} = {};
}
@ -107,6 +108,7 @@ sub save {
return;
}
my $subref = sub {
$self->{pbot}->{logger}->log("Saving $self->{name} to $filename\n");
if (not $self->get_data('$metadata$', '$metadata$', 'update_version')) {
@ -121,6 +123,19 @@ sub save {
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
print FILE "$json_text\n";
close FILE;
};
if ($self->{save_queue_timeout}) {
# enqueue the save to prevent save-thrashing
$self->{pbot}->{timer}->replace_subref_or_enqueue_event(
$subref,
$self->{save_queue_timeout},
"save $self->{name}",
);
} else {
# execute it right now
$subref->();
}
}
sub clear {

View File

@ -258,6 +258,13 @@ sub initialize {
# give botowner all capabilities
$self->{capabilities}->rebuild_botowner_capabilities();
# flush all pending save events to disk at exit
$self->{atexit}->register(sub {
$self->{pbot}->{timer}->execute_and_dequeue_event('save *');
return;
}
);
}
sub random_nick {