towerops/lib/towerops/workers/netbox_sync_worker.ex

82 lines
2.7 KiB
Elixir

defmodule Towerops.Workers.NetBoxSyncWorker do
@moduledoc """
Oban worker that syncs NetBox data for enabled integrations.
The cron job (no args) runs every 30 minutes and dispatches individual
per-integration sync jobs staggered across the 600-second window using
`PollingOffset.calculate_offset/2`. This prevents a thundering herd
against NetBox APIs and stops one slow tenant from blocking the
maintenance queue while every other integration waits in line.
Individual jobs receive `%{"integration_id" => id}` and perform the
actual sync via `NetBox.Sync.sync_organization/1`.
"""
use Oban.Pro.Worker, queue: :maintenance
alias Towerops.Integrations
alias Towerops.NetBox.Sync
alias Towerops.Workers.PollingOffset
alias Towerops.Workers.SyncErrors
require Logger
@provider "netbox"
@default_interval_minutes 30
@window_seconds 600
# Cron dispatcher: enqueues staggered individual sync jobs.
@impl Oban.Pro.Worker
def process(%Oban.Job{args: args}) when args == %{} do
integrations = Integrations.list_enabled_integrations(@provider)
eligible = Enum.filter(integrations, &due_for_sync?/1)
now = DateTime.utc_now()
enqueued =
Enum.count(eligible, fn integration ->
offset = PollingOffset.calculate_offset(integration.organization_id, @window_seconds)
scheduled_at = DateTime.add(now, offset, :second)
case %{"integration_id" => integration.id}
|> new(scheduled_at: scheduled_at, unique: [period: @window_seconds])
|> Oban.insert() do
{:ok, _job} -> true
{:error, _reason} -> false
end
end)
if enqueued > 0 do
Logger.info("NetBox sync: dispatched #{enqueued} jobs across #{@window_seconds}s window")
end
:ok
end
# Individual sync: loads integration by ID and syncs.
def process(%Oban.Job{args: %{"integration_id" => id}}) do
case Integrations.get_integration_by_id(id) do
{:ok, integration} ->
sync_integration(integration)
{:error, :not_found} ->
Logger.warning("NetBox sync: integration #{id} not found, skipping")
{:error, :not_found}
end
end
defp sync_integration(integration) do
case Sync.sync_organization(integration) do
{:ok, result} ->
Logger.info("NetBox sync completed for org #{integration.organization_id}: #{inspect(result)}")
:ok
{:error, reason} ->
Logger.error("NetBox sync failed for org #{integration.organization_id}: #{inspect(reason)}")
if SyncErrors.transient?(reason), do: {:error, reason}, else: :ok
end
end
@doc false
def due_for_sync?(integration), do: Integrations.due_for_sync?(integration, @default_interval_minutes)
end