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
class ActionWorker(object):
exchange = ''
- event_list = sched.scheduler(time.time, time.sleep)
+ event_list: List[TimerHandle] = []
@staticmethod
def parse_body(body):
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
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():
"""
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)
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>"
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:
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}')