2021-06-22 02:26:24 +02:00
|
|
|
# File: EventQueue.pm
|
2010-03-22 08:33:44 +01:00
|
|
|
#
|
2021-06-22 02:26:24 +02:00
|
|
|
# Purpose: Provides functionality to manage event subroutines which are invoked
|
2020-03-06 22:21:44 +01:00
|
|
|
# at a future time, optionally recurring.
|
2010-03-22 08:33:44 +01:00
|
|
|
#
|
2021-07-21 07:44:51 +02:00
|
|
|
# Note: PBot::Core::EventQueue has no relation to PBot::Core::EventDispatcher.
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
package PBot::Core::EventQueue;
|
|
|
|
use parent 'PBot::Core::Class';
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2021-07-24 04:22:25 +02:00
|
|
|
use PBot::Core::Utils::PriorityQueue;
|
2021-07-19 02:58:48 +02:00
|
|
|
|
2021-06-22 02:26:24 +02:00
|
|
|
use Time::HiRes qw/time/;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-07-24 04:22:25 +02:00
|
|
|
$self->{event_queue} = PBot::Core::Utils::PriorityQueue->new(pbot => $self->{pbot});
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# returns seconds until upcoming event.
|
|
|
|
sub duration_until_next_event {
|
|
|
|
my ($self) = @_;
|
2021-07-19 02:58:48 +02:00
|
|
|
return 0 if not $self->{event_queue}->count;
|
|
|
|
return $self->{event_queue}->get_priority(0) - time;
|
2021-06-22 23:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# invokes any current events and then returns seconds until upcoming event.
|
|
|
|
sub do_events {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
# early-return if no events available
|
2021-07-19 02:58:48 +02:00
|
|
|
return 0 if not $self->{event_queue}->count;
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
my $debug = $self->{pbot}->{registry}->get_value('eventqueue', 'debug') // 0;
|
|
|
|
|
|
|
|
# repeating events to re-enqueue
|
|
|
|
my @enqueue;
|
|
|
|
|
2021-07-19 02:58:48 +02:00
|
|
|
for (my $i = 0; $i < $self->{event_queue}->entries; $i++) {
|
2021-06-22 23:38:30 +02:00
|
|
|
# we call time for a fresh time, instead of using a stale $now that
|
2021-07-09 23:39:35 +02:00
|
|
|
# could be in the past depending on a previous event's duration
|
2021-07-19 02:58:48 +02:00
|
|
|
if (time >= $self->{event_queue}->get_priority($i)) {
|
2021-07-19 03:14:28 +02:00
|
|
|
my $event = $self->{event_queue}->get($i);
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("Processing event $i: $event->{id}\n") if $debug > 1;
|
|
|
|
|
|
|
|
# call event's subref, passing event as argument
|
|
|
|
$event->{subref}->($event);
|
|
|
|
|
|
|
|
# remove event from queue
|
2021-07-19 02:58:48 +02:00
|
|
|
$self->{event_queue}->remove($i--);
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
# add event to re-enqueue queue if repeating
|
|
|
|
push @enqueue, $event if $event->{repeating};
|
|
|
|
} else {
|
|
|
|
# no more events ready at this time
|
|
|
|
if ($debug > 2) {
|
2021-07-19 03:14:28 +02:00
|
|
|
my $event = $self->{event_queue}->get($i);
|
2021-07-19 02:58:48 +02:00
|
|
|
$self->{pbot}->{logger}->log("Event not ready yet: $event->{id} (timeout=$event->{priority})\n");
|
2021-06-22 23:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# re-enqueue repeating events
|
|
|
|
foreach my $event (@enqueue) {
|
|
|
|
$self->enqueue_event($event->{subref}, $event->{interval}, $event->{id}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->duration_until_next_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if an event is in the event queue.
|
|
|
|
sub exists {
|
|
|
|
my ($self, $id) = @_;
|
2021-07-19 02:58:48 +02:00
|
|
|
return scalar grep { $_->{id} eq $id } $self->{event_queue}->entries;
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# adds an event to the event queue, optionally repeating
|
2020-03-06 22:21:44 +01:00
|
|
|
sub enqueue_event {
|
2021-06-22 02:26:24 +02:00
|
|
|
my ($self, $subref, $interval, $id, $repeating) = @_;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# default values
|
|
|
|
$id //= "unnamed (${interval}s $subref)";
|
2021-06-21 00:10:16 +02:00
|
|
|
$repeating //= 0;
|
|
|
|
$interval //= 0;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# create event structure
|
2020-03-06 22:21:44 +01:00
|
|
|
my $event = {
|
|
|
|
id => $id,
|
2021-06-22 02:26:24 +02:00
|
|
|
subref => $subref,
|
2020-03-06 22:21:44 +01:00
|
|
|
interval => $interval,
|
2021-07-19 02:58:48 +02:00
|
|
|
priority => time + $interval,
|
2020-03-06 22:21:44 +01:00
|
|
|
repeating => $repeating,
|
|
|
|
};
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-07-19 02:58:48 +02:00
|
|
|
# add the event to the priority queue
|
|
|
|
my $position = $self->{event_queue}->add($event);
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# debugging noise
|
2021-06-22 02:26:24 +02:00
|
|
|
my $debug = $self->{pbot}->{registry}->get_value('eventqueue', 'debug') // 0;
|
2020-03-06 22:21:44 +01:00
|
|
|
if ($debug > 1) {
|
2021-07-19 02:58:48 +02:00
|
|
|
$self->{pbot}->{logger}->log("Enqueued new event $id at position $position: timeout=$event->{priority} interval=$interval repeating=$repeating\n");
|
2020-03-06 22:21:44 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# convenient alias to add an event with repeating defaulted to enabled.
|
|
|
|
sub enqueue {
|
|
|
|
my ($self, $subref, $interval, $id, $repeating) = @_;
|
|
|
|
$self->enqueue_event($subref, $interval, $id, $repeating // 1);
|
|
|
|
}
|
|
|
|
|
2021-06-22 23:02:51 +02:00
|
|
|
# removes an event from the event queue, optionally invoking it.
|
|
|
|
# `id` can contain `.*` and `.*?` for wildcard-matching/globbing.
|
2020-03-06 22:21:44 +01:00
|
|
|
sub dequeue_event {
|
2020-05-15 01:54:10 +02:00
|
|
|
my ($self, $id, $execute) = @_;
|
2020-03-06 22:21:44 +01:00
|
|
|
|
2020-03-09 00:05:37 +01:00
|
|
|
my $result = eval {
|
2021-06-22 23:38:30 +02:00
|
|
|
# escape special characters
|
2020-03-06 22:21:44 +01:00
|
|
|
$id = quotemeta $id;
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
# unescape .* and .*?
|
2020-03-06 22:21:44 +01:00
|
|
|
$id =~ s/\\\.\\\*\\\?/.*?/g;
|
|
|
|
$id =~ s/\\\.\\\*/.*/g;
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
# compile regex
|
2020-04-06 05:33:14 +02:00
|
|
|
my $regex = qr/^$id$/i;
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
# count total events before removal
|
2021-07-19 02:58:48 +02:00
|
|
|
my $count = $self->{event_queue}->count;
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
# collect events to be removed
|
2021-07-19 02:58:48 +02:00
|
|
|
my @removed = grep { $_->{id} =~ /$regex/i; } $self->{event_queue}->entries;
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
# remove events from event queue
|
2021-07-19 02:58:48 +02:00
|
|
|
@{$self->{event_queue}->queue} = grep { $_->{id} !~ /$regex/i; } $self->{event_queue}->entries;
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
# set count to total events removed
|
2021-07-19 02:58:48 +02:00
|
|
|
$count -= $self->{event_queue}->count;
|
2020-05-15 01:54:10 +02:00
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# invoke removed events, if requested
|
2020-05-15 01:54:10 +02:00
|
|
|
if ($execute) {
|
|
|
|
foreach my $event (@removed) {
|
|
|
|
$event->{subref}->($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# nothing removed
|
2020-04-25 02:28:27 +02:00
|
|
|
return "No matching events." if not $count;
|
2021-06-22 23:38:30 +02:00
|
|
|
|
2021-07-09 23:39:35 +02:00
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# list all removed events
|
2021-07-09 23:39:35 +02:00
|
|
|
my $removed = "Removed $count event" . ($count == 1 ? '' : 's') . ': ' . join(', ', map { $_->{id} } @removed);
|
|
|
|
$self->{pbot}->{logger}->log("EventQueue: dequeued $removed\n");
|
|
|
|
return $removed;
|
2020-03-06 22:21:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
2020-03-09 00:05:37 +01:00
|
|
|
my $error = $@;
|
|
|
|
$self->{pbot}->{logger}->log("Error in dequeue_event: $error\n");
|
|
|
|
$error =~ s/ at PBot.*//;
|
|
|
|
return "$error";
|
2020-03-06 22:21:44 +01:00
|
|
|
}
|
2020-03-09 00:05:37 +01:00
|
|
|
|
|
|
|
return $result;
|
2020-03-06 22:21:44 +01:00
|
|
|
}
|
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# alias to dequeue_event, for consistency.
|
|
|
|
sub dequeue {
|
|
|
|
my ($self, $id) = @_;
|
|
|
|
$self->dequeue_event($id);
|
|
|
|
}
|
|
|
|
|
2021-06-22 23:02:51 +02:00
|
|
|
# invoke and remove all events matching `id`, which can
|
2021-06-22 23:38:30 +02:00
|
|
|
# contain `.*` and `.*?` for wildcard-matching/globbing.
|
2020-05-15 01:54:10 +02:00
|
|
|
sub execute_and_dequeue_event {
|
|
|
|
my ($self, $id) = @_;
|
|
|
|
return $self->dequeue_event($id, 1);
|
|
|
|
}
|
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# replace code subrefs for matching events. if no events
|
|
|
|
# were found, then add the event to the event queue.
|
|
|
|
sub replace_subref_or_enqueue_event {
|
2021-06-22 02:26:24 +02:00
|
|
|
my ($self, $subref, $interval, $id, $repeating) = @_;
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
# find events matching id
|
2021-07-19 02:58:48 +02:00
|
|
|
my @events = grep { $_->{id} eq $id } $self->{event_queue}->entries;
|
2021-06-22 23:38:30 +02:00
|
|
|
|
|
|
|
# no events found, enqueue new event
|
|
|
|
if (not @events) {
|
|
|
|
$self->enqueue_event($subref, $interval, $id, $repeating);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
# otherwise update existing events
|
|
|
|
foreach my $event (@events) {
|
|
|
|
$event->{subref} = $subref;
|
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# remove existing events of this id then enqueue new event.
|
|
|
|
sub replace_or_enqueue_event {
|
|
|
|
my ($self, $subref, $interval, $id, $repeating) = @_;
|
|
|
|
|
|
|
|
# remove event if it exists
|
|
|
|
$self->dequeue_event($id) if $self->exists($id);
|
|
|
|
|
|
|
|
# enqueue new event
|
|
|
|
$self->enqueue_event($subref, $interval, $id, $repeating);
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# add event unless it already had been added.
|
|
|
|
sub enqueue_event_unless_exists {
|
|
|
|
my ($self, $subref, $interval, $id, $repeating) = @_;
|
|
|
|
|
|
|
|
# event already exists, bail out
|
|
|
|
return if $self->exists($id);
|
|
|
|
|
|
|
|
# enqueue new event
|
|
|
|
$self->enqueue_event($subref, $interval, $id, $repeating);
|
2020-05-15 01:54:10 +02:00
|
|
|
}
|
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# update the `repeating` flag for all events matching `id`.
|
2020-03-07 05:23:17 +01:00
|
|
|
sub update_repeating {
|
|
|
|
my ($self, $id, $repeating) = @_;
|
|
|
|
|
2021-07-19 02:58:48 +02:00
|
|
|
foreach my $event ($self->{event_queue}->entries) {
|
|
|
|
if ($event->{id} eq $id) {
|
|
|
|
$event->{repeating} = $repeating;
|
2020-03-07 05:23:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 23:38:30 +02:00
|
|
|
# update the `interval` value for all events matching `id`.
|
2014-05-19 12:30:25 +02:00
|
|
|
sub update_interval {
|
2020-03-06 22:21:44 +01:00
|
|
|
my ($self, $id, $interval, $dont_enqueue) = @_;
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2021-07-19 02:58:48 +02:00
|
|
|
for (my $i = 0; $i < $self->{event_queue}->count; $i++) {
|
2021-07-19 03:14:28 +02:00
|
|
|
my $event = $self->{event_queue}->get($i);
|
2021-07-19 02:58:48 +02:00
|
|
|
|
|
|
|
if ($event->{id} eq $id) {
|
2020-03-06 22:21:44 +01:00
|
|
|
if ($dont_enqueue) {
|
2021-06-22 02:26:24 +02:00
|
|
|
# update interval in-place without moving event to new place in queue
|
|
|
|
# (allows event to fire at expected time, then updates to new timeout afterwards)
|
2021-07-19 02:58:48 +02:00
|
|
|
$event->{interval} = $interval;
|
2020-03-06 22:21:44 +01:00
|
|
|
} else {
|
2021-06-22 02:26:24 +02:00
|
|
|
# remove and add event in new position in queue
|
2021-07-19 02:58:48 +02:00
|
|
|
$self->{event_queue}->remove($i);
|
2020-03-06 22:21:44 +01:00
|
|
|
$self->enqueue_event($event->{subref}, $interval, $id, $event->{repeating});
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2014-05-19 12:30:25 +02:00
|
|
|
}
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
2021-07-21 21:43:30 +02:00
|
|
|
sub count {
|
|
|
|
my ($self) = @_;
|
|
|
|
return $self->{event_queue}->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub entries {
|
|
|
|
my ($self) = @_;
|
|
|
|
return $self->{event_queue}->entries;
|
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
1;
|