Skip to content

Commit

Permalink
Fix #10 -- support day ranges (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
amureki committed Jul 4, 2023
1 parent 8a2eef0 commit 990329c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
23 changes: 6 additions & 17 deletions dramatiq_crontab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
__version__ = _version.version
VERSION = _version.version_tuple


__all__ = ["cron", "scheduler"]


scheduler = BlockingScheduler()


Expand All @@ -41,21 +39,12 @@ def cron_test():
"""

def decorator(actor):
*_, day_of_week = schedule.split(" ")

if day_of_week.lower() not in (
"*",
"mon",
"tue",
"wed",
"thu",
"fri",
"sat",
"sun",
):
# CronTrigger uses Python's timezone dependent first weekday,
# so in Berlin monday is 0 and sunday is 6. We use literals to avoid
# confusion. Literals are also more readable and crontab conform.
*_, day_schedule = schedule.split(" ")

# CronTrigger uses Python's timezone dependent first weekday,
# so in Berlin monday is 0 and sunday is 6. We use literals to avoid
# confusion. Literals are also more readable and crontab conform.
if any(i.isdigit() for i in day_schedule):
raise ValueError(
"Please use a literal day of week (Mon, Tue, Wed, Thu, Fri, Sat, Sun) or *"
)
Expand Down
38 changes: 35 additions & 3 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,38 @@ def test_cron__stars():
def test_cron__day_of_week():
assert not scheduler.remove_all_jobs()
assert tasks.cron("* * * * Mon")(tasks.heartbeat)
init = datetime.datetime(2021, 1, 1, 0, 0, 0)
init = datetime.datetime(2021, 1, 1, 0, 0, 0) # Friday
assert scheduler.get_jobs()[0].trigger.get_next_fire_time(
init, init
) == datetime.datetime(
2021, 1, 4, 0, 0, tzinfo=zoneinfo.ZoneInfo(key="Europe/Berlin")
)


@pytest.mark.parametrize(
"schedule",
[
"0 0 * * Tue-Wed",
"0 0 * * Tue,Wed",
],
)
def test_cron_day_range(schedule):
assert not scheduler.remove_all_jobs()
assert tasks.cron(schedule)(tasks.heartbeat)
init = datetime.datetime(2021, 1, 1, 0, 0, 0) # Friday
assert scheduler.get_jobs()[0].trigger.get_next_fire_time(
init, init
) == datetime.datetime(
2021, 1, 5, 0, 0, tzinfo=zoneinfo.ZoneInfo(key="Europe/Berlin")
)
init = datetime.datetime(2021, 1, 5, 0, 0, 0) # Tuesday
assert scheduler.get_jobs()[0].trigger.get_next_fire_time(
init, init
) == datetime.datetime(
2021, 1, 6, 0, 0, tzinfo=zoneinfo.ZoneInfo(key="Europe/Berlin")
)


def test_cron__every_15_minutes():
assert not scheduler.remove_all_jobs()
assert tasks.cron("*/15 * * * *")(tasks.heartbeat)
Expand All @@ -49,10 +73,18 @@ def test_cron__every_15_minutes():
)


def test_cron__error():
@pytest.mark.parametrize(
"schedule",
[
"* * * * 1",
"* * * * 2-3",
"* * * * 1,7",
],
)
def test_cron__error(schedule):
assert not scheduler.remove_all_jobs()
with pytest.raises(ValueError) as e:
tasks.cron("* * * * 1")(tasks.heartbeat)
tasks.cron(schedule)(tasks.heartbeat)
assert (
"Please use a literal day of week (Mon, Tue, Wed, Thu, Fri, Sat, Sun) or *"
in str(e.value)
Expand Down

0 comments on commit 990329c

Please sign in to comment.