Skip to content

Commit

Permalink
修复 cron表达式星期参数的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed Aug 16, 2023
1 parent a5806ea commit 06f3ffb
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions domain_admin/service/scheduler_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@

scheduler = BackgroundScheduler(job_defaults=JOB_DEFAULTS)


# 任务列表
TASK_LIST = [
# 更新证书信息
Expand All @@ -65,6 +64,32 @@
]


def crontab_compatible_weekday(expr):
"""
:param expr:
:return:
bugfix: 0-6表示周一到周日,改为周日到周六,并支持7为周日
day_of_week: number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
ref: https://github.com/agronholm/apscheduler/issues/286#issuecomment-449273964
"""
if expr == "*":
return expr

mapping = {
"0": "sun",
"1": "mon",
"2": "tue",
"3": "wed",
"4": "thu",
"5": "fri",
"6": "sat",
"7": "sun"
}

return "".join(map(lambda x: mapping.get(x, x), expr))


def init_scheduler():
scheduler_cron = system_service.get_config(ConfigKeyEnum.SCHEDULER_CRON)

Expand All @@ -90,7 +115,7 @@ def update_job(cron_exp):
hour=hour,
day=day,
month=month,
day_of_week=day_of_week
day_of_week=crontab_compatible_weekday(day_of_week)
)


Expand Down

0 comments on commit 06f3ffb

Please sign in to comment.