From bc8e8a426134966abcb1a6cb55c386c4869c7386 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sun, 8 Feb 2026 14:06:09 -0600 Subject: [PATCH] feat: notify agents when devices are deleted When a device is deleted, now broadcasts an assignment change event to the agent (if assigned), causing the agent to receive an updated job list without the deleted device. This ensures agents stop processing any in-flight jobs for deleted devices. Changes: - Get agent assignment before deleting device - Broadcast :device_deleted event to agent after deletion - Agent channel receives broadcast and sends updated job list - Agents naturally stop processing jobs for devices not in new list Previously only cancelled Oban jobs but didn't notify agents, leaving potential for agents to continue processing jobs for deleted devices. Co-Authored-By: Claude Sonnet 4.5 --- lib/towerops/devices.ex | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/towerops/devices.ex b/lib/towerops/devices.ex index 4192a016..f400ca12 100644 --- a/lib/towerops/devices.ex +++ b/lib/towerops/devices.ex @@ -800,7 +800,19 @@ defmodule Towerops.Devices do _ = DeviceMonitorWorker.stop_monitoring(device.id) _ = DevicePollerWorker.stop_polling(device.id) - Repo.delete(device) + # Get agent assignment before deleting (if any) + assignment = Agents.get_device_assignment(device.id) + agent_token_id = if assignment, do: assignment.agent_token_id + + # Delete the device + result = Repo.delete(device) + + # Notify agent to stop processing jobs for this device + if agent_token_id do + Agents.broadcast_assignment_change(agent_token_id, :device_deleted) + end + + result end @doc """