towerops/lib/towerops_web/controllers/admin_controller.ex
Graham McIntire 1712e7ac9d
Skip SNMP check execution in CheckExecutorWorker for agent-polled devices
CheckExecutorWorker was missing the phoenix_snmp_disabled and
should_phoenix_poll_device guards that DevicePollerWorker and
DeviceMonitorWorker already have. This caused SNMP checks to execute
through Phoenix even when devices are assigned to agents, creating
an infinite loop of failing jobs that fill the Oban queue.
2026-02-17 07:47:15 -06:00

45 lines
1.2 KiB
Elixir

defmodule ToweropsWeb.AdminController do
@moduledoc """
Controller for admin operations including user impersonation.
"""
use ToweropsWeb, :controller
alias ToweropsWeb.UserAuth
@doc """
Start impersonating a user.
"""
def start_impersonate(conn, %{"user_id" => user_id}) do
UserAuth.start_impersonation(conn, user_id)
end
@doc """
Stop impersonating and return to superuser.
"""
def stop_impersonate(conn, _params) do
UserAuth.stop_impersonation(conn)
end
@doc """
Flush Oban jobs by state. Cancels all jobs in the given state(s).
POST /admin/oban/flush?states=scheduled,retryable
"""
def flush_oban(conn, %{"states" => states_param}) do
import Ecto.Query
valid_states = ~w(scheduled retryable available)
requested = states_param |> String.split(",") |> Enum.map(&String.trim/1)
states = Enum.filter(requested, &(&1 in valid_states))
if states == [] do
conn |> put_status(400) |> json(%{error: "No valid states. Use: scheduled, retryable, available"})
else
{count, _} =
Oban.Job
|> where([j], j.state in ^states)
|> Towerops.Repo.delete_all()
json(conn, %{ok: true, deleted: count, states: states})
end
end
end