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:
parent
a8f119261e
commit
36edf54e7c
1 changed files with 7 additions and 12 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue