The schedules feature no longer runs on it: jobs live in the state store as the schedules control-plane document, read and written through the control-plane client (scheduleResourceStore), and the legacy jobs.json migration into that document has run its course. What remained of internal/infrastructure/cronstore was 1,600 lines of file store, OS-level cross-process locking and schema-versioned JSON envelope -- plus the schedule vocabulary held hostage inside it. The vocabulary moves to internal/domain/scheduling, which is where migration-decisions already said a schedules vocabulary belongs: a domain vocabulary package must not import infrastructure. JobSpec, Target, Payload, the kind constants, Schedule and Duration move unchanged -- same wire shape, same JSON tags -- with the two sentinel errors callers matched outside the package (ErrJobNotFound, ErrScheduleUnsupported) and ErrInvalidSpec, which the schedule arithmetic itself raises. The store's own sentinels die with the store. The legacy migration path (migrateLegacySchedules, readLegacyJobs) goes with it: its only dependency was cronstore.Open, and its work is done. crondelivery keeps its consumer-owned contracts; the compile-time assertions that pinned *cronstore.Store move to the production implementation, scheduleResourceStore, in internal/app/archied. The package's fixtures swap the real file store for an in-memory one with the same Get/MarkRun/Due semantics, so the run-recording acceptance test still asserts against real schedule arithmetic. A future storage backend (PocketBase) implements the same vocabulary.
74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package controlplane
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
|
)
|
|
|
|
const SchedulesKind = "schedules"
|
|
|
|
func scheduleDefinition() Definition {
|
|
return Definition{
|
|
Kind: SchedulesKind,
|
|
Title: "Schedules",
|
|
Document: []scheduling.JobSpec{},
|
|
ApplyMode: "live",
|
|
Seed: func(config.Config) any { return []scheduling.JobSpec{} },
|
|
Validate: func(input []byte) error {
|
|
return validateAs(input, validateSchedules)
|
|
},
|
|
Normalize: normalizeSchedules,
|
|
}
|
|
}
|
|
|
|
func validateSchedules(jobs []scheduling.JobSpec) error {
|
|
seen := make(map[string]struct{}, len(jobs))
|
|
for _, job := range jobs {
|
|
if strings.TrimSpace(job.ID) == "" {
|
|
return fmt.Errorf("schedule ID is required")
|
|
}
|
|
if _, exists := seen[job.ID]; exists {
|
|
return fmt.Errorf("duplicate schedule %q", job.ID)
|
|
}
|
|
seen[job.ID] = struct{}{}
|
|
if job.Kind != scheduling.KindWorkflow {
|
|
return fmt.Errorf("schedule %q has unsupported kind %q", job.ID, job.Kind)
|
|
}
|
|
if err := job.Schedule.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := scheduling.Pool(job.Pool).Validate(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func normalizeSchedules(input []byte) ([]byte, error) {
|
|
var jobs []scheduling.JobSpec
|
|
if err := json.Unmarshal(input, &jobs); err != nil {
|
|
return nil, err
|
|
}
|
|
now := time.Now().UTC()
|
|
for i := range jobs {
|
|
jobs[i].Schedule = jobs[i].Schedule.Resolved()
|
|
if jobs[i].NextRun.IsZero() {
|
|
next, err := jobs[i].Schedule.FirstRun(now)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
jobs[i].NextRun = next.UTC()
|
|
}
|
|
if jobs[i].Created.IsZero() {
|
|
jobs[i].Created = now
|
|
}
|
|
jobs[i].Updated = now
|
|
}
|
|
return json.Marshal(jobs)
|
|
}
|