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.
This commit is contained in:
Graham McIntire 2026-05-10 16:54:01 -05:00
parent a8f119261e
commit 36edf54e7c

View file

@ -32,19 +32,14 @@ defmodule Towerops.Workers.AiNetworkInsightWorker do
@impl Oban.Worker @impl Oban.Worker
def perform(%Oban.Job{}) do def perform(%Oban.Job{}) do
case Repo.transaction(fn -> org_ids =
Organization Organization
|> select([o], o.id) |> select([o], o.id)
|> Repo.stream() |> Repo.all()
|> Enum.each(&process_organization/1)
end) do
{:ok, _} ->
:ok
{:error, reason} -> Enum.each(org_ids, &process_organization/1)
Logger.error("AI network insight transaction failed: #{inspect(reason)}")
{:error, reason} :ok
end
end end
defp process_organization(organization_id) do defp process_organization(organization_id) do