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

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

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

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

54 lines
1.5 KiB
Go

//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