Skip to content

Defer DB connection until after jixia analysis completes#17

Merged
Gabrielebattimelli merged 1 commit into
mainfrom
add-agent-skill
Jul 7, 2026
Merged

Defer DB connection until after jixia analysis completes#17
Gabrielebattimelli merged 1 commit into
mainfrom
add-agent-skill

Conversation

@Gabrielebattimelli

Copy link
Copy Markdown
Member

Why

The TCP keepalive fix (#16) did not help — the run failed identically, at the same spot (Units/WithDim/Mass, the first INSERT INTO module after analysis), with the same error:

psycopg.OperationalError: consuming input failed: server closed the connection unexpectedly

This means the connection isn't being dropped by a network-level idle timeout that keepalive packets would prevent — something (most plausibly Heroku's own connection handling) is closing it based on application-level inactivity, which TCP keepalives are invisible to.

What

Stop trying to keep a connection alive through the long idle period at all. Split jixia_db.load_data into:

  • run_analysis(project, prefixes) — pure CPU work (batch_run_jixia), no DB connection, can take 30-90+ min.
  • load_data(project, analysis, conn) — DB writes only, using the precomputed analysis.

database/__init__.py now runs the full analysis first, and only opens the (still keepalive-enabled, cheap to keep) DB connection right before the write phase begins. No connection is ever left idle for more than the gap between individual queries.

Also factored connection setup into a _connect() helper and moved connection-opening into each command branch (previously one connection wrapped all four commands, opened before argument dispatch even mattered) — schema/informal/vector-db are unaffected since they were already fast.

TCP keepalives (previous fix) did not prevent the connection drop --
same failure recurred at the same spot. Keepalives are invisible to
whatever is actually closing the connection (likely an application-level
idle policy on Heroku's side, not a network path issue), so they can't
help.

Split jixia_db.load_data into run_analysis() (pure CPU work, no DB
connection) and load_data() (DB writes only). database/__init__.py now
runs the 30-90+ minute analysis phase first, then opens a fresh
connection right before writing -- so no connection is ever left idle
for more than the time between individual queries.
Copilot AI review requested due to automatic review settings July 7, 2026 22:26
@Gabrielebattimelli Gabrielebattimelli merged commit 8449a02 into main Jul 7, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR restructures the jixia ingestion pipeline so the long, CPU-only jixia analysis phase runs before any PostgreSQL connection is opened, preventing idle-connection drops (observed on the first INSERT INTO module after analysis) during CI/Heroku runs.

Changes:

  • Split database.jixia_db.load_data into run_analysis(...) (CPU-only) and load_data(..., analysis, conn) (DB writes only).
  • Updated CLI dispatch to run analysis first, then open a DB connection only for the write phase.
  • Factored DB connection creation into a shared _connect() helper and moved connection opening into each command branch.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
database/jixia_db.py Introduces run_analysis() and refactors load_data() to consume precomputed analysis results rather than running jixia while holding a DB connection.
database/__init__.py Defers DB connection creation until just before DB-write phases; adds _connect() and updates command dispatch accordingly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants