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
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