From 62ff6f7e8ad86e31e77f00e7f4ac0523b0b71ce5 Mon Sep 17 00:00:00 2001 From: mmonkey Date: Fri, 28 Jan 2022 23:48:44 -0600 Subject: [PATCH] Fixed tasks not running if scheduled time starts and ends on different days. --- cps/schedule.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cps/schedule.py b/cps/schedule.py index 39c9bc09..5b97cecb 100644 --- a/cps/schedule.py +++ b/cps/schedule.py @@ -66,8 +66,7 @@ def register_scheduled_tasks(): scheduler.schedule(func=end_scheduled_tasks, trigger='cron', hour=end) # Kick-off tasks, if they should currently be running - now = datetime.datetime.now().hour - if start <= now < end: + if should_task_be_running(start, end): scheduler.schedule_tasks_immediately(tasks=get_scheduled_tasks(False)) @@ -77,9 +76,13 @@ def register_startup_tasks(): if scheduler: start = config.schedule_start_time end = config.schedule_end_time - now = datetime.datetime.now().hour # Run scheduled tasks immediately for development and testing # Ignore tasks that should currently be running, as these will be added when registering scheduled tasks - if constants.APP_MODE in ['development', 'test'] and not (start <= now < end): + if constants.APP_MODE in ['development', 'test'] and not should_task_be_running(start, end): scheduler.schedule_tasks_immediately(tasks=get_scheduled_tasks(False)) + + +def should_task_be_running(start, end): + now = datetime.datetime.now().hour + return (start < end and start <= now < end) or (end < start <= now or now < end)