From cadba46ec9f02715d3b035f478b2ad96cf17d920 Mon Sep 17 00:00:00 2001 From: "Allen, Chris (Boston)" Date: Tue, 13 Sep 2022 21:54:43 -0400 Subject: [PATCH] Convert UNIX style crontab days-of-week to APScheduler format --- CHANGELOG.md | 5 ++++ notebooker/web/routes/scheduling.py | 34 ++++++++++++++++++++++++++ tests/unit/test_convert_day_of_week.py | 16 ++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 tests/unit/test_convert_day_of_week.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 9611d4e8..e7209656 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------ diff --git a/notebooker/web/routes/scheduling.py b/notebooker/web/routes/scheduling.py index ed54dc9c..2f3601be 100644 --- a/notebooker/web/routes/scheduling.py +++ b/notebooker/web/routes/scheduling.py @@ -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]) diff --git a/tests/unit/test_convert_day_of_week.py b/tests/unit/test_convert_day_of_week.py new file mode 100644 index 00000000..f9cb70a3 --- /dev/null +++ b/tests/unit/test_convert_day_of_week.py @@ -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" \ No newline at end of file