From fe33401d0216bdedcb505eb8a0fe6a47b5a3dc86 Mon Sep 17 00:00:00 2001 From: Raz Date: Mon, 14 Apr 2025 14:03:38 +0200 Subject: [PATCH] add BACKGROUND_SCHEDULED_TASK_INTERVAL settings --- padelclub_backend/settings_local.py.dist | 2 ++ .../management/commands/schedule_tasks.py | 21 +++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/padelclub_backend/settings_local.py.dist b/padelclub_backend/settings_local.py.dist index a9e417f..bdc3662 100644 --- a/padelclub_backend/settings_local.py.dist +++ b/padelclub_backend/settings_local.py.dist @@ -77,3 +77,5 @@ TOURNAMENT_SETTINGS = { } } } + +BACKGROUND_SCHEDULED_TASK_INTERVAL = 30 # minutes diff --git a/tournaments/management/commands/schedule_tasks.py b/tournaments/management/commands/schedule_tasks.py index 638fb6c..4a9d6d5 100644 --- a/tournaments/management/commands/schedule_tasks.py +++ b/tournaments/management/commands/schedule_tasks.py @@ -3,6 +3,7 @@ from tournaments.tasks import check_confirmation_deadlines from django.utils import timezone import datetime from background_task.models import Task +from django.conf import settings class Command(BaseCommand): help = 'Schedule background tasks to run at :00 and :30 of every hour' @@ -18,19 +19,13 @@ class Command(BaseCommand): local_timezone = timezone.get_current_timezone() local_now = now.astimezone(local_timezone) - # Calculate time until next half-hour mark (either :00 or :30) + # Calculate time until next interval current_minute = local_now.minute + minutes_until_next = settings.BACKGROUND_SCHEDULED_TASK_INTERVAL - (current_minute % settings.BACKGROUND_SCHEDULED_TASK_INTERVAL) + next_minute = (current_minute + minutes_until_next) % 60 + next_hour = local_now.hour + ((current_minute + minutes_until_next) // 60) - if current_minute < 30: - # Next run at XX:30:00 - next_minute = 30 - next_hour = local_now.hour - else: - # Next run at (XX+1):00:00 - next_minute = 0 - next_hour = local_now.hour + 1 - - # Create a datetime with exactly XX:30:00 or XX:00:00 in local time + # Create a datetime with the next run time in local time first_run_local = local_now.replace( hour=next_hour, minute=next_minute + 1, #let the expiration time be off first @@ -50,7 +45,7 @@ class Command(BaseCommand): # Schedule with seconds delay instead of a specific datetime check_confirmation_deadlines( schedule=int(seconds_until_first_run), # Delay in seconds before first run - repeat=1800 # 30 minutes in seconds + repeat=settings.BACKGROUND_SCHEDULED_TASK_INTERVAL * 60 # 30 minutes in seconds ) # Show the message with proper timezone info @@ -58,5 +53,5 @@ class Command(BaseCommand): self.stdout.write(self.style.SUCCESS( f'Task scheduled to first run at {first_run_local.strftime("%H:%M:%S")} {local_timezone_name} ' f'(in {int(seconds_until_first_run)} seconds) ' - f'and then every 30 minutes' + f'and then every {settings.BACKGROUND_SCHEDULED_TASK_INTERVAL} minutes' ))