|
|
|
|
@ -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' |
|
|
|
|
)) |
|
|
|
|
|