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 <noreply@anthropic.com>
This commit is contained in:
Graham McIntire 2026-02-08 14:06:09 -06:00
parent cbff651f83
commit bc8e8a4261
No known key found for this signature in database

View file

@ -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 """