fix tests
This commit is contained in:
parent
f16b0ebabd
commit
5e1a97fcb3
4 changed files with 21 additions and 14 deletions
|
|
@ -47,4 +47,5 @@ config :towerops, ToweropsWeb.Endpoint,
|
|||
config :towerops,
|
||||
ping_module: Towerops.Monitoring.PingMock,
|
||||
poller_module: Towerops.Snmp.PollerMock,
|
||||
snmp_adapter: Towerops.Snmp.SnmpMock
|
||||
snmp_adapter: Towerops.Snmp.SnmpMock,
|
||||
sql_sandbox: true
|
||||
|
|
|
|||
|
|
@ -83,18 +83,20 @@ defmodule Towerops.Agents do
|
|||
{1, nil}
|
||||
|
||||
"""
|
||||
def update_agent_token_heartbeat(agent_token_id, ip_address, metadata \\ %{}) do
|
||||
def update_agent_token_heartbeat(agent_token_id, ip_address, metadata \\ nil) do
|
||||
now = DateTime.truncate(DateTime.utc_now(), :second)
|
||||
|
||||
# Only update metadata if explicitly provided (not nil and not empty)
|
||||
updates =
|
||||
case metadata do
|
||||
nil -> [last_seen_at: now, last_ip: ip_address]
|
||||
meta when map_size(meta) > 0 -> [last_seen_at: now, last_ip: ip_address, metadata: meta]
|
||||
_ -> [last_seen_at: now, last_ip: ip_address]
|
||||
end
|
||||
|
||||
AgentToken
|
||||
|> where([t], t.id == ^agent_token_id)
|
||||
|> Repo.update_all(
|
||||
set: [
|
||||
last_seen_at: now,
|
||||
last_ip: ip_address,
|
||||
metadata: metadata
|
||||
]
|
||||
)
|
||||
|> Repo.update_all(set: updates)
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
|
|
|||
|
|
@ -77,11 +77,7 @@ defmodule ToweropsWeb.Api.AgentController do
|
|||
}
|
||||
|
||||
# Update synchronously in the heartbeat endpoint
|
||||
fn ->
|
||||
Agents.update_agent_token_heartbeat(agent_token.id, ip, metadata)
|
||||
end
|
||||
|> Task.async()
|
||||
|> Task.await()
|
||||
Agents.update_agent_token_heartbeat(agent_token.id, ip, metadata)
|
||||
|
||||
json(conn, %{status: "ok"})
|
||||
end
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ defmodule ToweropsWeb.Plugs.AgentAuth do
|
|||
import Phoenix.Controller
|
||||
import Plug.Conn
|
||||
|
||||
alias Ecto.Adapters.SQL.Sandbox
|
||||
alias Towerops.Agents
|
||||
|
||||
@doc """
|
||||
|
|
@ -43,7 +44,14 @@ defmodule ToweropsWeb.Plugs.AgentAuth do
|
|||
defp update_heartbeat(conn, agent_token) do
|
||||
ip = to_string(:inet_parse.ntoa(conn.remote_ip))
|
||||
|
||||
parent = self()
|
||||
|
||||
Task.start(fn ->
|
||||
# Allow this task to use the test database sandbox
|
||||
if Application.get_env(:towerops, :sql_sandbox) do
|
||||
Sandbox.allow(Towerops.Repo, parent, self())
|
||||
end
|
||||
|
||||
Agents.update_agent_token_heartbeat(agent_token.id, ip)
|
||||
end)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue