From: Thorsten Date: Sun, 9 Jul 2023 10:37:41 +0000 (+0200) Subject: three years later, fix the event list X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=2f76544b6669c79a87ba218da46bfc099180e76f;p=urlbot-v3.git three years later, fix the event list --- diff --git a/distbot/bot/action_worker.py b/distbot/bot/action_worker.py index e93be1f..b1f9571 100644 --- a/distbot/bot/action_worker.py +++ b/distbot/bot/action_worker.py @@ -2,8 +2,9 @@ import asyncio import json import logging -import sched import time +from asyncio import TimerHandle +from typing import List import pika from pika.channel import Channel @@ -18,7 +19,7 @@ logger = logging.getLogger(__name__) class ActionWorker(object): exchange = '' - event_list = sched.scheduler(time.time, time.sleep) + event_list: List[TimerHandle] = [] @staticmethod def parse_body(body): @@ -87,8 +88,8 @@ class ActionWorker(object): def find_scheduled_action_by_mutex(self, mutex): action_item = None - for item in self.event_list.queue: - if item.kwargs["action"].mutex == mutex: + for item in self.event_list: + if item._args and item._args[0].mutex == mutex: action_item = item break return action_item @@ -103,7 +104,8 @@ class ActionWorker(object): logger.info("not scheduling that event (prevented by mutex)") raise RuntimeError("not scheduling that event (prevented by mutex)") - self.loop.call_later(event.time - time.time(), lambda: send_action(self._queue_name, event)) + handle: TimerHandle = self.loop.call_later(event.time - time.time(), lambda ev: send_action(self._queue_name, ev), event) + self.event_list.append(handle) @staticmethod async def tick(): @@ -120,7 +122,7 @@ class ActionWorker(object): """ item = self.find_scheduled_action_by_mutex(event.mutex) if item: - self.event_list.cancel(item) + item.cancel() def run_action(self, action: Action): logger.debug("Executing action %s", action) diff --git a/distbot/plugins/basic.py b/distbot/plugins/basic.py index 5b4bce4..4309dd7 100644 --- a/distbot/plugins/basic.py +++ b/distbot/plugins/basic.py @@ -342,7 +342,7 @@ class Magic8Ball(Worker): class TeaTimer(Worker): - binding_keys = ["nick.teatimer.*", "nick.teatimer"] + binding_keys = ["nick.teatimer.*", "nick.teatimer", "nick.cancel.teatimer"] description = 'sets a tea timer to $1 or currently %d seconds' % conf_get('plugins.teatimer.steep_time') usage = "bot: teatimer " @@ -351,7 +351,12 @@ class TeaTimer(Worker): words = get_words(msg) steep = config.conf_get('plugins.teatimer.steep_time') - if words[1:]: + if words == ["cancel", "teatimer"]: + return Action( + msg="Cancelled teatimer", + event=Action(stop_event=True, mutex=f"teatimer_{sender}") + ) + elif words[1:]: try: steep = int(words[1]) except ValueError as e: @@ -364,6 +369,7 @@ class TeaTimer(Worker): except (ValueError, OverflowError) as e: return Action(msg=': time format error: ' + str(e)) + return Action( msg=sender + ': Tea timer set to %s' % time.strftime('%Y-%m-%d %H:%M', time.localtime(ready)), event=Action(time=ready, msg=f'{sender}: Your tea is ready!', mutex=f'teatimer_{sender}')