fixes related to automation and celery schedules
This commit is contained in:
@@ -186,13 +186,76 @@ class UnifiedSiteSettingsViewSet(viewsets.ViewSet):
|
||||
# Update automation settings
|
||||
if 'automation' in data:
|
||||
auto = data['automation']
|
||||
schedule_changed = False
|
||||
|
||||
if 'enabled' in auto:
|
||||
if automation_config.is_enabled != auto['enabled']:
|
||||
schedule_changed = True
|
||||
automation_config.is_enabled = auto['enabled']
|
||||
if 'frequency' in auto:
|
||||
if automation_config.frequency != auto['frequency']:
|
||||
schedule_changed = True
|
||||
automation_config.frequency = auto['frequency']
|
||||
if 'time' in auto:
|
||||
from datetime import datetime
|
||||
automation_config.scheduled_time = datetime.strptime(auto['time'], '%H:%M').time()
|
||||
new_time = datetime.strptime(auto['time'], '%H:%M').time()
|
||||
if automation_config.scheduled_time != new_time:
|
||||
schedule_changed = True
|
||||
automation_config.scheduled_time = new_time
|
||||
|
||||
# Reset last_run_at and recalculate next_run_at if any schedule setting changed
|
||||
if schedule_changed:
|
||||
automation_config.last_run_at = None
|
||||
|
||||
# Recalculate next_run_at based on new schedule
|
||||
from django.utils import timezone
|
||||
from datetime import timedelta
|
||||
|
||||
now = timezone.now()
|
||||
scheduled_time = automation_config.scheduled_time
|
||||
|
||||
# Calculate next run at the scheduled time
|
||||
next_run = now.replace(
|
||||
hour=scheduled_time.hour,
|
||||
minute=scheduled_time.minute,
|
||||
second=0,
|
||||
microsecond=0
|
||||
)
|
||||
|
||||
# If scheduled time has passed today, set to tomorrow (for daily)
|
||||
# or appropriate next occurrence for weekly/monthly
|
||||
if next_run <= now:
|
||||
if automation_config.frequency == 'daily':
|
||||
next_run = next_run + timedelta(days=1)
|
||||
elif automation_config.frequency == 'weekly':
|
||||
# Next Monday
|
||||
days_until_monday = (7 - now.weekday()) % 7
|
||||
if days_until_monday == 0:
|
||||
days_until_monday = 7
|
||||
next_run = now + timedelta(days=days_until_monday)
|
||||
next_run = next_run.replace(
|
||||
hour=scheduled_time.hour,
|
||||
minute=scheduled_time.minute,
|
||||
second=0,
|
||||
microsecond=0
|
||||
)
|
||||
elif automation_config.frequency == 'monthly':
|
||||
# Next 1st of month
|
||||
if now.month == 12:
|
||||
next_run = now.replace(year=now.year + 1, month=1, day=1)
|
||||
else:
|
||||
next_run = now.replace(month=now.month + 1, day=1)
|
||||
next_run = next_run.replace(
|
||||
hour=scheduled_time.hour,
|
||||
minute=scheduled_time.minute,
|
||||
second=0,
|
||||
microsecond=0
|
||||
)
|
||||
else:
|
||||
next_run = next_run + timedelta(days=1)
|
||||
|
||||
automation_config.next_run_at = next_run
|
||||
logger.info(f"[UnifiedSettings] Schedule changed for site {site_id}, reset last_run_at=None, next_run_at={next_run}")
|
||||
|
||||
# Update stage configuration
|
||||
if 'stages' in data:
|
||||
|
||||
Reference in New Issue
Block a user