Skip to content

feat(worker): add a procrastinate background worker - #601

Merged
aivong-openhands merged 4 commits into
mainfrom
jl/background-worker
Oct 5, 2026
Merged

aivong-openhands merged 4 commits into
mainfrom
jl/background-worker

Conversation

@jlav

@jlav jlav commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

  • A human has tested these changes.

AGENT:


Why

Part 3 of 5 in the sandbox lifecycle stack.

The sandbox lifecycle in part 5 needs periodic jobs that act across every user, and the app has nowhere to run them. procrastinate keeps its queue in the postgres the app already uses, so the worker needs no new service.

procrastinate and its tables are already on main (migration 174, #587). This PR only wires up the worker that runs its jobs. Part 4 adds the maintenance jobs and part 5 adds the sandbox jobs.

Summary

  • openhands/app_server/worker/ holds the app, the database connection and the python -m openhands.app_server.worker entrypoint.
  • make start-worker runs it, and make run and make run-saas now start it next to the backend.

Approach

Suggested reading order:

  1. worker/app.py. One module-level App. procrastinate binds each task to the app its blueprint is added to, so there's one per process. The real connection is swapped in when the worker runs (app.replace_connector).
  2. worker/__main__.py. Runs the worker with the app server's database settings. It exits when it can't reach the database. OH_WORKER_CONCURRENCY sets how many jobs run at once (default 10).
  3. worker/database.py. Builds procrastinate's PsycopgConnector from those settings. It always connects to DB_HOST. On Cloud SQL, that's a cloud-sql-proxy sidecar next to the worker.

Issue Number

How to Test

uv run pytest tests/unit/app_server/test_worker_database.py
make local-db
make start-worker    # logs worker.started

test_worker_database.py runs procrastinate against the test postgres. That includes LISTEN/NOTIFY, and reconnecting after postgres drops every connection.

Video/Screenshots

N/A

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • The worker runs as its own process. Its helm Deployment lives outside this repo and isn't written yet. On GCP, that Deployment needs a cloud-sql-proxy sidecar.

Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-ef1d9a6

@jlav
jlav added this pull request to stack #604 September 30, 2026 19:42
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ This PR contains migrations. Please synchronize before merging to prevent conflicts.

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  openhands/app_server/worker
  __main__.py 9-42
  app.py 3-22
  database.py
  openhands/db
  ssl.py
Project Total  

This report was generated by python-coverage-comment-action

@jlav jlav changed the title Add a procrastinate background worker feat(worker): add a procrastinate background worker Sep 30, 2026
@github-actions github-actions Bot added the type: feat A new feature label Sep 30, 2026
@jlav
jlav force-pushed the jl/background-worker branch from 52c1a18 to 1b04005 Compare September 30, 2026 21:43
@jlav
jlav force-pushed the jl/background-worker branch from 1b04005 to 994a500 Compare September 30, 2026 22:13
@jlav
jlav marked this pull request as ready for review September 30, 2026 22:15
@aivong-openhands

Copy link
Copy Markdown
Contributor

✅ Validation — PASS (Replicated smoke/no-regression + local worker functional)

Tested this PR two ways: the released image on a self-hosted Replicated embedded-cluster install, and the worker itself running locally (per the PR's "How to Test").

Replicated install (external Postgres)

  • Running openhands pod is on ghcr.io/openhands/enterprise-server:sha-f7e99ce — the image referenced in this PR.
  • alembic_version at 175; procrastinate tables present (procrastinate_events, procrastinate_jobs, procrastinate_periodic_defers, procrastinate_workers); stacked v1_remote_sandbox lifecycle columns intact.
  • Fresh install healthy (all pods Running), auth/login + TOS worked, and a test conversation completed — no regression.

⚠️ Scope note: as this PR states, the worker runs as its own process and its helm Deployment isn't written yet. Confirmed at runtime: there is no worker deployment/pod in the Replicated install — the image ships the worker code, but the chart doesn't launch python -m openhands.app_server.worker. So the Replicated run only covers image/schema/no-regression; the worker itself is validated locally below.

Local worker functional test (at f7e99ce)

  • Unit tests — 4 passed: uv run pytest tests/unit/app_server/test_worker_database.py (includes real-postgres LISTEN/NOTIFY and reconnect-after-server-drops-connections).
  • Worker runs: make local-db + make start-worker → the worker connects to postgres via the DB_HOST path and logs the expected startup:
    __main__.py:31 - worker.started
    Starting worker on all queues
    No periodic task found, periodic deferrer will not run.
    
    No periodic task found is expected — periodic/maintenance jobs come in parts 4–5, not this PR. Clean shutdown on SIGTERM.

Result: PASS — image ships and installs on Replicated with no regression and intact schema; the worker unit tests pass and the worker starts and runs locally.


This comment was prepared by an AI agent (OpenHands) on behalf of @aivong.

@aivong-openhands aivong-openhands left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Taste rating: 🟢 Good. No material findings.

I checked the worker app, connector and entrypoint against the installed procrastinate 3.10.0 (replace_connector, open_async, run_worker_async), and checked the Makefile wiring and the new psycopg SSL helper. The connector uses the same DbSessionInjector settings as the app server. Its SSL handling follows the existing asyncpg/pg8000 helpers, and it refuses to start with no host, the same way _require_host does. from_env(WorkerConfig, 'OH_WORKER') reads OH_WORKER_CONCURRENCY as described.

I ran test_worker_database.py and test_db_session_injector.py locally against real Postgres, and all 41 passed. The LISTEN/NOTIFY and reconnect tests are meaningful: polling is set to 60s and the job timeout to 20s, so they fail if NOTIFY or the reconnected LISTEN connection doesn't work. CI is green on f7e99ce.

Non-blocking question: The app server uses the Cloud SQL connector when GCP_DB_INSTANCE is set. The worker always connects to DB_HOST. If both are set in a GCP environment, the worker silently connects to whatever DB_HOST points at. That matches the documented cloud-sql-proxy sidecar design, and nothing deploys the worker yet. Still, the coming helm Deployment needs DB_HOST to point at the proxy. Alternatively, the worker could fail fast when GCP_DB_INSTANCE is set and DB_HOST isn't local.

[RISK ASSESSMENT]
[Overall PR] ⚠️ Risk Assessment: 🟡 MEDIUM
This PR adds a new process type, but the production deployment and proxy aren't written yet. The change only adds code: nothing in the app server depends on the worker, and no auth, secrets or schema code changes. Reviewer focus: connection-pool sizing (concurrency + 2) once real jobs land in parts 4–5, and how DB_HOST is set for GCP in the helm chart.

Verdict: APPROVE. Keeping the queue in the existing Postgres means no new infrastructure. The trade-off is that jobs now share the main database's connection budget, so pool sizing needs tuning as jobs are added.


This review was created by an AI agent (OpenHands) on behalf of @aivong-openhands.

@aivong-openhands
aivong-openhands force-pushed the jl/background-worker branch 2 times, most recently from 581e100 to 36fff18 Compare October 5, 2026 18:41
Base automatically changed from jl/docker-pause-stops-container to main October 5, 2026 19:25
@aivong-openhands
aivong-openhands merged commit adac20b into main Oct 5, 2026
20 of 27 checks passed
@aivong-openhands
aivong-openhands deleted the jl/background-worker branch October 5, 2026 22:20
@openhands-release-bot openhands-release-bot Bot added the released: 1.69.0 Shipped in 1.69.0 label Oct 6, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.69.0.

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

Labels

released: 1.69.0 Shipped in 1.69.0 type: feat A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants