3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-29 23:39:24 +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; $self->{pbot} = $pbot;
# maximum number of lag history entries to retain # 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 # 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 # 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_average} = undef; # average of entries in lag history, in seconds
$self->{lag_string} = undef; # string representation of lag history and lag average $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->{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 $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); $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 { sub send_ping {
my $self = shift; my $self = shift;

View File

@ -28,9 +28,22 @@ sub initialize {
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference in " . __FILE__); $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->{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->{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 { sub begin {

View File

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

View File

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