package controlplanestore import ( "bytes" "encoding/json" "fmt" "time" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/scheduling" ) // ScheduleSettingsKind is the instance's schedule settings: the zone cron // expressions are read in, and how much run history is kept. const ScheduleSettingsKind = "schedule-settings" type scheduleSettings struct { TimeZone string `json:"time_zone" title:"Time zone"` RunRetention string `json:"run_retention" title:"Keep runs for"` RunRetentionCount int `json:"run_retention_count" title:"Runs kept per schedule"` } // defaultScheduleSettings keeps a week of runs, at most 100 per schedule. var defaultScheduleSettings = scheduleSettings{TimeZone: "UTC", RunRetention: "168h", RunRetentionCount: 100} func scheduleSettingsDefinition() Definition { return Definition{ Kind: ScheduleSettingsKind, Title: "Schedule settings", ApplyMode: "live", Document: scheduleSettings{}, Seed: func(config.Config) any { return defaultScheduleSettings }, Validate: func(input []byte) error { _, err := ScheduleSettings(input) return err }, } } // ScheduleSettings decodes and checks a stored schedule-settings document. // The State Store reads it with this same parser, so a value the control // plane accepted is one the scheduler can use. func ScheduleSettings(document []byte) (scheduling.Settings, error) { var doc scheduleSettings decoder := json.NewDecoder(bytes.NewReader(document)) decoder.DisallowUnknownFields() if err := decoder.Decode(&doc); err != nil { return scheduling.Settings{}, fmt.Errorf("decode %s: %w", ScheduleSettingsKind, err) } loc, err := time.LoadLocation(doc.TimeZone) if err != nil || doc.TimeZone == "" || doc.TimeZone == "Local" { return scheduling.Settings{}, fmt.Errorf("time_zone must be an IANA zone such as Australia/Sydney or UTC") } retention, err := time.ParseDuration(doc.RunRetention) if err != nil || retention < time.Hour { return scheduling.Settings{}, fmt.Errorf("run_retention must be a duration of at least 1h, such as 168h") } if doc.RunRetentionCount < 1 { return scheduling.Settings{}, fmt.Errorf("run_retention_count must be at least 1") } return scheduling.Settings{Location: loc, RunRetention: retention, RunRetentionCount: doc.RunRetentionCount}, nil }