about summary refs log tree commit diff
diff options
context:
space:
mode:
authorpi coding agent <pi@localhost>2026-10-22 17:35:31 +0000
committerFrederick Muriuki Muriithi <fredmanglis@gmail.com>2026-10-22 13:03:27 -0500
commit87b3864100d11d6e509eb704d1bddd637fe8807d (patch)
tree49181c4e8c2b941448e565c78a8f73f84b1b8c81
parente9dee56bce30ef50b5a3f25b49055c6b004e3297 (diff)
downloadgn-libs-87b3864100d11d6e509eb704d1bddd637fe8807d.tar.gz
docs: add background-jobs design notes and unification tasks
Add WIP notes for the shared background-jobs effort:

- design notes: current implementations across repos, target shared design in
  gn-libs, the multi-task job consideration, and open questions;
- tasks & todos: the major unification task plus a migration inventory of every
  place background scripts are created, launched and tracked, so the refactor
  does not miss a spot.
-rw-r--r--WIP/background-jobs-design-notes.md115
-rw-r--r--WIP/background-jobs-tasks-and-todos.md127
2 files changed, 242 insertions, 0 deletions
diff --git a/WIP/background-jobs-design-notes.md b/WIP/background-jobs-design-notes.md
new file mode 100644
index 0000000..cca3793
--- /dev/null
+++ b/WIP/background-jobs-design-notes.md
@@ -0,0 +1,115 @@
+# Background jobs — design notes
+
+- **Feature:** background jobs (cross-cutting, shared across repos)
+- **Last updated:** 2026-10-22
+- **Related:** [tasks & todos](background-jobs-tasks-and-todos.md) ·
+  genotypes upload docs in `gn-uploader/WIP/`
+- **Status:** living design. The unification work below is a **major, not yet
+  committed** undertaking.
+
+`gn_libs.jobs` is intended to become the single, shared background-jobs
+implementation for the GeneNetwork repositories. Today there are several
+independent implementations; this note records the target design, the current
+state, and the open questions. The migration checklist lives in
+[tasks & todos](background-jobs-tasks-and-todos.md).
+
+## Current state: several implementations
+
+| Repo | Storage | Create/queue | Runner/launcher | Notes |
+|---|---|---|---|---|
+| gn-libs | SQLite | `gn_libs/jobs/jobs.py` (`initialise_job`, `launch_job`) | `gn_libs/jobs/launcher.py` | Intended shared implementation; migrations via `init_app`. |
+| gn-uploader | Redis (legacy) **and** SQLite | legacy `uploader/jobs.py`; new `gn_libs.jobs` | legacy `scripts/worker.py`; new `gn_libs.jobs.launcher` | Mid-migration; both coexist. |
+| genenetwork2 | Redis (`gn2:jobs`) | `gn2/jobs/jobs.py` (`queue`, `run`) | `gn2/scripts/run_external.py` | Redis hash per job; debug blueprint. |
+| genenetwork3 | Redis (`GN3::JOBS` + command queue) | `gn3/commands.py` (`queue_cmd`, `run_async_cmd`); `gn3/jobs.py` (`create_job`) | `sheepdog/worker.py` | `gn3/jobs.py` appears unused — verify. |
+| genenetwork4 | Redis (mirror of gn3) | same files as gn3 | `sheepdog/worker.py` | Essentially duplicated from gn3. |
+| gn-auth | Redis | `gn_auth/jobs.py` (`create_job`); `gn_auth/commands.py` (`queue_cmd`, `run_async_cmd`) | `gn_auth/scripts/worker.py` | Similar to gn3 but diverged. |
+
+Consequences: duplicated status models, two storage backends (Redis vs SQLite),
+inconsistent metadata/output handling, and UI/status code repeated per repo.
+
+## Target design
+
+- **One implementation in `gn-libs`** for: job creation, storage, metadata,
+  status, std-stream capture, listing/expiry, and launching/running.
+- **Repos supply job _definitions_ and _handlers_**, not their own job engines.
+- A **single runner/launcher** contract, so every repo launches jobs the same
+  way (no per-repo `scripts/worker.py` / `run_external.py` copies).
+- Consistent status vocabulary and metadata keys across repos.
+- Backwards-compatible handling of existing jobs/external IDs where feasible.
+- **Move away from Redis:** the Redis-backed implementations are to be adapted
+  to the SQLite implementation for running jobs; `gn_libs.jobs` is the target.
+
+### Decisions
+
+- **Storage backend: SQLite.** We are moving away from Redis. `gn_libs.jobs`
+  (SQLite) is the target implementation; the existing Redis-backed
+  implementations must be adapted to use it for running jobs. No new
+  Redis-backed job code.
+
+### Open questions
+
+- **Process model:** subprocess-per-job (current) vs a resident worker/queue.
+- **Status vocabulary:** unify names (`queued`/`running`/`completed`/`error`/
+  `stopped` vs `pending`/`success`/...).
+- **External IDs:** how to preserve `jobs_by_external_id` semantics.
+- **Migration path:** per-repo, behind the existing APIs, to avoid a big-bang
+  change.
+
+## Multi-task jobs (design consideration)
+
+Today a background job is effectively **one script == one job**. Some features
+(e.g. genotype uploads) are one logical operation made of several dependent
+stages. Splitting them into unrelated single-script jobs loses the relationship
+between steps and makes gating/status awkward.
+
+### Option A — keep "one script == one job"
+
+- Each stage is its own job; the client orchestrates the sequence.
+- ✅ No change to the job model; smallest near-term risk.
+- ❌ Several unrelated jobs for one operation.
+- ❌ Readiness/success gating duplicated in clients.
+- ❌ Coherent per-operation status is hard.
+
+### Option B — one job containing multiple tasks
+
+- A job owns tasks; each task has a status; the job tracks overall progress.
+- ✅ Natural for multi-stage operations.
+- ✅ Central readiness/gating and per-task success.
+- ✅ Better user-visible status (e.g. "collect info done, awaiting input").
+- ❌ Rework of storage schema, runner, handlers and UI.
+- ❌ Scope creep.
+
+### Recommendation
+
+Do not couple this to any single feature. Treat it as part of the shared-jobs
+effort, and in the meantime allow Option A (or a thin orchestration layer) so
+features are not blocked. If adopted, a task model would need: task statuses
+(`pending`/`ready`/`running`/`success`/`error`/blocked), dependency gating,
+per-task outputs passed to later tasks, derived overall status, and
+retry/"fix and resume" (lower priority).
+
+## Inventory of launch/tracking points
+
+The detailed, migration-oriented checklist (with file paths) lives in
+[tasks & todos](background-jobs-tasks-and-todos.md#migration-inventory-do-not-miss-any-spot). Keeping
+it there means the refactor has one checklist of places not to miss. Broadly,
+the points are:
+
+- **gn-uploader:** `uploader/jobs.py` + `scripts/worker.py` (legacy) and
+  `uploader/background_jobs.py` + `gn_libs.jobs` (new); launch sites in
+  `uploader/population/rqtl2.py`, `uploader/phenotypes/views.py`,
+  `uploader/expression_data/{views,dbinsert}.py`, `uploader/samples/views.py`;
+  worker-side status imports in several `scripts/`.
+- **genenetwork2:** `gn2/jobs/jobs.py`, `gn2/scripts/run_external.py`, launch at
+  `gn2/wqflask/views.py`, tracking UI in `gn2/wqflask/api/jobs.py` and
+  `gn2/wqflask/templates/jobs/`.
+- **genenetwork3/4:** `gn3/commands.py` + `sheepdog/worker.py`; launch sites in
+  `gn3/api/gemma.py` and `gn3/api/correlation.py`; legacy `gn3/jobs.py`.
+- **gn-auth:** `gn_auth/jobs.py`, `gn_auth/commands.py`,
+  `gn_auth/scripts/worker.py`; launch at
+  `gn_auth/auth/authorisation/data/views.py`; `gn_auth/scripts/search_phenotypes.py`.
+
+## Related reading
+
+- `gn-uploader/docs/dev/background_jobs.md` — earlier notes on per-user jobs,
+  multiprocessing within jobs, and fetching job status.
diff --git a/WIP/background-jobs-tasks-and-todos.md b/WIP/background-jobs-tasks-and-todos.md
new file mode 100644
index 0000000..f2e405e
--- /dev/null
+++ b/WIP/background-jobs-tasks-and-todos.md
@@ -0,0 +1,127 @@
+# Background jobs — tasks & todos
+
+- **Feature:** background jobs (cross-cutting, shared across repos)
+- **Last updated:** 2026-10-22
+- **Related:** [design notes](background-jobs-design-notes.md)
+- **Legend:** ✅ done · 🚧 in progress · ⬜ todo · 💭 consideration (not committed)
+
+The single place to track the background-jobs work so nothing falls through the
+cracks. Design rationale is in the [design notes](background-jobs-design-notes.md).
+
+## Major task: unify background jobs into a shared `gn-libs` implementation
+
+There are currently multiple, diverging background-job implementations across
+`gn-libs`, `gn-uploader`, `genenetwork2`, `genenetwork3`, `genenetwork4` and
+`gn-auth`. Goal: **one shared implementation in `gn-libs`**, with each repo
+supplying only job definitions/handlers.
+
+- ⬜ Adapt each Redis-backed implementation (gn2, gn3/gn4, gn-auth, gn-uploader
+  legacy) to the SQLite implementation for running jobs.
+- ⬜ Define the shared API (create/initialise, launch, status, metadata,
+  stdout/stderr, list/expiry, external IDs).
+- ⬜ Define a single runner/launcher contract and retire per-repo workers.
+- ⬜ Unify the status vocabulary and metadata keys.
+- ⬜ Provide a compatibility/migration path (per repo, behind existing APIs).
+- ⬜ Tests for the shared implementation.
+- ⬜ Update this doc and the design notes as decisions are made.
+
+## Migration inventory (do not miss any spot)
+
+Paths and line numbers are a snapshot and may drift; re-grep before starting a
+repo. For each implementation the relevant pieces are **create/queue**,
+**runner/launcher**, **launch sites**, and **status/tracking (incl. UI)**.
+
+### gn-libs (target implementation)
+
+- ✅ `gn_libs/jobs/jobs.py` — SQLite storage, metadata, streams, expiry,
+  external IDs (`initialise_job`, `launch_job`, `job`, `delete_job(s)`,
+  `jobs_by_external_id`, ...).
+- ✅ `gn_libs/jobs/launcher.py` — runner (`run_job`, `subprocess.Popen`,
+  streams to DB).
+- ✅ `gn_libs/jobs/migrations.py` + `init_app` — schema migrations.
+- ⬜ Extend for multi-task jobs (if adopted) and any missing features from the
+  other implementations (e.g. stop/kill, external IDs parity).
+
+### gn-uploader (mid-migration: Redis legacy + SQLite new)
+
+Legacy Redis system:
+
+- ⬜ `uploader/jobs.py` — `initialise_job`, `launch_job`, `job`, status updates.
+- ⬜ `scripts/worker.py` — runner (`run_job`).
+- ⬜ Launch sites:
+  - `uploader/population/rqtl2.py` (~lines 170, 866)
+  - `uploader/phenotypes/views.py` (~line 418)
+  - `uploader/expression_data/views.py` (~line 238)
+  - `uploader/expression_data/dbinsert.py` (~line 374)
+- ⬜ Worker-side status/output imports (old API):
+  - `scripts/validate_file.py`, `scripts/process_rqtl2_bundle.py`,
+    `scripts/qc_on_rqtl2_bundle.py`, `scripts/rqtl2/entry.py`,
+    `scripts/worker.py`.
+- ⬜ Old templates: `uploader/templates/jobs/*`,
+  `uploader/templates/phenotypes/job-status.html`,
+  `uploader/templates/populations/rqtl2/*job*`,
+  `uploader/templates/expression-data/job-progress.html`.
+
+New SQLite system (already on `gn_libs.jobs`):
+
+- ✅ `uploader/__init__.py` — `gnlibs_jobs.init_app(app)`.
+- ✅ `uploader/background_jobs.py` — handlers/blueprint on `gn_libs.jobs`.
+- ✅ `uploader/phenotypes/views.py` — several jobs via `gnlibs_jobs`
+  (~lines 706, 731, 1016, 1099, 1180, 1280, 1306).
+- ✅ `uploader/samples/views.py` — `gn_libs.jobs` (~line 160).
+- ✅ Templates: `uploader/templates/background-jobs/*`.
+- ⬜ Remove the legacy Redis system once all call sites are migrated.
+
+### genenetwork2
+
+- ⬜ `gn2/jobs/jobs.py` — Redis (`gn2:jobs`): `queue`, `run`, `job`, `status`,
+  `command`, `completed_successfully/erroneously`.
+- ⬜ `gn2/scripts/run_external.py` — runner (`run_job`) + status/stdout/stderr
+  updates.
+- ⬜ Launch site: `gn2/wqflask/views.py` (~line 1648: `jobs.queue` + `jobs.run`).
+- ⬜ Tracking/UI: `gn2/wqflask/api/jobs.py` (debug blueprint),
+  `gn2/wqflask/templates/jobs/*`, status checks in `gn2/wqflask/views.py`.
+- ⬜ `gn2/wqflask/oauth2/data.py` imports `gn2.jobs` — verify/remove.
+
+### genenetwork3 and genenetwork4 (mirrors)
+
+- ⬜ `gn3/commands.py` — `queue_cmd` (Redis queue + hash), `run_async_cmd`
+  (`subprocess.Popen` of `sheepdog.worker`).
+- ⬜ `sheepdog/worker.py` — `run_jobs` runner (status/result updates).
+- ⬜ Launch sites: `gn3/api/gemma.py` (many `queue_cmd` calls),
+  `gn3/api/correlation.py` (`run_async_cmd`).
+- ⬜ `gn3/jobs.py` — `create_job`/`job` (Redis `GN3::JOBS`); **no callers
+  found — verify and remove/absorb.**
+- ⬜ Apply the same changes to `genenetwork4` (files are effectively duplicated).
+
+### gn-auth
+
+- ⬜ `gn_auth/jobs.py` — `create_job`/`job` (Redis).
+- ⬜ `gn_auth/commands.py` — `queue_cmd`, `run_async_cmd`
+  (`subprocess.Popen` of `scripts.worker`).
+- ⬜ `gn_auth/scripts/worker.py` — `run_jobs` runner.
+- ⬜ Launch/track: `gn_auth/auth/authorisation/data/views.py` (~line 254:
+  `jobs.create_job` + `run_async_cmd`).
+- ⬜ `gn_auth/scripts/search_phenotypes.py` — uses `jobs.job_key` (migrate).
+
+### Consumers/tests to update
+
+- ⬜ Any job-status UI/endpoints and tests that reference the old namespaces or
+  status values (search for `jobs`, `job_key`, `GN3::JOBS`, `gn2:jobs`,
+  `JOBS_PREFIX`, `background-jobs`).
+- ⬜ `gn-integration-tests` and other test suites that exercise background jobs.
+
+## Multi-task jobs
+
+- 💭 Decide whether to adopt one job → many gated tasks (see
+  [design notes](background-jobs-design-notes.md#multi-task-jobs-design-consideration)).
+  **Not decided; do not block feature work on it.**
+- 💭 (Lower priority) retry/"fix and resume" a failed task.
+
+## Documentation
+
+- ⬜ Move/merge the genotypes-upload background-jobs consideration from
+  `gn-uploader/WIP/` into these gn-libs docs (done in this revision) and update
+  the gn-uploader cross-links to point here.
+- ⬜ Reconcile with `gn-uploader/docs/dev/background_jobs.md` (older notes).
+- ⬜ Record the final storage/API decisions here as they are made.