From 36edf54e7c0978d835a4170ecc51e90273bdaa29 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sun, 10 May 2026 16:54:01 -0500 Subject: [PATCH] fix(insights): don't hold DB transaction across LLM API calls The Repo.transaction wrapping Repo.stream() held a checkout for the entire iteration, including the LLM HTTP call. When the LLM took >15s, Postgrex killed the connection, cascading to all subsequent orgs. Collect org IDs first, then process each independently. --- .../workers/ai_network_insight_worker.ex | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/towerops/workers/ai_network_insight_worker.ex b/lib/towerops/workers/ai_network_insight_worker.ex index 49e7c7e6..8b8a7b87 100644 --- a/lib/towerops/workers/ai_network_insight_worker.ex +++ b/lib/towerops/workers/ai_network_insight_worker.ex @@ -32,19 +32,14 @@ defmodule Towerops.Workers.AiNetworkInsightWorker do @impl Oban.Worker def perform(%Oban.Job{}) do - case Repo.transaction(fn -> - Organization - |> select([o], o.id) - |> Repo.stream() - |> Enum.each(&process_organization/1) - end) do - {:ok, _} -> - :ok + org_ids = + Organization + |> select([o], o.id) + |> Repo.all() - {:error, reason} -> - Logger.error("AI network insight transaction failed: #{inspect(reason)}") - {:error, reason} - end + Enum.each(org_ids, &process_organization/1) + + :ok end defp process_organization(organization_id) do