3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-27 13:12:42 +01:00

Timer can now update timeout interval by timer id; add timer interval to registry for LagChecker and MessageHistory_SQLite

This commit is contained in:
Pragmatic Software 2014-05-19 10:30:25 +00:00
parent 710bbb76cc
commit ac45cf8036
4 changed files with 46 additions and 33 deletions

View File

@ -34,11 +34,13 @@ sub initialize {
$self->{pbot} = $pbot;
# maximum number of lag history entries to retain
$pbot->{registry}->add_default('text', 'lagchecker', 'lag_history_max', $conf{lag_history_max} // 3);
$pbot->{registry}->add_default('text', 'lagchecker', 'lag_history_max', $conf{lag_history_max} // 3);
# lagging is true if lag_average reaches or exceeds this threshold, in seconds
$pbot->{registry}->add_default('text', 'lagchecker', 'lag_threshold', $conf{lag_threadhold} // 2);
$pbot->{registry}->add_default('text', 'lagchecker', 'lag_threshold', $conf{lag_threadhold} // 2);
# how often to send PING, in seconds
$pbot->{registry}->add_default('text', 'lagchecker', 'lag_history_interval', $conf{lag_history_max} // 10);
$pbot->{registry}->add_default('text', 'lagchecker', 'lag_history_interval', $conf{lag_history_interval} // 10);
$pbot->{registry}->add_trigger('lagchecker', 'lag_history_interval', sub { $self->lag_history_interval_trigger(@_) });
$self->{lag_average} = undef; # average of entries in lag history, in seconds
$self->{lag_string} = undef; # string representation of lag history and lag average
@ -46,11 +48,20 @@ sub initialize {
$self->{pong_received} = undef; # tracks pong replies; undef if no ping sent; 0 if ping sent but no pong reply yet; 1 if ping/pong completed
$self->{ping_send_time} = undef; # when last ping was sent
$pbot->{timer}->register(sub { $self->send_ping }, $pbot->{registry}->get_value('lagchecker', 'lag_history_interval'));
$pbot->{timer}->register(
sub { $self->send_ping },
$pbot->{registry}->get_value('lagchecker', 'lag_history_interval'),
'lag_history_interval'
);
$pbot->{commands}->register(sub { return $self->lagcheck(@_) }, "lagcheck", 0);
}
sub lag_history_interval_trigger {
my ($self, $section, $item, $newvalue) = @_;
$self->{pbot}->{timer}->update_interval('lag_history_interval', $newvalue);
}
sub send_ping {
my $self = shift;

View File

@ -28,9 +28,22 @@ sub initialize {
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference in " . __FILE__);
$self->{filename} = delete $conf{filename} // $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/message_history.sqlite3';
$self->{pbot}->{timer}->register(sub { $self->commit_message_history }, 5);
$self->{new_entries} = 0;
$self->{pbot}->{registry}->add_default('text', 'messagehistory', 'sqlite_commit_interval', 30);
$self->{pbot}->{registry}->add_trigger('messagehistory', 'sqlite_commit_interval', sub { $self->sqlite_commit_interval_trigger(@_) });
$self->{pbot}->{timer}->register(
sub { $self->commit_message_history },
$self->{pbot}->{registry}->get_value('messagehistory', 'sqlite_commit_interval'),
'messagehistory_sqlite_commit_interval'
);
}
sub sqlite_commit_interval_trigger {
my ($self, $section, $item, $newvalue) = @_;
$self->{pbot}->{timer}->update_interval('messagehistory_sqlite_commit_interval', $newvalue);
}
sub begin {

View File

@ -107,11 +107,11 @@ sub on_tick_handler {
} else {
# call default overridable handler if timeout has elapsed
if(defined $self->{last}) {
# print "$self->{name} last = $self->{last}, seconds: $seconds, timeout: " . $self->timeout . " " . ($seconds - $self->{last}) . "\n";
# print "$self->{name} last = $self->{last}, seconds: $seconds, timeout: $self->{timeout} " . ($seconds - $self->{last}) . "\n";
$self->{last} -= $max_seconds if $seconds < $self->{last}; # handle wrap-around
if($seconds - $self->{last} >= $self->timeout) {
if($seconds - $self->{last} >= $self->{timeout}) {
$elapsed = 1;
$self->{last} = $seconds;
}
@ -139,22 +139,19 @@ sub on_tick {
sub register {
my $self = shift;
my ($ref, $timeout);
my ($ref, $timeout, $id) = @_;
if(@_) {
($ref, $timeout) = @_;
} else {
Carp::croak("Must pass subroutine reference to register()");
}
Carp::croak("Must pass subroutine reference to register()") if not defined $ref;
# TODO: Check if subref already exists in handlers?
$timeout = 300 if not defined $timeout; # set default value of 5 minutes if not defined
$id = 'timer' if not defined $id;
my $h = { subref => $ref, timeout => $timeout };
my $h = { subref => $ref, timeout => $timeout, id => $id };
push @{ $self->{handlers} }, $h;
# print "-- Registering timer $ref at $timeout seconds\n";
# print "-- Registering timer $ref [$id] at $timeout seconds\n";
if($timeout < $min_timeout) {
$min_timeout = $timeout;
@ -180,23 +177,15 @@ sub unregister {
@{ $self->{handlers} } = grep { $_->{subref} != $ref } @{ $self->{handlers} };
}
sub max_seconds {
if(@_) { $max_seconds = shift; }
return $max_seconds;
}
sub update_interval {
my ($self, $id, $interval) = @_;
sub timeout {
my $self = shift;
if(@_) { $self->{timeout} = shift; }
return $self->{timeout};
}
sub name {
my $self = shift;
if(@_) { $self->{name} = shift; }
return $self->{name};
foreach my $h (@{ $self->{handlers} }) {
if($h->{id} eq $id) {
$h->{timeout} = $interval;
last;
}
}
}
1;

View File

@ -13,7 +13,7 @@ use warnings;
# These are set automatically by the build/commit script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 590,
BUILD_REVISION => 591,
BUILD_DATE => "2014-05-19",
};