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.
This commit is contained in:
1 parent
b5ca9c8867
commit
37f7408474
10 files changed
+2000
-1
No files matched your search
+1
-1
@@ -83,7 +83,7 @@
|
||||
{"_type":"issue","id":"archie-core-1786637496320-249-be6d9a20.4","title":"Cron lifecycle guard: reject dangerous commands at job creation","description":"Slice 4 of the cron/scheduling epic (archie-core-1786637496320-249-be6d9a20). The epic's third acceptance criterion verbatim: 'Dangerous commands are rejected at creation time.'\n\nGuards both runners from the previous slice: ChatCourier (whose body could include shell-shaped payloads) and WorkflowTask (whose shell-step is a documented footgun). Rejection happens at job creation, not at tick time, so a refused job is visible in the store with a refusal reason and never reaches the ticker engine.","acceptance_criteria":"- Rejects job specs whose target body includes any of: `archied` restart, `systemctl`, `launchctl`, `pkill`, `mkfs`, `dd`, `shutdown`, `reboot`.\n- Rejection reason is recorded on the refused job and surfaced through the gateway / dashboard; the job is not written to the store.\n- One named rejection function both the CLI and gateway paths call, so adding a new dangerous token does not require touching every UI surface.\n- False positives are deliberate — the list over-broadens then narrows by operator feedback. Cost of accepting a dangerous command vastly exceeds the cost of a refused one.","status":"open","priority":2,"issue_type":"feature","owner":"sam@catlow.tech","created_at":"2026-08-27T14:34:40Z","created_by":"Sam Catlow","updated_at":"2026-08-27T14:34:40Z","labels":["enhancement"],"dependencies":[{"issue_id":"archie-core-1786637496320-249-be6d9a20.4","depends_on_id":"archie-core-1786637496320-249-be6d9a20","type":"parent-child","created_at":"2026-08-27T14:34:40Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-1786637496320-249-be6d9a20.4","depends_on_id":"archie-core-1786637496320-249-be6d9a20.1","type":"blocks","created_at":"2026-08-27T14:35:44Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":1,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-1786637496320-249-be6d9a20.3","title":"Cron blueprints: parameterised templates with typed slots and form schemas","description":"Slice 3 of the cron/scheduling epic (archie-core-1786637496320-249-be6d9a20). The authoring surface: predefined templates with typed slots (time, enum, text, weekdays — each with their validators) that gate the gateway and CLI creation forms.\n\nDaily status summary is the headline blueprint. An author without a blueprint can still create a job directly, but the gateway form is the recommended surface for non-trivial jobs.","acceptance_criteria":"- A small registry of built-in blueprints (daily-status-summary at minimum; weekday-pickable and time-pickable are sufficient for headline coverage) exposed to gateway and CLI form renderers.\n- Typed slot definitions (`time`, `enum`, `text`, `weekdays`) with a generated form schema suitable for the gateway chat-form surface.\n- Template renderer fills slots from a validated values map; missing or wrong-typed slots reject the job at creation, not at next tick.\n- Adding a new blueprint is data, not code, and the gateway / CLI form reflect added blueprints without a rebuild.","status":"open","priority":2,"issue_type":"feature","owner":"sam@catlow.tech","created_at":"2026-08-27T14:34:38Z","created_by":"Sam Catlow","updated_at":"2026-08-27T14:34:38Z","labels":["enhancement"],"dependencies":[{"issue_id":"archie-core-1786637496320-249-be6d9a20.3","depends_on_id":"archie-core-1786637496320-249-be6d9a20","type":"parent-child","created_at":"2026-08-27T14:34:38Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-1786637496320-249-be6d9a20.3","depends_on_id":"archie-core-1786637496320-249-be6d9a20.1","type":"blocks","created_at":"2026-08-27T14:35:42Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-1786637496320-249-be6d9a20.2","title":"Cron delivery runner + router: dispatch due jobs to chat/task targets","description":"Slice 2 of the cron/scheduling epic (archie-core-1786637496320-249-be6d9a20). Implements `scheduling.Runner` and the router that picks one runner per job kind.\n\nWithout this slice, the ticker engine has no work to dispatch. Concrete target types to ship: ChatCourier (sends a templated message through the existing channel-agnostic `channels.Channel` gateway, so the channel choice stays the operator's, not the cron's) and WorkflowTask (submits a `workintake` envelope so the existing task lifecycle owns scheduling, retries, gates, and PR opening).\n\nJob store is a hard prerequisite. Composition wiring and lifecycle guard block the wiring slice, not this one.","acceptance_criteria":"- At least two `Runner` implementations: `ChatCourier` (renders the job's templated body through the channel layer) and `WorkflowTask` (submits a `workintake` envelope carrying the cron identity and body so the existing workflow domain runs the rest).\n- A `Router` that maps `Job.Kind` to a Runner; an unrecognised kind emits `KindJobError` `phase=\"dispatch\"` and is otherwise a no-op — refused at the gate, not silently dropped.\n- Every Runner honours `ctx.Done()` so the engine's bounded shutdown actually drains in flight.\n- The epic's headline scenario ('daily status summary') is exercisable end-to-end through the ChatCourier runner with a hard-coded template; the blueprints slice replaces this with parameterisation.","status":"open","priority":2,"issue_type":"feature","owner":"sam@catlow.tech","created_at":"2026-08-27T14:34:35Z","created_by":"Sam Catlow","updated_at":"2026-08-27T14:34:35Z","labels":["enhancement"],"dependencies":[{"issue_id":"archie-core-1786637496320-249-be6d9a20.2","depends_on_id":"archie-core-1786637496320-249-be6d9a20","type":"parent-child","created_at":"2026-08-27T14:34:35Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-1786637496320-249-be6d9a20.2","depends_on_id":"archie-core-1786637496320-249-be6d9a20.1","type":"blocks","created_at":"2026-08-27T14:35:43Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":1,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-1786637496320-249-be6d9a20.1","title":"Cron job store: per-profile JSON with cross-process file locking","description":"Slice 1 of the cron/scheduling epic (archie-core-1786637496320-249-be6d9a20). Implements the durable job store: one JSON file per profile at the conventional path, CRUD operations, and cross-process file locking so a CLI edit and a daemon writer don't corrupt each other.\n\nThe ticker engine (internal/domain/scheduling) consumes this through its `JobSource` interface. The store's only contract with the engine is `Due(ctx, now) -\u003e ([]Job, error)` — the engine applies no schedule arithmetic of its own, so the store owns cron expressions, fixed intervals, and catch-up windows.","acceptance_criteria":"- One JSON file at the conventional per-profile path (`\u003cconfig_dir\u003e/cron/jobs.json` or equivalent). Schema is durable and version-stamped for forward compatibility.\n- Create / read / update / delete by job ID with merge semantics that preserve fields not in the patch.\n- Cross-process file lock (`flock` or OS equivalent) makes concurrent CLI+daemon writers serial; last-writer-wins is documented as the conflict policy.\n- Implements `scheduling.JobSource` so the ticker engine can poll it without knowing the file layout or format.\n- A truncated/partial-write file is not a panic: open returns an explicit error the engine surfaces as `phase=\"source\" KindJobError` rather than crashing the loop.","status":"open","priority":2,"issue_type":"feature","owner":"sam@catlow.tech","created_at":"2026-08-27T14:34:32Z","created_by":"Sam Catlow","updated_at":"2026-08-27T14:34:32Z","labels":["enhancement"],"dependencies":[{"issue_id":"archie-core-1786637496320-249-be6d9a20.1","depends_on_id":"archie-core-1786637496320-249-be6d9a20","type":"parent-child","created_at":"2026-08-27T14:34:32Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":4,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-1786637496320-249-be6d9a20.1","title":"Cron job store: per-profile JSON with cross-process file locking","description":"Slice 1 of the cron/scheduling epic (archie-core-1786637496320-249-be6d9a20). Implements the durable job store: one JSON file per profile at the conventional path, CRUD operations, and cross-process file locking so a CLI edit and a daemon writer don't corrupt each other.\n\nThe ticker engine (internal/domain/scheduling) consumes this through its `JobSource` interface. The store's only contract with the engine is `Due(ctx, now) -\u003e ([]Job, error)` — the engine applies no schedule arithmetic of its own, so the store owns cron expressions, fixed intervals, and catch-up windows.","acceptance_criteria":"- One JSON file at the conventional per-profile path (`\u003cconfig_dir\u003e/cron/jobs.json` or equivalent). Schema is durable and version-stamped for forward compatibility.\n- Create / read / update / delete by job ID with merge semantics that preserve fields not in the patch.\n- Cross-process file lock (`flock` or OS equivalent) makes concurrent CLI+daemon writers serial; last-writer-wins is documented as the conflict policy.\n- Implements `scheduling.JobSource` so the ticker engine can poll it without knowing the file layout or format.\n- A truncated/partial-write file is not a panic: open returns an explicit error the engine surfaces as `phase=\"source\" KindJobError` rather than crashing the loop.","status":"in_progress","priority":2,"issue_type":"feature","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-27T14:34:32Z","created_by":"Sam Catlow","updated_at":"2026-08-28T10:36:06Z","started_at":"2026-08-28T10:36:06Z","lease_expires_at":"2026-08-28T10:41:06Z","heartbeat_at":"2026-08-28T10:36:06Z","labels":["enhancement"],"dependencies":[{"issue_id":"archie-core-1786637496320-249-be6d9a20.1","depends_on_id":"archie-core-1786637496320-249-be6d9a20","type":"parent-child","created_at":"2026-08-27T14:34:32Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":4,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-h019.8","title":"Operator-triggered PR review with selectable reviewer model","description":"Expose the reviewer as a channel-neutral application capability that can review an existing pull request on operator request. The Web UI should offer a Review PR action as one adapter; chat/Telegram and future channels must invoke the same command rather than duplicating review behavior. The operator can configure or select the reviewer model independently from planner/builder models. Reuse the reviewer epic's fresh-context, read-only snapshot, structured findings, calibration, and visibility contracts.","acceptance_criteria":"A channel-neutral review command accepts a repository and PR identity, launches the approved isolated reviewer path, and returns/persists structured findings; Web UI exposes Review PR; at least one non-Web-UI channel can invoke the same command; reviewer model is configurable independently with a safe default; authorization and duplicate/concurrent review behavior are deterministic; findings and review status are visible without requiring raw logs.","status":"open","priority":2,"issue_type":"feature","owner":"sam@catlow.tech","created_at":"2026-08-24T18:08:58Z","created_by":"Sam Catlow","updated_at":"2026-08-24T18:09:03Z","external_ref":"https://github.com/samcharles93/archie-core/issues/636","dependencies":[{"issue_id":"archie-core-h019.8","depends_on_id":"archie-core-h019","type":"parent-child","created_at":"2026-08-24T18:08:58Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-atv2","title":"liveReply fallback lines appended after finalize are never rendered","description":"A media delivery that fails AFTER doFinalize has run appends its fallback line to l.toolLines and calls requestRender(), but stopRendering() has already closed renderDone and the renderer goroutine has exited. requestRender is a non-blocking send into a cap-1 channel with no consumer, so the line is dropped and the finalized Telegram message stands unchanged.\n\nMedia is fire-and-forget with a 30s budget while the turn's text finishes far earlier, so this is the NORMAL case for a large upload: a 40MB document that 400s or times out reports 'could not send x' into a slice nothing reads. Pre-dates the local-file work (URL fallbacks have the same hole) but archie-core-absb's guarantee that non-delivery is always visible depends on it.\n\nFound by adversarial review of 9a45fbb. internal/channels/telegram/live.go:419-437 with :498-532.\n\n## Acceptance Criteria\nA media failure occurring after the live reply has finalized still reaches the user -- e.g. as its own follow-up message rather than an edit to a message nothing re-renders. Covered by a test that finalizes first and fails the send second.","status":"open","priority":2,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-24T15:54:42Z","created_by":"Sam Catlow","updated_at":"2026-08-24T19:49:41Z","external_ref":"https://github.com/samcharles93/archie-core/issues/646","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-h3lg","title":"Dashboard swallows failed operator actions, showing nothing in the UI","description":"When an operator action fails, the dashboard surfaces nothing. Pressing Stop on a running task issued POST /api/tasks/3/action, received 403, and the only evidence anywhere was a browser console line: \"Failed to load resource: the server responded with a status of 403\". The page itself showed no error, no toast, no change of state -- the button simply appeared to do nothing.\n\nThis is how archie-core-440c (every dashboard mutation 403s behind a TLS-terminating reverse proxy) went unnoticed: the operator pressed Stop, saw nothing happen, and reasonably concluded the task was unstoppable rather than that the request had been refused. The bug was only found by opening devtools.\n\nConfirmed live 2026-08-25 while signed in to the production dashboard.\n\n## Why this is its own bead\n\n440c fixes the 403. This bead fixes the fact that ANY failed action is invisible -- a 500, a store failure, a refused state transition, an expired session -- and each of those has its own way of going wrong. Fixing only 440c leaves the next failure equally silent.\n\nNote internal/webui/api_tasks.go already returns distinguishable, human-readable failures (\"missing CSRF header\", \"cross-origin mutation refused\", \"runtime task control is unavailable\"). The server side is doing its job; the client discards it.\n\n## What is needed\n\n- A failed action tells the operator it failed, in the UI, with the server's reason where one is given.\n- The distinction that matters most: refused (403, bad state, not permitted) versus broken (5xx, network). The operator's next move differs -- the first is \"you cannot do that\", the second is \"try again or check the daemon\".\n- A 401 or an expired forward-auth session should prompt re-authentication rather than reading as a generic failure, since the deployment sits behind Authelia.\n- The optimistic UI must not show an action as applied when it was refused.\n\nPer CLAUDE.md the frontend is one folder per feature under ui/src/\u003cfeature\u003e with shared primitives in ui/src/base -- error surfacing used by more than one feature belongs in base, extracted on the SECOND real consumer, not in anticipation. Remember ui/dist is committed: run `task ui` and commit the rebuilt output in the same change.\n\n## Steps to Reproduce\n\n1. Load the dashboard in a deployment where mutations fail (any 440c-affected proxied deployment reproduces it directly).\n2. Press Stop, Retry, Abandon or Archive on a task.\n3. Observe the request fails in devtools while the page reports nothing.\n\n## Acceptance Criteria\n\nA failed operator action is reported in the dashboard with the server's reason where supplied; refused and broken failures are distinguishable to the operator; an authentication failure prompts re-auth rather than a generic error; no optimistic state change survives a failed request; dashboard tests cover a refused action, a server error and a network failure; ui/dist is rebuilt and committed.\n","status":"open","priority":2,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-24T15:49:18Z","created_by":"Sam Catlow","updated_at":"2026-08-24T15:50:15Z","external_ref":"https://github.com/samcharles93/archie-core/issues/629","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
// 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
|
||||
@@ -0,0 +1,39 @@
|
||||
package cronstore
|
||||
|
||||
import "errors"
|
||||
|
||||
// Sentinel errors callers can match with errors.Is.
|
||||
var (
|
||||
// ErrUnsupportedSchema is returned by Open when the persisted file
|
||||
// declares a schema version this build does not understand. The
|
||||
// store treats that as an explicit refusal to read: a forward
|
||||
// version may have shape we cannot safely render back out, and
|
||||
// silently downgrading would lose data.
|
||||
ErrUnsupportedSchema = errors.New("cronstore: unsupported schema version")
|
||||
|
||||
// ErrCorruptFile is returned by Open when the file exists but does
|
||||
// not decode as a valid envelope: truncated, garbage, or fields of
|
||||
// the wrong shape. The store never panics on bad input, so the
|
||||
// engine can surface this as phase="source" KindJobError.
|
||||
ErrCorruptFile = errors.New("cronstore: corrupt file")
|
||||
|
||||
// ErrJobExists is returned by Create when the requested id is
|
||||
// already present. Callers that want upsert semantics should
|
||||
// follow up with Update.
|
||||
ErrJobExists = errors.New("cronstore: job already exists")
|
||||
|
||||
// ErrJobNotFound is returned by Get, Update and MarkRun when the
|
||||
// id is absent. Delete is the exception: it is idempotent.
|
||||
ErrJobNotFound = errors.New("cronstore: job not found")
|
||||
|
||||
// ErrInvalidSpec is returned by Create and Update when the supplied
|
||||
// JobSpec / Patch fails the store's structural validation (empty id,
|
||||
// unknown schedule kind, non-positive interval).
|
||||
ErrInvalidSpec = errors.New("cronstore: invalid job spec")
|
||||
|
||||
// ErrScheduleUnsupported is returned by MarkRun when the job's
|
||||
// schedule kind cannot compute its next run time. Slice 1 only
|
||||
// knows interval arithmetic; cron / once return this so callers
|
||||
// can surface a clear error rather than silently advancing.
|
||||
ErrScheduleUnsupported = errors.New("cronstore: schedule kind not implemented")
|
||||
)
|
||||
@@ -0,0 +1,68 @@
|
||||
//go:build unix
|
||||
|
||||
package cronstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// lockedFile is an OS-level flock wrapper around a sidecar file. The
|
||||
// store acquires it before reading or writing the JSON, so a CLI edit
|
||||
// and a daemon writer cannot tear the same jobs.json.
|
||||
//
|
||||
// flock is per-open-file-description, which means a single Store holds
|
||||
// the lock for as long as it lives; concurrent processes opening the
|
||||
// same sidecar queue behind it.
|
||||
type lockedFile struct {
|
||||
f *os.File
|
||||
}
|
||||
|
||||
// openLock creates (or opens) the lock file and returns it unlocked.
|
||||
// The lock is acquired separately via Lock, so Open can hydrate without
|
||||
// holding it.
|
||||
func openLock(path string) (*lockedFile, error) {
|
||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o644)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open lock file %s: %w", path, err)
|
||||
}
|
||||
return &lockedFile{f: f}, nil
|
||||
}
|
||||
|
||||
// Lock blocks until the OS-level exclusive flock is acquired. flock is
|
||||
// reentrant on Linux per-fd, so calling Lock twice on the same lockedFile
|
||||
// from the same goroutine is fine; calling it from two goroutines still
|
||||
// serialises.
|
||||
func (l *lockedFile) Lock() error {
|
||||
if l == nil || l.f == nil {
|
||||
return fmt.Errorf("cronstore: lock not initialised")
|
||||
}
|
||||
if err := syscall.Flock(int(l.f.Fd()), syscall.LOCK_EX); err != nil {
|
||||
return fmt.Errorf("flock LOCK_EX: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unlock releases the exclusive flock. Idempotent in the sense that a
|
||||
// second call after Close simply returns nil because the fd is closed.
|
||||
func (l *lockedFile) Unlock() error {
|
||||
if l == nil || l.f == nil {
|
||||
return nil
|
||||
}
|
||||
if err := syscall.Flock(int(l.f.Fd()), syscall.LOCK_UN); err != nil {
|
||||
return fmt.Errorf("flock LOCK_UN: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the sidecar fd. The flock is released automatically by
|
||||
// the kernel.
|
||||
func (l *lockedFile) Close() error {
|
||||
if l == nil || l.f == nil {
|
||||
return nil
|
||||
}
|
||||
err := l.f.Close()
|
||||
l.f = nil
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
//go:build windows
|
||||
|
||||
// flock is a POSIX primitive; Windows does not implement syscall.Flock
|
||||
// the same way. Windows deployments of archied are not the supported
|
||||
// surface (the deployment model in CLAUDE.md and deployments/*.toml is
|
||||
// Linux + macOS), so this file deliberately fails closed: every Lock
|
||||
// returns an error rather than silently degrading to "no lock".
|
||||
//
|
||||
// If a Windows deployment ever becomes supported, swap this for a real
|
||||
// LockFileEx / UnlockFileEx implementation; the rest of the store does
|
||||
// not need to change.
|
||||
|
||||
package cronstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// lockedFile is a stub on Windows. The store still opens the sidecar
|
||||
// file so the on-disk layout matches across platforms, but every Lock
|
||||
// returns an explicit error.
|
||||
type lockedFile struct {
|
||||
f *os.File
|
||||
}
|
||||
|
||||
func openLock(path string) (*lockedFile, error) {
|
||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o644)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open lock file %s: %w", path, err)
|
||||
}
|
||||
return &lockedFile{f: f}, nil
|
||||
}
|
||||
|
||||
// errLockUnsupported is wrapped in every Lock call so callers can match
|
||||
// on it without parsing strings.
|
||||
var errLockUnsupported = errors.New("cronstore: cross-process locking is unsupported on Windows")
|
||||
|
||||
func (l *lockedFile) Lock() error { return errLockUnsupported }
|
||||
func (l *lockedFile) Unlock() error { return nil }
|
||||
|
||||
func (l *lockedFile) Close() error {
|
||||
if l == nil || l.f == nil {
|
||||
return nil
|
||||
}
|
||||
err := l.f.Close()
|
||||
l.f = nil
|
||||
return err
|
||||
}
|
||||
|
||||
// Keep imports alive when this file is built in isolation.
|
||||
var _ = fmt.Sprintf
|
||||
@@ -0,0 +1,33 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package cronstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Schedule decides when a job is due. The Kind field selects which of
|
||||
// Interval, Cron, At the store uses to compute nextRun. Empty Kind
|
||||
// resolves to "interval" so the simplest recurring job needs no
|
||||
// explicit kind in its JSON.
|
||||
type Schedule struct {
|
||||
// Kind is one of ScheduleInterval, ScheduleCron, ScheduleOnce.
|
||||
// Empty resolves to ScheduleInterval.
|
||||
Kind string `json:"kind,omitempty"`
|
||||
|
||||
// Interval is the period for ScheduleInterval. Required when Kind
|
||||
// is ScheduleInterval; ignored otherwise.
|
||||
Interval time.Duration `json:"interval,omitempty"`
|
||||
|
||||
// Cron is the 5-field cron expression for ScheduleCron. Required
|
||||
// when Kind is ScheduleCron; ignored otherwise.
|
||||
Cron string `json:"cron,omitempty"`
|
||||
|
||||
// At is the one-shot firing time for ScheduleOnce. Required when
|
||||
// Kind is ScheduleOnce; ignored otherwise.
|
||||
At *time.Time `json:"at,omitempty"`
|
||||
|
||||
// Start anchors the first run for ScheduleInterval / ScheduleCron.
|
||||
// Empty means "use creation time" — a freshly-created job with no
|
||||
// Start still has a finite next_run. Omitted from the wire when
|
||||
// unset so operator-readable files are not noisy.
|
||||
Start time.Time `json:"start"`
|
||||
}
|
||||
|
||||
// Schedule kinds the store recognises. Slice 1 fully implements
|
||||
// Interval; Once is accepted at Create / fires at Due exactly once, but
|
||||
// has no next-run arithmetic for MarkRun (one-shot by definition). Cron
|
||||
// is parsed at Create but has no firing arithmetic yet — Slice 2 will
|
||||
// add the parser. MarkRun on a kind this build does not implement
|
||||
// returns ErrScheduleUnsupported rather than silently advancing next_run.
|
||||
const (
|
||||
// ScheduleInterval fires every Interval after the last successful run
|
||||
// (or after Start, if never run). The simplest recurring schedule.
|
||||
ScheduleInterval = "interval"
|
||||
|
||||
// ScheduleCron fires at the next moment matching a standard 5-field
|
||||
// cron expression after the last successful run. Slice 2 will add
|
||||
// the parser; until then, MarkRun on a cron job returns
|
||||
// ErrScheduleUnsupported.
|
||||
ScheduleCron = "cron"
|
||||
|
||||
// ScheduleOnce fires once at the configured At time. Subsequent
|
||||
// MarkRun calls return ErrScheduleUnsupported because there is no
|
||||
// recurring next-run to compute.
|
||||
ScheduleOnce = "once"
|
||||
)
|
||||
|
||||
// Validate rejects empty or unknown schedule kinds up front so Create does
|
||||
// not have to defer the check until MarkRun. Interval additionally requires
|
||||
// a strictly positive duration; the others defer their full validation.
|
||||
func (s Schedule) Validate() error {
|
||||
switch s.Kind {
|
||||
case "", ScheduleInterval:
|
||||
if s.Interval <= 0 {
|
||||
return fmt.Errorf("%w: interval must be positive, got %v", ErrInvalidSpec, s.Interval)
|
||||
}
|
||||
case ScheduleCron:
|
||||
if s.Cron == "" {
|
||||
return fmt.Errorf("%w: cron expression must be non-empty", ErrInvalidSpec)
|
||||
}
|
||||
case ScheduleOnce:
|
||||
if s.At == nil {
|
||||
return fmt.Errorf("%w: once schedule requires an `at` time", ErrInvalidSpec)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("%w: unknown schedule kind %q", ErrInvalidSpec, s.Kind)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// resolve maps the empty kind onto its documented default — "interval" is
|
||||
// what an operator gets when they leave the field unset on the simplest
|
||||
// recurring job.
|
||||
func (s Schedule) resolve() Schedule {
|
||||
if s.Kind == "" {
|
||||
s.Kind = ScheduleInterval
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// firstRun returns when the job should fire the very first time it has
|
||||
// never run before. For Interval, it is Start + Interval (or now + Interval
|
||||
// when Start is zero, so a freshly-created job with no Start still has a
|
||||
// finite next-run). For Once, it is At. For Cron, it is left as a future
|
||||
// responsibility — Slice 1 will not synthesise a value it cannot honour.
|
||||
func (s Schedule) firstRun(now time.Time) (time.Time, error) {
|
||||
switch s.resolve().Kind {
|
||||
case ScheduleInterval:
|
||||
if !s.Start.IsZero() {
|
||||
return s.Start.Add(s.Interval), nil
|
||||
}
|
||||
return now.Add(s.Interval), nil
|
||||
case ScheduleOnce:
|
||||
if s.At == nil {
|
||||
return time.Time{}, fmt.Errorf("%w: once schedule has no at time", ErrScheduleUnsupported)
|
||||
}
|
||||
return *s.At, nil
|
||||
case ScheduleCron:
|
||||
return time.Time{}, fmt.Errorf("%w: cron parsing not yet implemented (slice 2)", ErrScheduleUnsupported)
|
||||
default:
|
||||
return time.Time{}, fmt.Errorf("%w: unknown kind %q", ErrScheduleUnsupported, s.Kind)
|
||||
}
|
||||
}
|
||||
|
||||
// nextRun returns when the job should fire after a successful run at
|
||||
// lastRun. It is the engine-side counterpart to firstRun: every MarkRun
|
||||
// advances next_run through this function. Interval is fully implemented;
|
||||
// Once and Cron deliberately fail so a silently-wrong value cannot reach
|
||||
// the ticker.
|
||||
func (s Schedule) nextRun(lastRun time.Time) (time.Time, error) {
|
||||
switch s.resolve().Kind {
|
||||
case ScheduleInterval:
|
||||
return lastRun.Add(s.Interval), nil
|
||||
case ScheduleOnce:
|
||||
return time.Time{}, fmt.Errorf("%w: once schedule has no next run", ErrScheduleUnsupported)
|
||||
case ScheduleCron:
|
||||
return time.Time{}, fmt.Errorf("%w: cron arithmetic not yet implemented (slice 2)", ErrScheduleUnsupported)
|
||||
default:
|
||||
return time.Time{}, fmt.Errorf("%w: unknown kind %q", ErrScheduleUnsupported, s.Kind)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,644 @@
|
||||
package cronstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
||||
)
|
||||
|
||||
// schemaVersion is stamped on every persisted file. Bump it on any
|
||||
// breaking change to JobSpec, Schedule, Payload or envelope shape, AND
|
||||
// on any additive change: DisallowUnknownFields means a future build
|
||||
// that adds a new field to JobSpec must also bump schemaVersion so
|
||||
// older builds keep rejecting the file rather than silently dropping
|
||||
// the new field.
|
||||
const schemaVersion = 1
|
||||
|
||||
// filePerm is the mode applied to the jobs.json file on write. Payloads
|
||||
// and chat ids can carry operator-identifying information, so 0o600 is
|
||||
// the default; operators who want it group-readable can chmod after.
|
||||
const filePerm = 0o600
|
||||
|
||||
// JobSpec is one persisted cron job as the engine's view sees it, plus
|
||||
// the bookkeeping fields the store computes (NextRun, LastRun, Created,
|
||||
// Updated). The wire shape is the public API; the scheduling.Job returned
|
||||
// to the engine is a stripped subset.
|
||||
type JobSpec struct {
|
||||
// ID is the unique identifier. Required, non-empty, and the merge
|
||||
// key for Update and MarkRun.
|
||||
ID string `json:"id"`
|
||||
|
||||
// Pool is "parallel" or "sequential". Empty means sequential — the
|
||||
// safer default, matching scheduling.Pool.resolve().
|
||||
Pool string `json:"pool,omitempty"`
|
||||
|
||||
// Detail is a human-readable label rendered on events.
|
||||
Detail string `json:"detail,omitempty"`
|
||||
|
||||
// Schedule decides when this job is due. See schedule.go for the
|
||||
// supported kinds and their arithmetic.
|
||||
Schedule Schedule `json:"schedule"`
|
||||
|
||||
// Target and Payload are placeholders for Slice 2 (delivery runner).
|
||||
// They live on the wire now so jobs created ahead of Slice 2 do not
|
||||
// need a schema migration when delivery lands.
|
||||
Target Target `json:"target"`
|
||||
Payload Payload `json:"payload"`
|
||||
|
||||
// NextRun is the absolute moment the job will next fire. Computed
|
||||
// at write time so the engine's hot path is a single comparison.
|
||||
NextRun time.Time `json:"next_run"`
|
||||
|
||||
// LastRun is the last run time reported by MarkRun, nil if the job
|
||||
// has never run. Mutated only by MarkRun. The store does not know
|
||||
// whether the run succeeded — that judgement belongs to the runner.
|
||||
LastRun *time.Time `json:"last_run,omitempty"`
|
||||
|
||||
// Created and Updated are stamped by Create / Update respectively.
|
||||
// They are audit metadata; the engine does not read them.
|
||||
Created time.Time `json:"created"`
|
||||
Updated time.Time `json:"updated"`
|
||||
}
|
||||
|
||||
// Target identifies where the job's output goes (Slice 2 fills this in).
|
||||
// Empty Target is valid — Slice 1 stores the field but no consumer reads
|
||||
// it yet.
|
||||
type Target struct {
|
||||
// ChatID is the chat channel id (Telegram chat id, email address,
|
||||
// webhook URL — depends on the channel). Empty defers to a default
|
||||
// chosen by the delivery runner.
|
||||
ChatID string `json:"chat_id,omitempty"`
|
||||
}
|
||||
|
||||
// Payload is the content sent at run time. Slice 2 fills this in; Slice 1
|
||||
// stores the text verbatim so the engine does not need to know about it.
|
||||
type Payload struct {
|
||||
// Text is the literal message body. Templating (cron blueprints) is
|
||||
// a Slice 3 concern and is deliberately absent here.
|
||||
Text string `json:"text,omitempty"`
|
||||
}
|
||||
|
||||
// Patch is the partial JobSpec Update merges over the existing record. A
|
||||
// nil pointer field leaves the corresponding persisted field untouched;
|
||||
// the non-nil pointer or non-zero value overwrites it.
|
||||
//
|
||||
// Schedule is replaced wholesale when the Patch's Schedule pointer is
|
||||
// non-nil — schedules are not deep-merged, because each field of
|
||||
// Schedule carries semantics (Kind, Interval, Cron, At) that have no
|
||||
// meaningful "preserve" answer on their own.
|
||||
type Patch struct {
|
||||
Pool *string `json:"pool,omitempty"`
|
||||
Detail *string `json:"detail,omitempty"`
|
||||
Schedule *Schedule `json:"schedule,omitempty"`
|
||||
Target *Target `json:"target,omitempty"`
|
||||
Payload *Payload `json:"payload,omitempty"`
|
||||
}
|
||||
|
||||
// envelope is the on-disk shape. Kept private so the only public surface
|
||||
// is JobSpec / Patch.
|
||||
type envelope struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
Jobs []JobSpec `json:"jobs"`
|
||||
}
|
||||
|
||||
// Store is one open handle to the job file. It is safe for concurrent
|
||||
// callers in the same process, and safe against concurrent CLI / daemon
|
||||
// processes through the cross-process lock.
|
||||
//
|
||||
// All exported methods take a context for cancellation; reads and writes
|
||||
// are short, so the ctx is plumbed through but rarely observed.
|
||||
//
|
||||
// After Close, every other method returns ErrClosed. Close itself is
|
||||
// safe to call multiple times.
|
||||
type Store struct {
|
||||
path string // jobs.json
|
||||
lock *lockedFile
|
||||
|
||||
// closed is set by Close so concurrent callers fail fast rather than
|
||||
// dereferencing a closed lock fd.
|
||||
closed atomic.Bool
|
||||
|
||||
// mu guards the in-memory copy that mirrors the file. Acquire it
|
||||
// before reading or writing jobs; the cross-process lock guards
|
||||
// the file itself. They must always be taken in the same order:
|
||||
// cross-process lock first, then mu.
|
||||
mu sync.Mutex
|
||||
buf []JobSpec // in-memory mirror of the on-disk jobs array
|
||||
}
|
||||
|
||||
// ErrClosed is returned by every method after Close. It lets callers
|
||||
// distinguish "I gave up" from a real error.
|
||||
var ErrClosed = errors.New("cronstore: store is closed")
|
||||
|
||||
// Open constructs a Store for the given jobs.json path. The file is
|
||||
// created lazily on first write, but the parent directory is created
|
||||
// immediately so a subsequent Create does not race against mkdir.
|
||||
//
|
||||
// Open validates the persisted file's schema version: a future version
|
||||
// the build does not know returns ErrUnsupportedSchema rather than a
|
||||
// silent best-effort decode. A corrupt or truncated file returns
|
||||
// ErrCorruptFile.
|
||||
func Open(path string) (*Store, error) {
|
||||
if path == "" {
|
||||
return nil, fmt.Errorf("%w: path must be non-empty", ErrInvalidSpec)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return nil, fmt.Errorf("cronstore: create parent directory: %w", err)
|
||||
}
|
||||
lock, err := openLock(path + ".lock")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cronstore: open lock: %w", err)
|
||||
}
|
||||
|
||||
s := &Store{
|
||||
path: path,
|
||||
lock: lock,
|
||||
}
|
||||
|
||||
// Best-effort hydrate. An absent file is fine: the store will
|
||||
// materialise it on first Create.
|
||||
if err := s.hydrate(); err != nil {
|
||||
_ = lock.Close()
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Path returns the file the store reads and writes. Exposed for
|
||||
// diagnostics and for the CLI to print the location it is editing.
|
||||
func (s *Store) Path() string { return s.path }
|
||||
|
||||
// Close releases the cross-process lock. After Close, every other
|
||||
// method returns ErrClosed. Close is safe to call more than once.
|
||||
func (s *Store) Close() error {
|
||||
if !s.closed.CompareAndSwap(false, true) {
|
||||
return nil
|
||||
}
|
||||
if s.lock == nil {
|
||||
return nil
|
||||
}
|
||||
err := s.lock.Close()
|
||||
s.lock = nil
|
||||
return err
|
||||
}
|
||||
|
||||
// checkOpen returns ErrClosed when the store has been closed. Every
|
||||
// public method calls this first so a Close mid-operation surfaces as a
|
||||
// real error rather than a panic on a nil lock fd.
|
||||
func (s *Store) checkOpen() error {
|
||||
if s.closed.Load() {
|
||||
return ErrClosed
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create adds a new job. Fails with ErrJobExists on a duplicate id, with
|
||||
// ErrInvalidSpec on a structurally invalid spec. The store stamps
|
||||
// Created / Updated to now and computes NextRun from the schedule.
|
||||
func (s *Store) Create(ctx context.Context, spec JobSpec) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.checkOpen(); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(spec.ID) == "" {
|
||||
return fmt.Errorf("%w: id must be non-empty", ErrInvalidSpec)
|
||||
}
|
||||
if err := spec.Schedule.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := scheduling.Pool(spec.Pool).Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.withLock(func() error {
|
||||
for _, existing := range s.buf {
|
||||
if existing.ID == spec.ID {
|
||||
return fmt.Errorf("%w: id %q", ErrJobExists, spec.ID)
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
// Honor a caller-supplied NextRun when present: the test suite
|
||||
// needs to set up past/future due states deterministically, and
|
||||
// the CLI may want to schedule a one-off for a specific time.
|
||||
// Zero NextRun falls back to the schedule's first-run arithmetic.
|
||||
if spec.NextRun.IsZero() {
|
||||
next, err := spec.Schedule.firstRun(now)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
spec.NextRun = next.UTC()
|
||||
}
|
||||
spec.Created = now
|
||||
spec.Updated = now
|
||||
spec.Schedule = spec.Schedule.resolve()
|
||||
s.buf = append(s.buf, spec)
|
||||
return s.writeLocked()
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get returns the job by id. The second return value is false when the
|
||||
// id is absent; the error is reserved for I/O failures. The returned
|
||||
// JobSpec is a deep copy: callers can mutate LastRun and other fields
|
||||
// without disturbing the store's in-memory mirror.
|
||||
func (s *Store) Get(ctx context.Context, id string) (JobSpec, bool, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return JobSpec{}, false, err
|
||||
}
|
||||
if err := s.checkOpen(); err != nil {
|
||||
return JobSpec{}, false, err
|
||||
}
|
||||
var (
|
||||
got JobSpec
|
||||
ok bool
|
||||
)
|
||||
err := s.withLock(func() error {
|
||||
for _, j := range s.buf {
|
||||
if j.ID == id {
|
||||
got = deepCopyJob(j)
|
||||
ok = true
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return got, ok, err
|
||||
}
|
||||
|
||||
// List returns a copy of every persisted job. Order is the on-disk
|
||||
// order, which is also the creation order. Callers may mutate the
|
||||
// returned slice and its elements without disturbing the store's
|
||||
// in-memory mirror.
|
||||
func (s *Store) List(ctx context.Context) ([]JobSpec, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.checkOpen(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []JobSpec
|
||||
err := s.withLock(func() error {
|
||||
out = make([]JobSpec, len(s.buf))
|
||||
for i := range s.buf {
|
||||
out[i] = deepCopyJob(s.buf[i])
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
// Update applies a Patch over the existing record. Fields the Patch
|
||||
// leaves nil are preserved verbatim; non-nil pointers and the Schedule
|
||||
// are wholesale replacements. Schedule changes trigger a NextRun
|
||||
// recomputation, since NextRun is derived from Schedule and LastRun.
|
||||
//
|
||||
// Returns ErrJobNotFound when the id is absent.
|
||||
func (s *Store) Update(ctx context.Context, id string, patch Patch) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.checkOpen(); err != nil {
|
||||
return err
|
||||
}
|
||||
if id == "" {
|
||||
return fmt.Errorf("%w: id must be non-empty", ErrInvalidSpec)
|
||||
}
|
||||
if patch.Pool != nil {
|
||||
if err := scheduling.Pool(*patch.Pool).Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if patch.Schedule != nil {
|
||||
if err := patch.Schedule.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return s.withLock(func() error {
|
||||
for i := range s.buf {
|
||||
if s.buf[i].ID != id {
|
||||
continue
|
||||
}
|
||||
if patch.Pool != nil {
|
||||
s.buf[i].Pool = *patch.Pool
|
||||
}
|
||||
if patch.Detail != nil {
|
||||
s.buf[i].Detail = *patch.Detail
|
||||
}
|
||||
if patch.Schedule != nil {
|
||||
s.buf[i].Schedule = patch.Schedule.resolve()
|
||||
// Schedule change: NextRun is stale. Recompute from
|
||||
// the last run if there is one, else as a first run
|
||||
// from now. Keeping it strictly future-of-now would
|
||||
// re-fire a job the operator just shortened.
|
||||
now := time.Now().UTC()
|
||||
if s.buf[i].LastRun != nil {
|
||||
next, err := s.buf[i].Schedule.nextRun(*s.buf[i].LastRun)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.buf[i].NextRun = next.UTC()
|
||||
} else {
|
||||
next, err := s.buf[i].Schedule.firstRun(now)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.buf[i].NextRun = next.UTC()
|
||||
}
|
||||
}
|
||||
if patch.Target != nil {
|
||||
s.buf[i].Target = *patch.Target
|
||||
}
|
||||
if patch.Payload != nil {
|
||||
s.buf[i].Payload = *patch.Payload
|
||||
}
|
||||
s.buf[i].Updated = time.Now().UTC()
|
||||
return s.writeLocked()
|
||||
}
|
||||
return fmt.Errorf("%w: id %q", ErrJobNotFound, id)
|
||||
})
|
||||
}
|
||||
|
||||
// Delete removes the job by id. Idempotent: deleting a missing id
|
||||
// returns nil rather than ErrJobNotFound, so a CLI retry after a
|
||||
// partial failure cannot stall the operator.
|
||||
func (s *Store) Delete(ctx context.Context, id string) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.checkOpen(); err != nil {
|
||||
return err
|
||||
}
|
||||
if id == "" {
|
||||
return fmt.Errorf("%w: id must be non-empty", ErrInvalidSpec)
|
||||
}
|
||||
return s.withLock(func() error {
|
||||
for i := range s.buf {
|
||||
if s.buf[i].ID != id {
|
||||
continue
|
||||
}
|
||||
s.buf = append(s.buf[:i], s.buf[i+1:]...)
|
||||
return s.writeLocked()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// MarkRun records that id ran at runAt and advances NextRun to the
|
||||
// schedule's next firing. It is the engine-side counterpart to Create:
|
||||
// once the ticker has dispatched a job, the runner (or the engine
|
||||
// itself, in tests) calls MarkRun to keep next_run fresh.
|
||||
//
|
||||
// Returns ErrJobNotFound when the id is absent and
|
||||
// ErrScheduleUnsupported when the schedule kind has no defined
|
||||
// "next after this run" semantics.
|
||||
func (s *Store) MarkRun(ctx context.Context, id string, runAt time.Time) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.checkOpen(); err != nil {
|
||||
return err
|
||||
}
|
||||
if id == "" {
|
||||
return fmt.Errorf("%w: id must be non-empty", ErrInvalidSpec)
|
||||
}
|
||||
return s.withLock(func() error {
|
||||
for i := range s.buf {
|
||||
if s.buf[i].ID != id {
|
||||
continue
|
||||
}
|
||||
next, err := s.buf[i].Schedule.nextRun(runAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t := runAt.UTC()
|
||||
s.buf[i].LastRun = &t
|
||||
s.buf[i].NextRun = next.UTC()
|
||||
s.buf[i].Updated = time.Now().UTC()
|
||||
return s.writeLocked()
|
||||
}
|
||||
return fmt.Errorf("%w: id %q", ErrJobNotFound, id)
|
||||
})
|
||||
}
|
||||
|
||||
// Due implements scheduling.JobSource. It returns the jobs whose
|
||||
// NextRun is at or before now, in creation order. The returned
|
||||
// scheduling.Job is the engine's projection of the persisted JobSpec:
|
||||
// only the fields the engine relies on (ID, Pool, Detail) cross the
|
||||
// boundary. Schedule / Payload / Target are deliberately not exposed:
|
||||
// the engine does no arithmetic on them and has no business knowing
|
||||
// they exist.
|
||||
//
|
||||
// A job with an unrecognised pool is skipped: Create / Update enforce
|
||||
// the set, but a file edited by hand could carry an invalid value, and
|
||||
// the engine cannot reason about a pool it has never heard of. The
|
||||
// store does not crash on bad input — the same way it does not panic
|
||||
// on a truncated file.
|
||||
func (s *Store) Due(ctx context.Context, now time.Time) ([]scheduling.Job, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.checkOpen(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var due []scheduling.Job
|
||||
err := s.withLock(func() error {
|
||||
for _, j := range s.buf {
|
||||
if j.NextRun.After(now) {
|
||||
continue
|
||||
}
|
||||
pool := scheduling.Pool(j.Pool)
|
||||
if err := pool.Validate(); err != nil {
|
||||
continue
|
||||
}
|
||||
due = append(due, scheduling.Job{
|
||||
ID: j.ID,
|
||||
Pool: pool,
|
||||
Detail: j.Detail,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return due, err
|
||||
}
|
||||
|
||||
// withLock serialises access to the file. Callers receive a closure
|
||||
// that runs with the cross-process lock held and the in-memory buffer
|
||||
// freshly hydrated from disk; the closure's return value becomes
|
||||
// withLock's return value.
|
||||
//
|
||||
// Three locks stack, always in this order:
|
||||
//
|
||||
// 1. processMu: keyed by lock-file path. Serialises every Store that
|
||||
// points at the same jobs.json within one process. Necessary
|
||||
// because Linux flock is per file description: two Store instances
|
||||
// in the same process have independent fds and would otherwise
|
||||
// proceed in parallel. fcntl / POSIX locks would not have this
|
||||
// property — they would block — but flock is the documented
|
||||
// primitive on Linux + macOS, so we compensate at this layer.
|
||||
// 2. cross-process flock: serialises Store instances across
|
||||
// processes (CLI vs daemon). flock alone is insufficient for
|
||||
// in-process coordination (see #1).
|
||||
// 3. s.mu: guards s.buf against same-Store concurrent goroutines.
|
||||
// Redundant under #1 + #2 but cheap, and it documents the
|
||||
// invariant for any reader trying to follow the locking story.
|
||||
//
|
||||
// After all three are held, the in-memory buffer is re-read from disk
|
||||
// so a writer that just acquired the locks sees every change the
|
||||
// previous writer committed. Without this, a Store that opened before
|
||||
// any writes happened would carry an empty view forever and silently
|
||||
// overwrite everything.
|
||||
//
|
||||
// Re-reading on every operation is acceptable for cron workloads: the
|
||||
// file holds O(dozens) of jobs, not O(thousands), and operations are
|
||||
// human-paced, not high-throughput.
|
||||
func (s *Store) withLock(fn func() error) error {
|
||||
processMu := processLockFor(s.path + ".lock")
|
||||
processMu.Lock()
|
||||
defer processMu.Unlock()
|
||||
|
||||
if s.closed.Load() {
|
||||
return ErrClosed
|
||||
}
|
||||
if err := s.lock.Lock(); err != nil {
|
||||
return fmt.Errorf("cronstore: acquire lock: %w", err)
|
||||
}
|
||||
defer func() { _ = s.lock.Unlock() }()
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if err := s.hydrate(); err != nil {
|
||||
return err
|
||||
}
|
||||
return fn()
|
||||
}
|
||||
|
||||
// hydrate reads the file into the in-memory buffer. Called from Open
|
||||
// (no lock needed; nothing else can see the Store yet) and from
|
||||
// withLock (with both locks held).
|
||||
func (s *Store) hydrate() error {
|
||||
data, err := os.ReadFile(s.path)
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%w: read %s: %v", ErrCorruptFile, s.path, err)
|
||||
}
|
||||
if len(bytes.TrimSpace(data)) == 0 {
|
||||
return nil
|
||||
}
|
||||
var env envelope
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
// Reject unknown fields so a typo on the operator's side does not
|
||||
// silently land in a future field and shadow a real value. The
|
||||
// cost is that any additive change to JobSpec MUST bump
|
||||
// schemaVersion — see the comment on that constant.
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&env); err != nil {
|
||||
return fmt.Errorf("%w: decode %s: %v", ErrCorruptFile, s.path, err)
|
||||
}
|
||||
if env.SchemaVersion == 0 {
|
||||
return fmt.Errorf("%w: missing schema_version in %s", ErrCorruptFile, s.path)
|
||||
}
|
||||
if env.SchemaVersion > schemaVersion {
|
||||
return fmt.Errorf("%w: file is version %d, this build understands up to %d",
|
||||
ErrUnsupportedSchema, env.SchemaVersion, schemaVersion)
|
||||
}
|
||||
s.buf = env.Jobs
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeLocked persists the in-memory buffer to the file. Must be called
|
||||
// with s.mu held and the cross-process lock held — both guarantees
|
||||
// follow from withLock. Atomic write via temp-file + rename, so a
|
||||
// crash mid-write leaves the previous file intact rather than a
|
||||
// truncated mess the next Open would have to clean up.
|
||||
func (s *Store) writeLocked() error {
|
||||
env := envelope{
|
||||
SchemaVersion: schemaVersion,
|
||||
Jobs: s.buf,
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
enc := json.NewEncoder(&buf)
|
||||
// operator-readable file: do not escape <, >, &. The default
|
||||
// encoding/json behaviour writes these as < etc, which is
|
||||
// round-trip-safe but unreadable when a human opens jobs.json to
|
||||
// understand why a job failed to fire.
|
||||
enc.SetEscapeHTML(false)
|
||||
if err := enc.Encode(env); err != nil {
|
||||
return fmt.Errorf("cronstore: encode: %w", err)
|
||||
}
|
||||
if err := atomicWriteFile(s.path, buf.Bytes(), filePerm); err != nil {
|
||||
return fmt.Errorf("cronstore: write %s: %w", s.path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// atomicWriteFile writes data to path via a sibling temp file and
|
||||
// rename. Rename within a single filesystem is atomic on POSIX, so a
|
||||
// concurrent reader either sees the previous file in full or the new
|
||||
// one in full — never a half-written body. Sync before rename gives a
|
||||
// durability guarantee: the inode's data is on disk before the
|
||||
// directory entry switches over.
|
||||
func atomicWriteFile(path string, data []byte, perm os.FileMode) error {
|
||||
dir := filepath.Dir(path)
|
||||
tmp, err := os.CreateTemp(dir, filepath.Base(path)+".tmp-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tmpName := tmp.Name()
|
||||
defer func() {
|
||||
// Best-effort cleanup on any failure path. The rename has
|
||||
// either happened (and tmpName is gone) or it has not (and
|
||||
// the temp file still needs to go).
|
||||
_ = os.Remove(tmpName)
|
||||
}()
|
||||
if _, err := tmp.Write(data); err != nil {
|
||||
_ = tmp.Close()
|
||||
return err
|
||||
}
|
||||
if err := tmp.Sync(); err != nil {
|
||||
_ = tmp.Close()
|
||||
return err
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Chmod(tmpName, perm); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmpName, path)
|
||||
}
|
||||
|
||||
// deepCopyJob returns a JobSpec value independent of the source's
|
||||
// pointer fields. JobSpec carries one pointer (LastRun); a shallow
|
||||
// copy aliases it and lets callers mutate the store's in-memory state
|
||||
// by writing through the returned value.
|
||||
func deepCopyJob(j JobSpec) JobSpec {
|
||||
out := j
|
||||
if j.LastRun != nil {
|
||||
t := *j.LastRun
|
||||
out.LastRun = &t
|
||||
}
|
||||
if j.Schedule.At != nil {
|
||||
t := *j.Schedule.At
|
||||
out.Schedule.At = &t
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,485 @@
|
||||
// Additional tests added in response to the Slice-1 adversarial review.
|
||||
// They live in a separate file so the original spec file stays focused
|
||||
// on the contract-level behaviour the engine depends on, and these
|
||||
// cases stay grouped where the reviewer can re-read them as a unit.
|
||||
package cronstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
||||
)
|
||||
|
||||
// --- C2 + P8: ErrCorruptFile is the right sentinel for bad files -------
|
||||
|
||||
func TestOpenTruncatedFileReturnsErrCorruptFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
if err := os.WriteFile(path, []byte(`{"schema_version":1,"jobs":[{"id":"x","`), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
_, err := Open(path)
|
||||
if !errors.Is(err, ErrCorruptFile) {
|
||||
t.Fatalf("Open on truncated file = %v, want ErrCorruptFile", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenGarbageFileReturnsErrCorruptFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
if err := os.WriteFile(path, []byte("not json at all"), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
_, err := Open(path)
|
||||
if !errors.Is(err, ErrCorruptFile) {
|
||||
t.Fatalf("Open on garbage file = %v, want ErrCorruptFile", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenEmptyFileIsTreatedAsNoJobs(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
if err := os.WriteFile(path, []byte(" \n\t "), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
s, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open on whitespace-only file = %v, want nil", err)
|
||||
}
|
||||
defer s.Close()
|
||||
jobs, _ := s.List(context.Background())
|
||||
if len(jobs) != 0 {
|
||||
t.Errorf("List on whitespace-only file = %d jobs, want 0", len(jobs))
|
||||
}
|
||||
}
|
||||
|
||||
// --- C6 / P8: DisallowUnknownFields rejects hand-edited extras ---------
|
||||
|
||||
func TestOpenRejectsUnknownFieldAtSameSchemaVersion(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
// A future build might add a "tags" field to JobSpec. An older
|
||||
// build with DisallowUnknownFields set must reject the file rather
|
||||
// than silently dropping the field — that's the contract behind
|
||||
// bumping schemaVersion on additive changes.
|
||||
contents := `{"schema_version":1,"jobs":[{"id":"x","pool":"parallel","schedule":{"kind":"interval","interval":60000000000},"target":{},"payload":{},"next_run":"2026-08-28T12:00:00Z","created":"2026-08-28T12:00:00Z","updated":"2026-08-28T12:00:00Z","tags":["ops"]}]}`
|
||||
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
_, err := Open(path)
|
||||
if !errors.Is(err, ErrCorruptFile) {
|
||||
t.Fatalf("Open on file with unknown field = %v, want ErrCorruptFile", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- P2: Get / List return deep copies -------------------------------
|
||||
|
||||
func TestGetReturnsDeepCopyOfLastRun(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, fixedIntervalSpec("x", time.Hour))
|
||||
first, _, _ := s.Get(context.Background(), "x")
|
||||
if first.LastRun != nil {
|
||||
t.Fatalf("LastRun on fresh create = %v, want nil", first.LastRun)
|
||||
}
|
||||
runAt := time.Now().UTC().Add(-time.Minute)
|
||||
if err := s.MarkRun(context.Background(), "x", runAt); err != nil {
|
||||
t.Fatalf("MarkRun: %v", err)
|
||||
}
|
||||
got, _, _ := s.Get(context.Background(), "x")
|
||||
if got.LastRun == nil {
|
||||
t.Fatal("LastRun is nil after MarkRun")
|
||||
}
|
||||
// Caller mutates the returned LastRun. The store's in-memory
|
||||
// value must NOT change — a future Get must still report the
|
||||
// original runAt.
|
||||
*got.LastRun = time.Date(2099, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
got2, _, _ := s.Get(context.Background(), "x")
|
||||
if !got2.LastRun.Equal(runAt) {
|
||||
t.Errorf("after caller mutation, Get returned LastRun=%v, want %v", got2.LastRun, runAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListReturnsDeepCopies(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, fixedIntervalSpec("x", time.Hour))
|
||||
mustCreate(t, s, fixedIntervalSpec("y", time.Hour))
|
||||
|
||||
list1, _ := s.List(context.Background())
|
||||
if len(list1) != 2 {
|
||||
t.Fatalf("List returned %d jobs, want 2", len(list1))
|
||||
}
|
||||
// Caller mutates the returned slice (deletes an entry) and a job's
|
||||
// string fields. Neither must touch the store's in-memory mirror.
|
||||
list1[0].Detail = "tampered"
|
||||
list1 = list1[:1]
|
||||
|
||||
list2, _ := s.List(context.Background())
|
||||
if len(list2) != 2 {
|
||||
t.Errorf("after caller truncated list, second List returned %d jobs, want 2", len(list2))
|
||||
}
|
||||
for _, j := range list2 {
|
||||
if j.Detail == "tampered" {
|
||||
t.Errorf("caller's Detail mutation leaked into the store: %+v", j)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- P3: pool validation in Create / Update ---------------------------
|
||||
|
||||
func TestCreateRejectsInvalidPool(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
spec := fixedIntervalSpec("x", time.Minute)
|
||||
spec.Pool = "paralle" // typo
|
||||
if err := s.Create(context.Background(), spec); err == nil {
|
||||
t.Fatal("Create with invalid pool succeeded; want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateRejectsInvalidPool(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, fixedIntervalSpec("x", time.Minute))
|
||||
if err := s.Update(context.Background(), "x", Patch{Pool: new("paralle")}); err == nil {
|
||||
t.Fatal("Update with invalid pool succeeded; want error")
|
||||
}
|
||||
}
|
||||
|
||||
// --- P5 / C1 / P6: Once schedule behaviour + MarkRun sentinel ---------
|
||||
|
||||
func TestCreateAcceptsOnceSchedule(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
at := time.Now().Add(time.Hour)
|
||||
spec := JobSpec{
|
||||
ID: "one-off",
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleOnce,
|
||||
At: &at,
|
||||
},
|
||||
}
|
||||
if err := s.Create(context.Background(), spec); err != nil {
|
||||
t.Fatalf("Create with Once schedule: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDueFiresOnceAtConfiguredTime(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
at := time.Date(2026, 8, 28, 12, 0, 0, 0, time.UTC)
|
||||
spec := JobSpec{
|
||||
ID: "one-off",
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleOnce,
|
||||
At: &at,
|
||||
},
|
||||
}
|
||||
if err := s.Create(context.Background(), spec); err != nil {
|
||||
t.Fatalf("Create: %v", err)
|
||||
}
|
||||
|
||||
due, err := s.Due(context.Background(), at)
|
||||
if err != nil {
|
||||
t.Fatalf("Due: %v", err)
|
||||
}
|
||||
if len(due) != 1 || due[0].ID != "one-off" {
|
||||
t.Errorf("Due(at) = %+v, want one-off", due)
|
||||
}
|
||||
due, _ = s.Due(context.Background(), at.Add(-time.Second))
|
||||
if len(due) != 0 {
|
||||
t.Errorf("Due(at - 1s) = %+v, want empty", due)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkRunOnOnceReturnsErrScheduleUnsupported(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
at := time.Now().Add(time.Hour)
|
||||
spec := JobSpec{
|
||||
ID: "one-off",
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleOnce,
|
||||
At: &at,
|
||||
},
|
||||
}
|
||||
if err := s.Create(context.Background(), spec); err != nil {
|
||||
t.Fatalf("Create: %v", err)
|
||||
}
|
||||
err := s.MarkRun(context.Background(), "one-off", time.Now())
|
||||
if !errors.Is(err, ErrScheduleUnsupported) {
|
||||
t.Fatalf("MarkRun on once = %v, want ErrScheduleUnsupported", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- P8: Due drops unknown pool values silently -----------------------
|
||||
|
||||
func TestDueSilentlySkipsUnknownPool(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
s, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
defer s.Close()
|
||||
// Hand-edit the file: bypass Create's validation by writing a
|
||||
// job with an invalid pool directly to disk.
|
||||
past := time.Now().Add(-time.Minute).UTC().Format(time.RFC3339Nano)
|
||||
contents := `{"schema_version":1,"jobs":[{"id":"badpool","pool":"paralle","schedule":{"kind":"interval","interval":60000000000},"target":{},"payload":{},"next_run":"` + past + `","created":"` + past + `","updated":"` + past + `"}]}`
|
||||
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
// Drop the in-memory mirror so the next read picks up the file.
|
||||
if err := s.hydrate(); err != nil {
|
||||
t.Fatalf("hydrate: %v", err)
|
||||
}
|
||||
due, err := s.Due(context.Background(), time.Now())
|
||||
if err != nil {
|
||||
t.Fatalf("Due: %v", err)
|
||||
}
|
||||
if len(due) != 0 {
|
||||
t.Errorf("Due returned %d jobs for an invalid pool, want 0", len(due))
|
||||
}
|
||||
// But Get still sees the job — Create/Update validation is the
|
||||
// gate that prevents this in normal operation; a hand-edited
|
||||
// file remains visible through CRUD.
|
||||
if _, ok, _ := s.Get(context.Background(), "badpool"); !ok {
|
||||
t.Error("Get returned ok=false for a hand-edited job with bad pool")
|
||||
}
|
||||
}
|
||||
|
||||
// --- P10: UTF-8 + quote + newline round-trip --------------------------
|
||||
|
||||
func TestCreateAndGetRoundTripWithUnicodeAndQuotes(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
spec := JobSpec{
|
||||
ID: "ops-standup",
|
||||
Pool: string(scheduling.PoolSequential),
|
||||
Detail: "Hello\n\"世界\"\\back",
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleInterval,
|
||||
Interval: 30 * time.Minute,
|
||||
},
|
||||
Payload: Payload{Text: "<script>alert(1)</script> & 中文"},
|
||||
}
|
||||
mustCreate(t, s, spec)
|
||||
got, ok, err := s.Get(context.Background(), spec.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Get: %v", err)
|
||||
}
|
||||
if !ok {
|
||||
t.Fatal("Get returned ok=false")
|
||||
}
|
||||
if got.Detail != spec.Detail {
|
||||
t.Errorf("Detail = %q, want %q", got.Detail, spec.Detail)
|
||||
}
|
||||
if got.Payload.Text != spec.Payload.Text {
|
||||
t.Errorf("Payload.Text = %q, want %q", got.Payload.Text, spec.Payload.Text)
|
||||
}
|
||||
}
|
||||
|
||||
// --- C3: operator-readable file does not HTML-escape -------------------
|
||||
|
||||
func TestFileIsOperatorReadableNoHTMLEscape(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
s, _ := Open(path)
|
||||
defer s.Close()
|
||||
spec := fixedIntervalSpec("x", time.Minute)
|
||||
spec.Payload.Text = "<a&b>"
|
||||
mustCreate(t, s, spec)
|
||||
_ = s.Close()
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile: %v", err)
|
||||
}
|
||||
// With SetEscapeHTML(false), < > & appear as the literal characters.
|
||||
// The escaped form would be <, >, & — none of which
|
||||
// is the printable payload. Assert the literal is there.
|
||||
if !bytes.Contains(data, []byte(`"<a&b>"`)) {
|
||||
t.Errorf("payload was HTML-escaped; want operator-readable literal: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
// --- C4: file is 0o600, not 0o644 --------------------------------------
|
||||
|
||||
func TestFileIsCreatedWithOwnerOnlyPermissions(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
s, _ := Open(path)
|
||||
defer s.Close()
|
||||
mustCreate(t, s, fixedIntervalSpec("x", time.Minute))
|
||||
_ = s.Close()
|
||||
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Stat: %v", err)
|
||||
}
|
||||
perm := info.Mode().Perm()
|
||||
if perm != 0o600 {
|
||||
t.Errorf("jobs.json perm = %#o, want 0o600", perm)
|
||||
}
|
||||
}
|
||||
|
||||
// --- C5: cross-process flock via os/exec ------------------------------
|
||||
|
||||
// TestCrossProcessFlockBlocksParentWhileChildHoldsLock runs a child
|
||||
// process that grabs the cross-process flock and sleeps; the parent
|
||||
// then tries to take the same lock and must block for at least the
|
||||
// child's sleep duration. Without the flock, the parent's Create
|
||||
// would return immediately and the test would fail.
|
||||
func TestCrossProcessFlockBlocksParentWhileChildHoldsLock(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("subprocess test")
|
||||
}
|
||||
if _, err := exec.LookPath("go"); err != nil {
|
||||
t.Skip("no go toolchain available for subprocess test")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
|
||||
// Build a tiny helper that opens the same file and holds the
|
||||
// flock. The helper is built once per run; the cost is paid only
|
||||
// when this test runs.
|
||||
helperSrc := filepath.Join(dir, "helper.go")
|
||||
if err := os.WriteFile(helperSrc, []byte(`
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, err := os.OpenFile(os.Args[1]+".lock", os.O_RDWR|os.O_CREATE, 0o644)
|
||||
if err != nil { fmt.Println("open:", err); os.Exit(2) }
|
||||
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
|
||||
fmt.Println("flock:", err); os.Exit(3)
|
||||
}
|
||||
fmt.Println("locked")
|
||||
// Signal readiness, then hold the lock long enough for the
|
||||
// parent to observe the block.
|
||||
time.Sleep(2 * time.Second)
|
||||
_ = f.Close()
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile helper: %v", err)
|
||||
}
|
||||
helperBin := filepath.Join(dir, "helper")
|
||||
if out, err := exec.Command("go", "build", "-o", helperBin, helperSrc).CombinedOutput(); err != nil {
|
||||
t.Fatalf("go build helper: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
// Launch the child in the background.
|
||||
child := exec.Command(helperBin, path)
|
||||
child.Stdout = os.Stdout
|
||||
child.Stderr = os.Stderr
|
||||
if err := child.Start(); err != nil {
|
||||
t.Fatalf("child Start: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = child.Process.Kill()
|
||||
_ = child.Wait()
|
||||
}()
|
||||
|
||||
// Give the child a moment to acquire the lock.
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
|
||||
// From the parent, open the same Store and call Create. Without
|
||||
// flock, Create would return instantly; with flock, it must
|
||||
// block until the child releases.
|
||||
parent, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("parent Open: %v", err)
|
||||
}
|
||||
defer parent.Close()
|
||||
|
||||
start := time.Now()
|
||||
if err := parent.Create(context.Background(), fixedIntervalSpec("from-parent", time.Minute)); err != nil {
|
||||
t.Fatalf("parent Create: %v", err)
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
|
||||
// The child held the lock for ~2s. The parent's Create must have
|
||||
// blocked for at least the overlap between its start and the
|
||||
// child's release — conservatively, >1s.
|
||||
if elapsed < time.Second {
|
||||
t.Errorf("parent Create returned in %v; expected to block on the child's flock", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
// --- P1: Close mid-operation returns ErrClosed ------------------------
|
||||
|
||||
func TestMethodsReturnErrClosedAfterClose(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, fixedIntervalSpec("x", time.Minute))
|
||||
if err := s.Close(); err != nil {
|
||||
t.Fatalf("Close: %v", err)
|
||||
}
|
||||
// Every public method must surface ErrClosed, not panic on the
|
||||
// nil lock fd.
|
||||
tests := []struct {
|
||||
name string
|
||||
fn func() error
|
||||
}{
|
||||
{"Create", func() error { return s.Create(context.Background(), fixedIntervalSpec("y", time.Minute)) }},
|
||||
{"Get", func() error { _, _, err := s.Get(context.Background(), "x"); return err }},
|
||||
{"List", func() error { _, err := s.List(context.Background()); return err }},
|
||||
{"Update", func() error { return s.Update(context.Background(), "x", Patch{Detail: new("z")}) }},
|
||||
{"Delete", func() error { return s.Delete(context.Background(), "x") }},
|
||||
{"MarkRun", func() error { return s.MarkRun(context.Background(), "x", time.Now()) }},
|
||||
{"Due", func() error { _, err := s.Due(context.Background(), time.Now()); return err }},
|
||||
}
|
||||
// Close itself is documented as idempotent (returns nil on the
|
||||
// second call), so it does NOT appear in the ErrClosed list above.
|
||||
if err := s.Close(); err != nil {
|
||||
t.Errorf("second Close = %v, want nil", err)
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.fn()
|
||||
if !errors.Is(err, ErrClosed) {
|
||||
t.Errorf("%s after Close = %v, want ErrClosed", tc.name, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// --- P8: same-path stores share the in-process mutex ------------------
|
||||
|
||||
func TestTwoStoresForSamePathSerialiseInProcess(t *testing.T) {
|
||||
// This is the test that fails without process_lock.go. With it,
|
||||
// 50/50 writes from each side land; without it, half the data
|
||||
// disappears. Mirrors TestConcurrentWritersAreSerialised but in
|
||||
// its own right so the regression is named.
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
a, _ := Open(path)
|
||||
defer a.Close()
|
||||
b, _ := Open(path)
|
||||
defer b.Close()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := range 25 {
|
||||
wg.Add(2)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
_ = a.Create(context.Background(), fixedIntervalSpec("a"+itoa(i), time.Minute))
|
||||
}(i)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
_ = b.Create(context.Background(), fixedIntervalSpec("b"+itoa(i), time.Minute))
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
jobs, _ := a.List(context.Background())
|
||||
if len(jobs) != 50 {
|
||||
t.Errorf("after 50 concurrent writes from two stores, %d persisted, want 50", len(jobs))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,509 @@
|
||||
// Package cronstore is the persistent job store backing the scheduling
|
||||
// ticker engine. Tests in this file are the spec: an implementing agent
|
||||
// must make every test in this file pass WITHOUT modifying the tests
|
||||
// themselves.
|
||||
//
|
||||
// The store's only contract with the engine is scheduling.JobSource: one
|
||||
// file-backed JSON document, cross-process locked, with version-stamped
|
||||
// schema so future slices can extend it without breaking old files.
|
||||
package cronstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
||||
)
|
||||
|
||||
// helpers
|
||||
|
||||
func newTestStore(t *testing.T) *Store {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
s, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open(%s): %v", path, err)
|
||||
}
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
return s
|
||||
}
|
||||
|
||||
func mustCreate(t *testing.T, s *Store, spec JobSpec) {
|
||||
t.Helper()
|
||||
if err := s.Create(context.Background(), spec); err != nil {
|
||||
t.Fatalf("Create(%q): %v", spec.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func fixedIntervalSpec(id string, every time.Duration) JobSpec {
|
||||
return JobSpec{
|
||||
ID: id,
|
||||
Pool: string(scheduling.PoolParallel),
|
||||
Detail: "test job " + id,
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleInterval,
|
||||
Interval: every,
|
||||
},
|
||||
Payload: Payload{Text: "hello " + id},
|
||||
}
|
||||
}
|
||||
|
||||
// --- open / close ------------------------------------------------------------
|
||||
|
||||
func TestOpenMissingFileReturnsEmpty(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s, err := Open(filepath.Join(dir, "jobs.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
jobs, err := s.List(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if len(jobs) != 0 {
|
||||
t.Errorf("List on missing file = %d jobs, want 0", len(jobs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenCreatesParentDirectory(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
deep := filepath.Join(dir, "a", "b", "c", "jobs.json")
|
||||
s, err := Open(deep)
|
||||
if err != nil {
|
||||
t.Fatalf("Open with missing parent: %v", err)
|
||||
}
|
||||
defer s.Close()
|
||||
mustCreate(t, s, fixedIntervalSpec("x", time.Minute))
|
||||
if _, err := os.Stat(deep); err != nil {
|
||||
t.Fatalf("expected file to exist at %s: %v", deep, err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- schema versioning -------------------------------------------------------
|
||||
|
||||
func TestPersistedFileHasVersionStamp(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
s, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
mustCreate(t, s, fixedIntervalSpec("x", time.Minute))
|
||||
_ = s.Close()
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(data), `"schema_version":1`) {
|
||||
t.Errorf("file does not contain schema_version:1 stamp: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenRejectsUnknownSchemaVersion(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
if err := os.WriteFile(path, []byte(`{"schema_version":999,"jobs":[]}`), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
if _, err := Open(path); err == nil {
|
||||
t.Fatal("Open with future schema version succeeded; want error")
|
||||
} else if !errors.Is(err, ErrUnsupportedSchema) {
|
||||
t.Errorf("error = %v, want ErrUnsupportedSchema", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- create / read -----------------------------------------------------------
|
||||
|
||||
func TestCreateAndGetRoundTrip(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
want := JobSpec{
|
||||
ID: "morning-status",
|
||||
Pool: string(scheduling.PoolSequential),
|
||||
Detail: "morning status",
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleInterval,
|
||||
Interval: 30 * time.Minute,
|
||||
},
|
||||
Payload: Payload{Text: "status please"},
|
||||
}
|
||||
mustCreate(t, s, want)
|
||||
|
||||
got, ok, err := s.Get(context.Background(), want.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Get: %v", err)
|
||||
}
|
||||
if !ok {
|
||||
t.Fatal("Get returned ok=false")
|
||||
}
|
||||
if got.ID != want.ID {
|
||||
t.Errorf("ID = %q, want %q", got.ID, want.ID)
|
||||
}
|
||||
if got.Pool != want.Pool {
|
||||
t.Errorf("Pool = %q, want %q", got.Pool, want.Pool)
|
||||
}
|
||||
if got.Detail != want.Detail {
|
||||
t.Errorf("Detail = %q, want %q", got.Detail, want.Detail)
|
||||
}
|
||||
if got.Schedule.Kind != want.Schedule.Kind {
|
||||
t.Errorf("Schedule.Kind = %q, want %q", got.Schedule.Kind, want.Schedule.Kind)
|
||||
}
|
||||
if got.Schedule.Interval != want.Schedule.Interval {
|
||||
t.Errorf("Schedule.Interval = %v, want %v", got.Schedule.Interval, want.Schedule.Interval)
|
||||
}
|
||||
if got.Payload.Text != want.Payload.Text {
|
||||
t.Errorf("Payload.Text = %q, want %q", got.Payload.Text, want.Payload.Text)
|
||||
}
|
||||
if got.NextRun.IsZero() {
|
||||
t.Error("NextRun is zero; Create must compute it for an interval job")
|
||||
}
|
||||
if got.Created.IsZero() {
|
||||
t.Error("Created is zero; Create must stamp it")
|
||||
}
|
||||
if got.Updated.IsZero() {
|
||||
t.Error("Updated is zero; Create must stamp it")
|
||||
}
|
||||
if !got.Created.Equal(got.Updated) {
|
||||
t.Errorf("Created %v != Updated %v on a fresh create", got.Created, got.Updated)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMissingReturnsNotFound(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
_, ok, err := s.Get(context.Background(), "nope")
|
||||
if err != nil {
|
||||
t.Fatalf("Get: %v", err)
|
||||
}
|
||||
if ok {
|
||||
t.Error("Get of missing id returned ok=true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRejectsEmptyID(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
if err := s.Create(context.Background(), fixedIntervalSpec("", time.Minute)); err == nil {
|
||||
t.Fatal("Create with empty id succeeded; want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRejectsUnknownScheduleKind(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
spec := fixedIntervalSpec("x", time.Minute)
|
||||
spec.Schedule.Kind = "magic"
|
||||
if err := s.Create(context.Background(), spec); err == nil {
|
||||
t.Fatal("Create with unknown schedule kind succeeded; want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRejectsDuplicateID(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, fixedIntervalSpec("dup", time.Minute))
|
||||
if err := s.Create(context.Background(), fixedIntervalSpec("dup", time.Hour)); err == nil {
|
||||
t.Fatal("Create with duplicate id succeeded; want error")
|
||||
}
|
||||
}
|
||||
|
||||
// --- update with merge semantics --------------------------------------------
|
||||
|
||||
func TestUpdatePreservesFieldsNotInPatch(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, JobSpec{
|
||||
ID: "patch-me",
|
||||
Pool: string(scheduling.PoolParallel),
|
||||
Detail: "original detail",
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleInterval,
|
||||
Interval: 10 * time.Minute,
|
||||
},
|
||||
Payload: Payload{Text: "original payload"},
|
||||
})
|
||||
|
||||
if err := s.Update(context.Background(), "patch-me", Patch{
|
||||
Detail: new("updated detail"),
|
||||
}); err != nil {
|
||||
t.Fatalf("Update: %v", err)
|
||||
}
|
||||
|
||||
got, _, _ := s.Get(context.Background(), "patch-me")
|
||||
if got.Detail != "updated detail" {
|
||||
t.Errorf("Detail = %q, want %q", got.Detail, "updated detail")
|
||||
}
|
||||
// Untouched fields must survive.
|
||||
if got.Pool != string(scheduling.PoolParallel) {
|
||||
t.Errorf("Pool = %q, want %q (merge must preserve)", got.Pool, scheduling.PoolParallel)
|
||||
}
|
||||
if got.Schedule.Interval != 10*time.Minute {
|
||||
t.Errorf("Schedule.Interval = %v, want %v (merge must preserve)", got.Schedule.Interval, 10*time.Minute)
|
||||
}
|
||||
if got.Payload.Text != "original payload" {
|
||||
t.Errorf("Payload.Text = %q, want %q (merge must preserve)", got.Payload.Text, "original payload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateAdvancesUpdatedTimestamp(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, fixedIntervalSpec("ts", time.Minute))
|
||||
first, _, _ := s.Get(context.Background(), "ts")
|
||||
|
||||
// Sleep long enough that Updated must move forward even on coarse
|
||||
// clocks. mtime resolution on some filesystems is the second.
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
|
||||
if err := s.Update(context.Background(), "ts", Patch{Detail: new("new")}); err != nil {
|
||||
t.Fatalf("Update: %v", err)
|
||||
}
|
||||
got, _, _ := s.Get(context.Background(), "ts")
|
||||
if !got.Updated.After(first.Updated) {
|
||||
t.Errorf("Updated did not advance: first=%v got=%v", first.Updated, got.Updated)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateRecomputesNextRunWhenScheduleChanges(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, fixedIntervalSpec("rs", time.Hour))
|
||||
first, _, _ := s.Get(context.Background(), "rs")
|
||||
|
||||
// Change the interval; NextRun must move.
|
||||
if err := s.Update(context.Background(), "rs", Patch{
|
||||
Schedule: &Schedule{Kind: ScheduleInterval, Interval: 2 * time.Minute},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update: %v", err)
|
||||
}
|
||||
got, _, _ := s.Get(context.Background(), "rs")
|
||||
if got.Schedule.Interval != 2*time.Minute {
|
||||
t.Errorf("Schedule.Interval = %v, want %v", got.Schedule.Interval, 2*time.Minute)
|
||||
}
|
||||
if !got.NextRun.Before(first.NextRun) {
|
||||
t.Errorf("NextRun did not move earlier after shortening interval: first=%v got=%v", first.NextRun, got.NextRun)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateMissingReturnsNotFound(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
if err := s.Update(context.Background(), "ghost", Patch{Detail: new("x")}); err == nil {
|
||||
t.Fatal("Update of missing id succeeded; want error")
|
||||
}
|
||||
}
|
||||
|
||||
// --- delete ------------------------------------------------------------------
|
||||
|
||||
func TestDeleteRemovesJob(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, fixedIntervalSpec("bye", time.Minute))
|
||||
if err := s.Delete(context.Background(), "bye"); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
if _, ok, _ := s.Get(context.Background(), "bye"); ok {
|
||||
t.Error("Get after Delete returned ok=true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteMissingIsIdempotent(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
// First delete is a no-op on a missing id; second delete must not panic
|
||||
// and must not return an error. CLI retries should be safe.
|
||||
if err := s.Delete(context.Background(), "never-existed"); err != nil {
|
||||
t.Errorf("Delete(missing) = %v, want nil", err)
|
||||
}
|
||||
if err := s.Delete(context.Background(), "never-existed"); err != nil {
|
||||
t.Errorf("Delete(missing) again = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- list --------------------------------------------------------------------
|
||||
|
||||
func TestListReturnsAllJobs(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, fixedIntervalSpec("a", time.Minute))
|
||||
mustCreate(t, s, fixedIntervalSpec("b", time.Minute))
|
||||
mustCreate(t, s, fixedIntervalSpec("c", time.Minute))
|
||||
|
||||
jobs, err := s.List(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if len(jobs) != 3 {
|
||||
t.Errorf("List returned %d jobs, want 3", len(jobs))
|
||||
}
|
||||
}
|
||||
|
||||
// --- JobSource.Due -----------------------------------------------------------
|
||||
|
||||
func TestDueReturnsOnlyJobsWhoseNextRunIsInThePast(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
now := time.Date(2026, 8, 28, 12, 0, 0, 0, time.UTC)
|
||||
|
||||
// past: due
|
||||
past := fixedIntervalSpec("past", 30*time.Minute)
|
||||
past.NextRun = now.Add(-time.Minute)
|
||||
// future: not due
|
||||
future := fixedIntervalSpec("future", 30*time.Minute)
|
||||
future.NextRun = now.Add(time.Hour)
|
||||
mustCreate(t, s, past)
|
||||
mustCreate(t, s, future)
|
||||
|
||||
got, err := s.Due(context.Background(), now)
|
||||
if err != nil {
|
||||
t.Fatalf("Due: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("Due returned %d jobs, want 1: %+v", len(got), got)
|
||||
}
|
||||
if got[0].ID != "past" {
|
||||
t.Errorf("due job = %q, want %q", got[0].ID, "past")
|
||||
}
|
||||
if got[0].Pool != scheduling.PoolParallel {
|
||||
t.Errorf("due job pool = %q, want %q", got[0].Pool, scheduling.PoolParallel)
|
||||
}
|
||||
if got[0].Detail != "test job past" {
|
||||
t.Errorf("due job detail = %q, want %q", got[0].Detail, "test job past")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDueOnEmptyStoreReturnsEmptySlice(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
got, err := s.Due(context.Background(), time.Now())
|
||||
if err != nil {
|
||||
t.Fatalf("Due: %v", err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("Due on empty = %d jobs, want 0", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestImplementsSchedulingJobSource(t *testing.T) {
|
||||
// Static, compile-time check: *Store must satisfy scheduling.JobSource
|
||||
// so the ticker engine can poll it without knowing the file layout.
|
||||
var _ scheduling.JobSource = (*Store)(nil)
|
||||
}
|
||||
|
||||
func TestMarkRunAdvancesNextRun(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
mustCreate(t, s, fixedIntervalSpec("recurring", time.Hour))
|
||||
|
||||
first, _, _ := s.Get(context.Background(), "recurring")
|
||||
runAt := first.NextRun.Add(time.Minute) // pretend engine ran a bit late
|
||||
if err := s.MarkRun(context.Background(), "recurring", runAt); err != nil {
|
||||
t.Fatalf("MarkRun: %v", err)
|
||||
}
|
||||
got, _, _ := s.Get(context.Background(), "recurring")
|
||||
if got.LastRun == nil {
|
||||
t.Fatal("LastRun is nil after MarkRun")
|
||||
}
|
||||
if !got.LastRun.Equal(runAt) {
|
||||
t.Errorf("LastRun = %v, want %v", got.LastRun, runAt)
|
||||
}
|
||||
wantNext := runAt.Add(time.Hour)
|
||||
if !got.NextRun.Equal(wantNext) {
|
||||
t.Errorf("NextRun = %v, want %v (last_run + interval)", got.NextRun, wantNext)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkRunMissingReturnsNotFound(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
if err := s.MarkRun(context.Background(), "ghost", time.Now()); err == nil {
|
||||
t.Fatal("MarkRun of missing id succeeded; want error")
|
||||
}
|
||||
}
|
||||
|
||||
// --- partial-write handling --------------------------------------------------
|
||||
|
||||
func TestOpenTruncatedFileReturnsExplicitError(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
// Header present, body cut mid-array. Must not panic on read; the
|
||||
// engine surfaces this as phase="source" KindJobError, not a crash.
|
||||
if err := os.WriteFile(path, []byte(`{"schema_version":1,"jobs":[{"id":"x","`), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
_, err := Open(path)
|
||||
if err == nil {
|
||||
t.Fatal("Open on truncated file succeeded; want explicit error")
|
||||
}
|
||||
if errors.Is(err, ErrUnsupportedSchema) {
|
||||
t.Errorf("error = ErrUnsupportedSchema; want a corrupt-file error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenGarbageFileReturnsExplicitError(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
if err := os.WriteFile(path, []byte("not json at all"), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile: %v", err)
|
||||
}
|
||||
if _, err := Open(path); err == nil {
|
||||
t.Fatal("Open on garbage file succeeded; want explicit error")
|
||||
}
|
||||
}
|
||||
|
||||
// --- cross-process locking ---------------------------------------------------
|
||||
|
||||
func TestConcurrentWritersAreSerialised(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "jobs.json")
|
||||
a, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open a: %v", err)
|
||||
}
|
||||
defer a.Close()
|
||||
b, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open b: %v", err)
|
||||
}
|
||||
defer b.Close()
|
||||
|
||||
// 50 writes from each writer, on the same file, interleaved. Without
|
||||
// the lock, two writers would race and the file would end up either
|
||||
// truncated or holding a non-deterministic subset. The lock makes
|
||||
// each writer wait, so all 100 distinct ids end up persisted.
|
||||
var wg sync.WaitGroup
|
||||
for i := range 50 {
|
||||
wg.Add(2)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
id := "a" + itoa(i)
|
||||
_ = a.Create(context.Background(), fixedIntervalSpec(id, time.Minute))
|
||||
}(i)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
id := "b" + itoa(i)
|
||||
_ = b.Create(context.Background(), fixedIntervalSpec(id, time.Minute))
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
jobs, err := a.List(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if len(jobs) != 100 {
|
||||
t.Errorf("after 100 concurrent writes, %d jobs persisted, want 100", len(jobs))
|
||||
}
|
||||
}
|
||||
|
||||
// itoa avoids strconv import in tests.
|
||||
func itoa(n int) string {
|
||||
if n == 0 {
|
||||
return "0"
|
||||
}
|
||||
var buf [20]byte
|
||||
i := len(buf)
|
||||
for n > 0 {
|
||||
i--
|
||||
buf[i] = byte('0' + n%10)
|
||||
n /= 10
|
||||
}
|
||||
return string(buf[i:])
|
||||
}
|
||||
|
||||
//go:fix inline
|
||||
func stringPtr(s string) *string { return new(s) }
|
||||
Reference in new issue
Block a user