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.
34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
package cronstore
|
|
|
|
import "sync"
|
|
|
|
// processLocks serialises Stores within one process. It exists because
|
|
// Linux flock is per file description, not per inode: two Store
|
|
// instances opening the same jobs.json.lock each get their own fd and
|
|
// their own independent flock state, so they would not block each
|
|
// other inside a single process. The cross-process lock handles the
|
|
// CLI-vs-daemon case; this map handles the in-process case.
|
|
//
|
|
// Keyed by absolute lock-file path so two unrelated cronstores in the
|
|
// same process do not contend. Entries are created lazily and live for
|
|
// the life of the process; the map is append-only and small in
|
|
// practice (one entry per distinct config dir).
|
|
var (
|
|
processLocksMu sync.Mutex
|
|
processLocks = map[string]*sync.Mutex{}
|
|
)
|
|
|
|
// processLockFor returns the per-lock-path mutex, creating it on first
|
|
// use. Callers must hold it for the duration of an operation that has
|
|
// already acquired the cross-process flock.
|
|
func processLockFor(path string) *sync.Mutex {
|
|
processLocksMu.Lock()
|
|
defer processLocksMu.Unlock()
|
|
if m, ok := processLocks[path]; ok {
|
|
return m
|
|
}
|
|
m := &sync.Mutex{}
|
|
processLocks[path] = m
|
|
return m
|
|
}
|