U 优化任务计划Cron触发器的周保持与crontab一致

pull/103/head
vapao 2020-06-02 13:16:58 +08:00
parent 0e98c1553e
commit 8c2dac9ea8
2 changed files with 18 additions and 3 deletions

View File

@ -26,6 +26,17 @@ counter = dict()
class Scheduler: class Scheduler:
timezone = settings.TIME_ZONE 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): def __init__(self):
self.scheduler = BackgroundScheduler(timezone=self.timezone) self.scheduler = BackgroundScheduler(timezone=self.timezone)
@ -42,8 +53,9 @@ class Scheduler:
elif trigger == 'cron': elif trigger == 'cron':
args = json.loads(trigger_args) if not isinstance(trigger_args, dict) else trigger_args args = json.loads(trigger_args) if not isinstance(trigger_args, dict) else trigger_args
minute, hour, day, month, week = args['rule'].split() minute, hour, day, month, week = args['rule'].split()
return CronTrigger(minute=minute, hour=hour, day=day, month=month, week=week, start_date=args['start'], week = cls.week_map[week]
end_date=args['stop']) return CronTrigger(minute=minute, hour=hour, day=day, month=month, day_of_week=week,
start_date=args['start'], end_date=args['stop'])
else: else:
raise TypeError(f'unknown schedule policy: {trigger!r}') 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}') 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}') Notify.make_notify('schedule', '1', f'{obj.name} - 执行异常', f'{event.exception}')
elif event.code == EVENT_JOB_EXECUTED: elif event.code == EVENT_JOB_EXECUTED:
job = self.scheduler.get_job('3')
print(job.id, job.name, job.next_run_time)
if event.retval: if event.retval:
score = 0 score = 0
for item in event.retval: for item in event.retval:

View File

@ -35,8 +35,9 @@ class Schedule(View):
if len(args) != 5: if len(args) != 5:
return json_response(error='无效的执行规则,请更正后再试') return json_response(error='无效的执行规则,请更正后再试')
minute, hour, day, month, week = args minute, hour, day, month, week = args
week = '0' if week == '7' else week
try: 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: except ValueError:
return json_response(error='无效的执行规则,请更正后再试') return json_response(error='无效的执行规则,请更正后再试')
if form.id: if form.id: