Files
archie/internal/infrastructure/cronstore/doc.go
T
sam 37f7408474 feat(cronstore): durable job store with cross-process locking
Slice 1 of the cron/scheduling epic (archie-core-1786637496320-249-be6d9a20).
Implements the file-backed job store the scheduling ticker engine polls
through scheduling.JobSource. The engine applies no schedule arithmetic
of its own, so the store owns cron expressions, fixed intervals, and
catch-up windows — Slice 1 ships Interval fully, with Once accepted at
Create/fired at Due, and Cron reserved as a wire shape for Slice 2.

Files: per-profile JSON at <path>/jobs.json with a version-stamped
schema. CRUD by job id with merge-semantic Update (nil Patch fields are
preserved). Truncated/partial-write files return ErrCorruptFile; forward
schema versions return ErrUnsupportedSchema. DisallowUnknownFields means
additive shape changes must bump schemaVersion.

Concurrency: three locks stack in fixed order. (1) A package-level
mutex keyed by lock-file path serialises Stores within one process —
Linux flock is per file description, so two Store instances in the
same process have independent fds and would otherwise proceed in
parallel. (2) Cross-process flock serialises Stores across processes
(CLI vs daemon). (3) s.mu guards the in-memory mirror. Every
operation re-hydrates from disk before applying its change so a writer
that just acquired the locks sees every change the previous writer
committed. Atomic write via temp-file + rename + fsync for durability.

Validation: Create and Update validate Pool, Schedule, and required
fields up front so a hand-edited file cannot persist a job the engine
cannot reason about. MarkRun on a kind without next-run arithmetic
returns ErrScheduleUnsupported. Get/List return deep copies so caller
mutation cannot corrupt the in-memory mirror. Files are written 0o600
because payloads can carry operator-identifying data; SetEscapeHTML(false)
keeps operator-readable files human-friendly.
2026-08-28 22:28:49 +10:00

37 lines
1.7 KiB
Go

// Package cronstore is the file-backed job store that backs the scheduling
// ticker engine. It is infrastructure: it owns the on-disk layout, the
// cross-process lock, and the version-stamped JSON schema. It does not own
// timing semantics — those live in internal/domain/scheduling.
//
// Two surfaces:
//
// - scheduling.JobSource: Due(ctx, now) — what the engine consumes.
// - CRUD on JobSpec — what the CLI and dashboard consume.
//
// Both share the same Store value, the same lock, and the same on-disk
// file. The engine applies no schedule arithmetic of its own; whatever
// "due" means is the store's decision, including future kinds the store
// grows into.
//
// Concurrency model: every read and write acquires an OS-level flock on a
// sibling lock file, so a CLI edit and a daemon writer cannot tear the file.
// Conflict resolution is last-writer-wins: the second writer overwrites the
// first's whole document. The lock makes that serial rather than racy.
// In-process, two Store values for the same path are serialised by a
// package-level mutex keyed by lock-file path — flock is per file
// description on Linux and would not block them otherwise.
//
// File format (versioned; bump schemaVersion on ANY shape change
// including additive ones, since DisallowUnknownFields rejects them):
//
// {
// "schema_version": 1,
// "jobs": [ JobSpec, ... ]
// }
//
// JobSpec carries the engine-facing fields it always needed (ID, Pool,
// Detail) plus everything the store computes for it (NextRun, LastRun,
// Created, Updated). Merge semantics on Update mean a nil field in a Patch
// leaves the corresponding persisted field untouched.
package cronstore