aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-06-20 14:02:23 -0700
committerGravatar MarkKoz <[email protected]>2020-06-20 14:11:59 -0700
commitdfcf71f36c85e357028ea2f86aac7e38c6b8ab47 (patch)
treeab784258d58c49b8885938bbc6898765c748fe45
parentScheduler: add a method to schedule with a delay (diff)
Scheduler: add a method to schedule at a specific datetime
-rw-r--r--bot/utils/scheduling.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py
index ac67278f6..f5308059a 100644
--- a/bot/utils/scheduling.py
+++ b/bot/utils/scheduling.py
@@ -2,6 +2,7 @@ import asyncio
import contextlib
import logging
import typing as t
+from datetime import datetime
from functools import partial
@@ -32,6 +33,18 @@ class Scheduler:
self._scheduled_tasks[task_id] = task
self._log.debug(f"Scheduled task #{task_id} {id(task)}.")
+ def schedule_at(self, time: datetime, task_id: t.Hashable, coroutine: t.Coroutine) -> None:
+ """
+ Schedule `coroutine` to be executed at the given naïve UTC `time`.
+
+ If `time` is in the past, schedule `coroutine` immediately.
+ """
+ delay = (time - datetime.utcnow()).total_seconds()
+ if delay > 0:
+ coroutine = self._await_later(delay, coroutine)
+
+ self.schedule(task_id, coroutine)
+
def schedule_later(self, delay: t.Union[int, float], task_id: t.Hashable, coroutine: t.Coroutine) -> None:
"""Schedule `coroutine` to be executed after the given `delay` number of seconds."""
self.schedule(task_id, self._await_later(delay, coroutine))