From 8c2dac9ea8988e84b242bd2efbed5cd4500aff0d Mon Sep 17 00:00:00 2001 From: vapao Date: Tue, 2 Jun 2020 13:16:58 +0800 Subject: [PATCH] =?UTF-8?q?U=20=E4=BC=98=E5=8C=96=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E8=AE=A1=E5=88=92Cron=E8=A7=A6=E5=8F=91=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E5=91=A8=E4=BF=9D=E6=8C=81=E4=B8=8Ecrontab=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/apps/schedule/scheduler.py | 18 ++++++++++++++++-- spug_api/apps/schedule/views.py | 3 ++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/spug_api/apps/schedule/scheduler.py b/spug_api/apps/schedule/scheduler.py index d11707d..b1834f0 100644 --- a/spug_api/apps/schedule/scheduler.py +++ b/spug_api/apps/schedule/scheduler.py @@ -26,6 +26,17 @@ counter = dict() class Scheduler: timezone = settings.TIME_ZONE + week_map = { + '*': '*', + '7': '6', + '0': '6', + '1': '0', + '2': '1', + '3': '2', + '4': '3', + '5': '4', + '6': '5', + } def __init__(self): self.scheduler = BackgroundScheduler(timezone=self.timezone) @@ -42,8 +53,9 @@ class Scheduler: elif trigger == 'cron': args = json.loads(trigger_args) if not isinstance(trigger_args, dict) else trigger_args minute, hour, day, month, week = args['rule'].split() - return CronTrigger(minute=minute, hour=hour, day=day, month=month, week=week, start_date=args['start'], - end_date=args['stop']) + week = cls.week_map[week] + return CronTrigger(minute=minute, hour=hour, day=day, month=month, day_of_week=week, + start_date=args['start'], end_date=args['stop']) else: raise TypeError(f'unknown schedule policy: {trigger!r}') @@ -60,6 +72,8 @@ class Scheduler: logger.info(f'EVENT_JOB_ERROR: job_id {event.job_id} exception: {event.exception}') Notify.make_notify('schedule', '1', f'{obj.name} - 执行异常', f'{event.exception}') elif event.code == EVENT_JOB_EXECUTED: + job = self.scheduler.get_job('3') + print(job.id, job.name, job.next_run_time) if event.retval: score = 0 for item in event.retval: diff --git a/spug_api/apps/schedule/views.py b/spug_api/apps/schedule/views.py index b451b33..4017e7d 100644 --- a/spug_api/apps/schedule/views.py +++ b/spug_api/apps/schedule/views.py @@ -35,8 +35,9 @@ class Schedule(View): if len(args) != 5: return json_response(error='无效的执行规则,请更正后再试') minute, hour, day, month, week = args + week = '0' if week == '7' else week try: - CronTrigger(minute=minute, hour=hour, day=day, month=month, week=week) + CronTrigger(minute=minute, hour=hour, day=day, month=month, day_of_week=week) except ValueError: return json_response(error='无效的执行规则,请更正后再试') if form.id: