diff --git a/lib/towerops_web/controllers/api/agent_controller.ex b/lib/towerops_web/controllers/api/agent_controller.ex index d63ef184..f220acdd 100644 --- a/lib/towerops_web/controllers/api/agent_controller.ex +++ b/lib/towerops_web/controllers/api/agent_controller.ex @@ -116,8 +116,15 @@ defmodule ToweropsWeb.Api.AgentController do } end - # Update synchronously in the heartbeat endpoint - Agents.update_agent_token_heartbeat(agent_token.id, ip, metadata) + # In test environment, update synchronously to avoid DB ownership issues + # In production, update asynchronously for better performance + if Application.get_env(:towerops, :env) == :test do + Agents.update_agent_token_heartbeat(agent_token.id, ip, metadata) + else + Task.start(fn -> + Agents.update_agent_token_heartbeat(agent_token.id, ip, metadata) + end) + end # Return response in appropriate format case get_req_header(conn, "content-type") do