towerops/lib/towerops_web/live/maintenance_live/index.ex
Graham McIntire de986bddf6
Prevent Oban polling/monitoring job stacking per device
When the Oban queue backs up, the 60-second uniqueness window expires
and duplicate jobs stack up per device. Switch to period: :infinity so
only one poll/monitor job exists per device at any time. Add replace
option to supersede stale scheduled jobs with updated scheduled_at.
Remove :executing from unique states so self-scheduling works while
the current job runs. Set max_attempts: 1 since retrying stale polls
is pointless.

Also fix all credo --strict issues across the codebase.
2026-02-16 16:37:48 -06:00

67 lines
1.7 KiB
Elixir

defmodule ToweropsWeb.MaintenanceLive.Index do
@moduledoc false
use ToweropsWeb, :live_view
alias Towerops.Maintenance
@impl true
def mount(_params, _session, socket) do
organization = socket.assigns.current_scope.organization
{:ok,
socket
|> assign(:page_title, t("Maintenance Windows"))
|> assign(:filter, "all")
|> assign(:timezone, socket.assigns.current_scope.timezone)
|> load_windows(organization.id)}
end
@impl true
def handle_params(params, _url, socket) do
filter = Map.get(params, "filter", "all")
{:noreply,
socket
|> assign(:filter, filter)
|> load_windows(socket.assigns.current_scope.organization.id)}
end
defp load_windows(socket, org_id) do
filter_atom =
case socket.assigns.filter do
"active" -> :active
"upcoming" -> :upcoming
"past" -> :past
_ -> nil
end
opts = if filter_atom, do: [filter: filter_atom], else: []
windows = Maintenance.list_windows(org_id, opts)
assign(socket, :windows, windows)
end
defp status_for(window) do
now = DateTime.utc_now()
cond do
DateTime.after?(window.starts_at, now) -> :upcoming
DateTime.before?(window.ends_at, now) -> :past
true -> :active
end
end
defp scope_label(window) do
cond do
window.device -> "Device: #{window.device.name}"
window.site -> "Site: #{window.site.name}"
true -> "Org-wide"
end
end
defp format_datetime(dt, timezone) do
case DateTime.shift_zone(dt, timezone) do
{:ok, local} -> Calendar.strftime(local, "%b %d, %Y %I:%M %p")
_ -> Calendar.strftime(dt, "%b %d, %Y %I:%M %p UTC")
end
end
end