diff --git a/backend/routers/recurring_schedules.py b/backend/routers/recurring_schedules.py index b784c5d..9a992c4 100644 --- a/backend/routers/recurring_schedules.py +++ b/backend/routers/recurring_schedules.py @@ -330,19 +330,35 @@ async def disable_schedule( db: Session = Depends(get_db), ): """ - Disable a schedule. + Disable a schedule and cancel all its pending actions. """ service = get_recurring_schedule_service(db) + + # Count pending actions before disabling (for response message) + from sqlalchemy import and_ + from backend.models import ScheduledAction + pending_count = db.query(ScheduledAction).filter( + and_( + ScheduledAction.execution_status == "pending", + ScheduledAction.notes.like(f'%"schedule_id": "{schedule_id}"%'), + ) + ).count() + schedule = service.disable_schedule(schedule_id) if not schedule: raise HTTPException(status_code=404, detail="Schedule not found") + message = "Schedule disabled" + if pending_count > 0: + message += f" and {pending_count} pending action(s) cancelled" + return { "success": True, "schedule_id": schedule.id, "enabled": schedule.enabled, - "message": "Schedule disabled", + "cancelled_actions": pending_count, + "message": message, } diff --git a/backend/services/recurring_schedule_service.py b/backend/services/recurring_schedule_service.py index f1da36a..3f53210 100644 --- a/backend/services/recurring_schedule_service.py +++ b/backend/services/recurring_schedule_service.py @@ -169,8 +169,25 @@ class RecurringScheduleService: return self.update_schedule(schedule_id, enabled=True) def disable_schedule(self, schedule_id: str) -> Optional[RecurringSchedule]: - """Disable a schedule.""" - return self.update_schedule(schedule_id, enabled=False) + """Disable a schedule and cancel its pending actions.""" + schedule = self.update_schedule(schedule_id, enabled=False) + if schedule: + # Cancel all pending actions generated by this schedule + pending_actions = self.db.query(ScheduledAction).filter( + and_( + ScheduledAction.execution_status == "pending", + ScheduledAction.notes.like(f'%"schedule_id": "{schedule_id}"%'), + ) + ).all() + + for action in pending_actions: + action.execution_status = "cancelled" + + if pending_actions: + self.db.commit() + logger.info(f"Cancelled {len(pending_actions)} pending actions for disabled schedule {schedule.name}") + + return schedule def generate_actions_for_schedule( self, diff --git a/templates/projects/detail.html b/templates/projects/detail.html index 9b6021e..fecf085 100644 --- a/templates/projects/detail.html +++ b/templates/projects/detail.html @@ -311,6 +311,7 @@ +