]> git.aero2k.de Git - urlbot-v3.git/commitdiff
three years later, fix the event list
authorThorsten <mail@aero2k.de>
Sun, 9 Jul 2023 10:37:41 +0000 (12:37 +0200)
committerThorsten <mail@aero2k.de>
Sun, 9 Jul 2023 10:37:41 +0000 (12:37 +0200)
distbot/bot/action_worker.py
distbot/plugins/basic.py

index e93be1fc7a9d65a932b97bf858b2164ff0cbf6ab..b1f9571e22f4bc7f431ad2396b8e8fe324291cf0 100644 (file)
@@ -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)
index 5b4bce49dce2a3a203689f3238dcf337100e0290..4309dd788267445fb6f75a7d8a5e4394e9f74cbc 100644 (file)
@@ -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 <seconds>"
 
@@ -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}')