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
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue