Skip to content

Commit a6ec7f3

Browse files
zevaryxeightweenpre-commit-ci[bot]
authored
feat: add new CronTrigger task type (#1632)
* [feat] Add CronTrigger * [feat] Add timezone support to CronTrigger * docs: utc -> tz rename Signed-off-by: 18 <111544899+xvi-ii@users.noreply.github.com> * ci: correct from checks. * docs: specify tz as datetime attribute I'm aware that datetime is not what it actually is, but we shouldn't be using private data types in our docstrings. Developers will additionally be using it as a reference, not the literal representation when using Signed-off-by: 18 <111544899+xvi-ii@users.noreply.github.com> --------- Signed-off-by: 18 <111544899+xvi-ii@users.noreply.github.com> Co-authored-by: 18 <111544899+xvi-ii@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ced71df commit a6ec7f3

File tree

3 files changed

+2532
-25
lines changed

3 files changed

+2532
-25
lines changed

interactions/models/internal/tasks/triggers.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from abc import ABC, abstractmethod
2-
from datetime import datetime, timedelta, timezone
2+
from datetime import datetime, timedelta, timezone, _TzInfo
3+
4+
from croniter import croniter
35

46
__all__ = ("BaseTrigger", "IntervalTrigger", "DateTrigger", "TimeTrigger", "OrTrigger")
57

@@ -140,3 +142,21 @@ def _set_current_trigger(self) -> BaseTrigger | None:
140142

141143
def next_fire(self) -> datetime | None:
142144
return self.current_trigger.next_fire() if self._set_current_trigger() else None
145+
146+
147+
class CronTrigger(BaseTrigger):
148+
"""
149+
Trigger the task based on a cron schedule.
150+
151+
Attributes:
152+
cron str: The cron schedule, use https://crontab.guru for help
153+
tz datetime: Whether or not to use UTC for the time (uses timezone information)
154+
155+
"""
156+
157+
def __init__(self, cron: str, tz: _TzInfo = timezone.utc) -> None:
158+
self.cron = cron
159+
self.tz = tz
160+
161+
def next_fire(self) -> datetime | None:
162+
return croniter(self.cron, datetime.now(tz=self.tz)).next(datetime)

0 commit comments

Comments
 (0)