Repository navigation
Description
Analysis baseline: main @ 3ec3455 — 9 November 2026.
Priority: P2
Context / Current Behavior
A shared startWorkerLifecycle helper already delays writers during restore, handles shutdown during dynamic import and collects worker stop promises. It is a useful safety boundary, not a worker recovery supervisor. Its current contract has start() and asynchronous stop(), without identity, common cancellation or lifecycle health.
Workers differ intentionally: proxy/CrowdSec reconciliation serializes applies and retries; certificate event synchronization persists receipts and cursor atomically; certificate binding jobs claim up to four jobs with 90-second leases; backup outcomes already forwards cancellation and has a five-second stop bound. Several other stops await indefinitely. The web server force-exit guard is 18 seconds, while reconciler stop waits can be 25/60 seconds. Clearing a timer or winning a timeout race does not cancel an existing write.
Problem Statement
Startup failures have no common retry/reporting path, stopped factories have different restart semantics, and shutdown/restore guarantees are difficult to assess across workers. A long dependency call or late callback can outlive a nominal stop. Shared drift checks can overlap even though apply work is serialized. Replacing all workers with a framework would risk their existing durable workflows.
Proposed Architecture / Solution
Extend the smallest useful shared lifecycle contract: stable identity, explicit states, bounded/coalesced wakeups, transient-start retry, cancellation and an honest stop result. Reuse proven behavior from existing factories. Keep lease recovery, permissions, cursor commits and domain state machines with their owners.
Separate ordinary process shutdown, temporary non-destructive pause and destructive restore fencing. The old web process must remain permanently quiesced after destructive restore quiescence and database-close acknowledgement; preparation/validation failures before that boundary may release the temporary fence. A generic resume/restart must not bypass state replacement and readiness confirmation.
Implementation Scope
- Inventory every registration and intentional exclusion: Valkey subscriber, maintenance watcher, WebSocket timers and root backup agent have different owners.
- Classify initialization/execution failures as retryable, terminal or maintenance-blocked; bound retry/backoff and avoid duplicate timers/listeners after retry.
- Propagate AbortSignal/deadlines through supported controller/network operations. Specify how uncancellable database transactions are awaited or fenced.
- Align internal draining with the existing process/appliance shutdown budgets; report timeout separately from confirmed cancellation or completion.
- Make repeated start/stop behavior explicit per factory. Do not restart a permanently stopped instance or a worker during destructive maintenance.
- Make drift observation single-flight with bounded coalescing.
- Preserve claim cleanup and idempotent recovery for interrupted certificate jobs, event/audit consumers and backup outcomes. Expired DB authority does not prove a controller operation never happened.
- Surface persistent failures currently swallowed by job/cleanup catches through bounded status/logging shared with Introduce Comprehensive Background Worker Observability #396.
Security and Reliability Considerations
Keep the root maintenance watcher available while writers stop. Preserve writer quiescence, DB closure, maintenance acknowledgement, restored-key reload and confirmed restart ordering. No failed drain may be reported as a safe restore checkpoint. Preserve reauthorization before certificate binding, revision checks, token/expiry fences, atomic audit/cursor transactions and controller operation idempotency. No new supervisor may grant database or root privileges to another runtime.
Acceptance Criteria
- A worker whose import/start/first dependency call fails transiently recovers once; permanent failure remains visible without a tight loop.
- Shutdown during loading, retry sleep, network execution or persistence prevents later admission and duplicate startup.
- Each migrated worker has explicit cancellation/drain bounds within the process budget and distinguishes stopped, cancelled, timed out and failed states.
- Repeated start/stop and triggers during a running pass leave at most one admitted pass plus bounded follow-up work; stalled drift checks do not accumulate.
- Restore keeps writers fenced through interrupted/failed destructive phases; no automatic resume in the old process.
- Stale leases/generations cannot commit late state, and interrupted native side effects are reconciled idempotently on recovery.
- Existing specific worker semantics remain covered; shared abstractions reduce duplicated lifecycle logic without absorbing domain workflow code.
Testing Requirements
Extend lifecycle loading/shutdown-race tests, reconciler/CrowdSec tests, certificate job/event database tests, backup-outcome tests, restore fence/runtime tests and the real web shutdown boundary tests. Inject hanging dependencies, failed abort/drain, late initialization, lease expiry and stop errors. Add short cross-process maintenance/restart cases through #397. Tests must assert actual writer quiescence, not only that a timeout promise resolved.
Dependencies / Related Issues
Use the existing worker implementations rather than reopening #12/#81/#78. #401 records ownership and intentional lifecycle exclusions; #396 defines shared status vocabulary; #397 adds fault fixtures incrementally. This issue owns lifecycle/cancellation conventions. #400 owns scheduling/resource policy, and #403 owns appliance process restart/fault isolation and consumes this issue's maintenance/drain contract. These boundaries avoid a new global job framework.
Relevant Source Files
- web/src/start.ts
- web/src/server/Foundation/worker-lifecycle.server.ts
- web/src/server/Foundation/Types/worker-lifecycle.types.ts
- web/src/server/Foundation/runtime-reconciler.server.ts
- web/src/server/Admin/CertificateManagement/certificate-events.worker.ts
- web/src/server/Admin/ProxyHostManagement/certificate-jobs.worker.server.ts
- web/src/server/Admin/Backups/backup-outcomes.worker.ts
- web/src/server/Backups/restore-maintenance.server.ts
- docker/web/serve.ts
- web/src/tests/server/Foundation/worker-lifecycle.test.ts
- web/src/tests/server/Admin/ProxyHostManagement/certificate-jobs-worker.db.test.ts
- web/src/tests/server/Admin/Backups/backup-outcomes.test.ts
- web/src/tests/server/Backups/restore-maintenance-fence.test.ts
- web/src/tests/tooling/docker/web/serve-runtime.coverage.test.ts
Activity