fixes related to automation and celery schedules
This commit is contained in:
@@ -16,45 +16,63 @@ logger = get_task_logger(__name__)
|
||||
@shared_task(name='automation.check_scheduled_automations')
|
||||
def check_scheduled_automations():
|
||||
"""
|
||||
Check for scheduled automation runs (runs every hour)
|
||||
Check for scheduled automation runs (runs every 15 minutes)
|
||||
Matches automations scheduled within the current 15-minute window.
|
||||
"""
|
||||
logger.info("[AutomationTask] Checking scheduled automations")
|
||||
|
||||
now = timezone.now()
|
||||
current_time = now.time()
|
||||
|
||||
# Calculate 15-minute window boundaries
|
||||
# Window starts at current quarter hour (0, 15, 30, 45)
|
||||
window_start_minute = (current_time.minute // 15) * 15
|
||||
window_end_minute = window_start_minute + 14
|
||||
|
||||
logger.info(f"[AutomationTask] Current time: {current_time}, checking window {current_time.hour}:{window_start_minute:02d}-{current_time.hour}:{window_end_minute:02d}")
|
||||
|
||||
# Find configs that should run now
|
||||
for config in AutomationConfig.objects.filter(is_enabled=True):
|
||||
# Check if it's time to run
|
||||
should_run = False
|
||||
scheduled_hour = config.scheduled_time.hour
|
||||
scheduled_minute = config.scheduled_time.minute
|
||||
|
||||
# Check if scheduled time falls within current 15-minute window
|
||||
def is_in_window():
|
||||
if current_time.hour != scheduled_hour:
|
||||
return False
|
||||
return window_start_minute <= scheduled_minute <= window_end_minute
|
||||
|
||||
if config.frequency == 'daily':
|
||||
# Run if current time matches scheduled_time
|
||||
if current_time.hour == config.scheduled_time.hour and current_time.minute < 60:
|
||||
# Run if scheduled_time falls within current 15-minute window
|
||||
if is_in_window():
|
||||
should_run = True
|
||||
elif config.frequency == 'weekly':
|
||||
# Run on Mondays at scheduled_time
|
||||
if now.weekday() == 0 and current_time.hour == config.scheduled_time.hour and current_time.minute < 60:
|
||||
# Run on Mondays within scheduled window
|
||||
if now.weekday() == 0 and is_in_window():
|
||||
should_run = True
|
||||
elif config.frequency == 'monthly':
|
||||
# Run on 1st of month at scheduled_time
|
||||
if now.day == 1 and current_time.hour == config.scheduled_time.hour and current_time.minute < 60:
|
||||
# Run on 1st of month within scheduled window
|
||||
if now.day == 1 and is_in_window():
|
||||
should_run = True
|
||||
|
||||
logger.debug(f"[AutomationTask] Site {config.site_id}: freq={config.frequency}, scheduled={config.scheduled_time}, should_run={should_run}")
|
||||
|
||||
if should_run:
|
||||
# Check if already ran today
|
||||
# Check if already ran within the last 23 hours (prevents duplicate runs)
|
||||
if config.last_run_at:
|
||||
time_since_last_run = now - config.last_run_at
|
||||
if time_since_last_run < timedelta(hours=23):
|
||||
logger.info(f"[AutomationTask] Skipping site {config.site.id} - already ran today")
|
||||
logger.info(f"[AutomationTask] Skipping site {config.site_id} - already ran {time_since_last_run} ago")
|
||||
continue
|
||||
|
||||
# Check if already running OR paused (don't start new if existing in progress)
|
||||
if AutomationRun.objects.filter(site=config.site, status__in=['running', 'paused']).exists():
|
||||
logger.info(f"[AutomationTask] Skipping site {config.site.id} - automation in progress (running/paused)")
|
||||
logger.info(f"[AutomationTask] Skipping site {config.site_id} - automation in progress (running/paused)")
|
||||
continue
|
||||
|
||||
logger.info(f"[AutomationTask] Starting scheduled automation for site {config.site.id}")
|
||||
logger.info(f"[AutomationTask] Starting scheduled automation for site {config.site_id}")
|
||||
|
||||
try:
|
||||
service = AutomationService(config.account, config.site)
|
||||
|
||||
Reference in New Issue
Block a user