feat: integrate lifecycle event broadcasting into DevicePollerWorker
Add Events.broadcast_job_event calls to track job lifecycle: - :started when job begins executing - :completed/:failed when job finishes - Track duration in seconds for all job executions Broadcast events enable real-time monitoring of polling operations. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
9f30d366b1
commit
a2778d9b7c
1 changed files with 26 additions and 9 deletions
|
|
@ -22,6 +22,7 @@ defmodule Towerops.Workers.DevicePollerWorker do
|
|||
|
||||
alias Towerops.Agents
|
||||
alias Towerops.Devices
|
||||
alias Towerops.JobMonitoring.Events
|
||||
alias Towerops.Snmp
|
||||
alias Towerops.Snmp.ArpDiscovery
|
||||
alias Towerops.Snmp.Client
|
||||
|
|
@ -34,17 +35,33 @@ defmodule Towerops.Workers.DevicePollerWorker do
|
|||
@default_poll_interval 60
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(%Oban.Job{args: %{"device_id" => device_id}}) do
|
||||
case Devices.get_device(device_id) do
|
||||
nil ->
|
||||
Logger.debug("Device #{device_id} no longer exists, skipping poll")
|
||||
:ok
|
||||
def perform(%Oban.Job{args: %{"device_id" => device_id}} = job) do
|
||||
Events.broadcast_job_event(job, :started)
|
||||
start_time = System.monotonic_time(:second)
|
||||
|
||||
device ->
|
||||
maybe_poll_device(device)
|
||||
schedule_next_poll_with_error_handling(device_id, device)
|
||||
:ok
|
||||
result =
|
||||
case Devices.get_device(device_id) do
|
||||
nil ->
|
||||
Logger.debug("Device #{device_id} no longer exists, skipping poll")
|
||||
:ok
|
||||
|
||||
device ->
|
||||
maybe_poll_device(device)
|
||||
schedule_next_poll_with_error_handling(device_id, device)
|
||||
:ok
|
||||
end
|
||||
|
||||
duration = System.monotonic_time(:second) - start_time
|
||||
|
||||
case result do
|
||||
:ok ->
|
||||
Events.broadcast_job_event(job, :completed, %{duration: duration})
|
||||
|
||||
{:error, reason} ->
|
||||
Events.broadcast_job_event(job, :failed, %{error: inspect(reason), duration: duration})
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
defp maybe_poll_device(device) do
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue