Skip to content

Commit

Permalink
Merge pull request #115 from ceallen/72-native-cron-scheduler-doesnt-…
Browse files Browse the repository at this point in the history
…match-convention

Convert UNIX style crontab days-of-week to APScheduler format
  • Loading branch information
jonbannister authored Sep 29, 2022
2 parents 6c92170 + ddfba56 commit 56f55f2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.4.5 (2022-09-28)
------------------

* Bugfix: The scheduler now follows UNIX conventions for day-of-week specifications.

0.4.4 (2022-07-18)
------------------

Expand Down
34 changes: 34 additions & 0 deletions notebooker/web/routes/scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,45 @@ def create_schedule(report_name):
return jsonify({"status": "Failed", "content": str(e)})


def convert_day_of_week(day_of_week: str) -> str:
"""
UNIX standard maps days-of-week to ints as:
SUN: 0
MON: 1
TUE: 2....
APScheduler uses:
MON: 0
TUE: 1
WED: 2.....
Given we are providing a UNIX-style crontab, convert ints accordingly. Don't shift char-based descriptors,
ie., 'MON-FRI'.
Parameters
----------
day_of_week - str - "FRI", "MON-FRI", "1"(UNIX Monday)
Returns
-------
day_of_week formatted to APScheduler standard
"""

def shift(mychar):
if mychar.isnumeric():
myint = int(mychar)
return str((myint + 6) % 7)
else:
return mychar

return "".join([shift(char) for char in day_of_week])


def validate_crontab(crontab: str, issues: List[str]) -> cron.CronTrigger:
parts = crontab.split()
if len(parts) != 5:
issues.append("The crontab key must be passed with a string using 5 crontab parts")
else:
parts[4] = convert_day_of_week(parts[4])
return cron.CronTrigger(minute=parts[0], hour=parts[1], day=parts[2], month=parts[3], day_of_week=parts[4])


Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_convert_day_of_week.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from notebooker.web.routes.scheduling import convert_day_of_week


def test_weekdays():
result = convert_day_of_week("1-5")
assert result == "0-4"


def test_sunday():
result = convert_day_of_week("0")
assert result == "6"


def test_string_days():
result = convert_day_of_week("MON-FRI")
assert result == "MON-FRI"

0 comments on commit 56f55f2

Please sign in to comment.