Improve orphaned CrontabSchedule cleanup logic to avoid deleting in-use schedules

This commit is contained in:
SergeantPanda 2025-12-15 16:54:12 -06:00
parent 1b27472c81
commit 41642cd479

View file

@ -190,9 +190,14 @@ def _sync_periodic_task() -> None:
def _cleanup_orphaned_crontab(crontab_schedule):
"""Delete old CrontabSchedule from backup task."""
"""Delete old CrontabSchedule if no other tasks are using it."""
if crontab_schedule is None:
return
logger.debug(f"Cleaning up old CrontabSchedule: {crontab_schedule.id}")
# Check if any other tasks are using this crontab
if PeriodicTask.objects.filter(crontab=crontab_schedule).exists():
logger.debug(f"CrontabSchedule {crontab_schedule.id} still in use, not deleting")
return
logger.debug(f"Cleaning up orphaned CrontabSchedule: {crontab_schedule.id}")
crontab_schedule.delete()