Skip to content

Commit 56f55f2

Browse files
authored
Merge pull request #115 from ceallen/72-native-cron-scheduler-doesnt-match-convention
Convert UNIX style crontab days-of-week to APScheduler format
2 parents 6c92170 + ddfba56 commit 56f55f2

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
0.4.5 (2022-09-28)
2+
------------------
3+
4+
* Bugfix: The scheduler now follows UNIX conventions for day-of-week specifications.
5+
16
0.4.4 (2022-07-18)
27
------------------
38

notebooker/web/routes/scheduling.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,45 @@ def create_schedule(report_name):
119119
return jsonify({"status": "Failed", "content": str(e)})
120120

121121

122+
def convert_day_of_week(day_of_week: str) -> str:
123+
"""
124+
UNIX standard maps days-of-week to ints as:
125+
SUN: 0
126+
MON: 1
127+
TUE: 2....
128+
129+
APScheduler uses:
130+
MON: 0
131+
TUE: 1
132+
WED: 2.....
133+
134+
Given we are providing a UNIX-style crontab, convert ints accordingly. Don't shift char-based descriptors,
135+
ie., 'MON-FRI'.
136+
Parameters
137+
----------
138+
day_of_week - str - "FRI", "MON-FRI", "1"(UNIX Monday)
139+
140+
Returns
141+
-------
142+
day_of_week formatted to APScheduler standard
143+
"""
144+
145+
def shift(mychar):
146+
if mychar.isnumeric():
147+
myint = int(mychar)
148+
return str((myint + 6) % 7)
149+
else:
150+
return mychar
151+
152+
return "".join([shift(char) for char in day_of_week])
153+
154+
122155
def validate_crontab(crontab: str, issues: List[str]) -> cron.CronTrigger:
123156
parts = crontab.split()
124157
if len(parts) != 5:
125158
issues.append("The crontab key must be passed with a string using 5 crontab parts")
126159
else:
160+
parts[4] = convert_day_of_week(parts[4])
127161
return cron.CronTrigger(minute=parts[0], hour=parts[1], day=parts[2], month=parts[3], day_of_week=parts[4])
128162

129163

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from notebooker.web.routes.scheduling import convert_day_of_week
2+
3+
4+
def test_weekdays():
5+
result = convert_day_of_week("1-5")
6+
assert result == "0-4"
7+
8+
9+
def test_sunday():
10+
result = convert_day_of_week("0")
11+
assert result == "6"
12+
13+
14+
def test_string_days():
15+
result = convert_day_of_week("MON-FRI")
16+
assert result == "MON-FRI"

0 commit comments

Comments
 (0)