fix(cronstore): write a schedule interval as a duration, not nanoseconds

The schedules document stored interval as a bare time.Duration, so its JSON was
the nanosecond count -- 1800000000000 for thirty minutes -- and the Web UI, which
now derives field metadata from the document type, correctly rendered a number
input. Same defect 02rm fixed for rate_limit.window, in the second stored time
this tree has. Nobody should be asked for a count of nanoseconds to configure a
schedule.

cronstore.Duration writes the string time.ParseDuration reads and reads that, a
nanosecond count, and nothing else: every schedule stored before this shape
existed carries a count, in both the control plane's schedules resource and this
package's own jobs table, which persists the spec as JSON. A value that parses as
neither is refused rather than decoding as zero, because a zero interval is a job
that never fires.

The derived schema needed no change and gets it for free -- which exposed a fix
the derivation itself needed: it classified durations by NAME, so cronstore.Duration
derived as an integer. durationLike now asks the type instead -- does it expose the
standard duration, and does it marshal as a quoted string -- so the next duration
type is classified correctly rather than silently rendering as a number box. That
failure mode was invisible until a live daemon showed it.

normalizeSchedules already re-marshals, so a stored document converges on its next
write while reads keep tolerating the count.

Verified: cronstore, crondelivery, controlplane, scheduling, webui and archied
suites, go vet ./..., go build ./..., golangci-lint run ./... (0 issues),
docs:check. Against a live daemon (8484): the catalog serves schedule.interval as
{type: string, format: duration}.
This commit is contained in:
sam committed 2026-09-22 04:22:46 +10:00
1 parent 04d4f784b2
commit f29169c2cf
11 files changed
+229 -33

No files matched your search

@@ -83,7 +83,7 @@ func legacyJob(id, kind string, interval time.Duration) cronstore.JobSpec {
stamped := time.Date(2026, 9, 1, 9, 0, 0, 0, time.UTC)
return cronstore.JobSpec{
ID: id, Kind: kind, Detail: "legacy " + id,
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: interval},
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(interval)},
NextRun: stamped, Created: stamped, Updated: stamped,
}
}
@@ -125,6 +125,10 @@ func decodeRenamingLegacyKeys(data []byte, legacy map[string]string, v any) erro
// those documents keep working without being re-saved.
type channelDuration time.Duration
// Std is the standard-library duration, for arithmetic and for the marker
// schema.go uses to recognise a string-form duration (see durationLike).
func (d channelDuration) Std() time.Duration { return time.Duration(d) }
func (d channelDuration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
+1 -1
View File
@@ -12,7 +12,7 @@ func TestScheduleDefinitionComputesFirstRunAndRejectsDuplicateIDs(t *testing.T)
t.Parallel()
definition := scheduleDefinition()
jobs := []cronstore.JobSpec{{ID: "daily", Kind: cronstore.KindWorkflow, Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: time.Hour}}}
jobs := []cronstore.JobSpec{{ID: "daily", Kind: cronstore.KindWorkflow, Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)}}}
input, err := json.Marshal(jobs)
if err != nil {
t.Fatal(err)
+29 -14
View File
@@ -6,8 +6,6 @@ import (
"reflect"
"strings"
"time"
"github.com/samcharles93/archie-core/internal/config"
)
// bareObjectSchema is what a descriptor advertises when its definition names no
@@ -16,11 +14,28 @@ import (
// (archie-core-2xs6).
const bareObjectSchema = `{"type":"object"}`
var (
configDurationType = reflect.TypeFor[config.Duration]()
channelDurationType = reflect.TypeFor[channelDuration]()
timeType = reflect.TypeFor[time.Time]()
)
var timeType = reflect.TypeFor[time.Time]()
// stdDuration is the marker this tree's string-form duration types satisfy:
// config.Duration, channelDuration and cronstore.Duration each expose the
// standard duration for arithmetic.
var stdDuration = reflect.TypeOf((*interface{ Std() time.Duration })(nil)).Elem()
// durationLike reports whether t is a duration type that writes itself as the
// string time.ParseDuration reads, rather than as its nanosecond count.
//
// It asks the type rather than listing the ones that exist, because a list goes
// stale the moment a fourth duration type appears and the failure is silent in
// the worst direction: the field derives as an integer and a client renders a
// number box for a duration. That is exactly what the enumerated version of this
// function did to cronstore.Duration when the schedules interval became one.
func durationLike(t reflect.Type) bool {
if !t.Implements(stdDuration) {
return false
}
encoded, err := json.Marshal(reflect.Zero(t).Interface())
return err == nil && len(encoded) > 0 && encoded[0] == '"'
}
// schemaJSON renders the JSON Schema a resource descriptor advertises, derived
// from the document type the definition owns rather than written out beside it.
@@ -60,14 +75,14 @@ func schemaOf(t reflect.Type) map[string]any {
for t.Kind() == reflect.Pointer {
t = t.Elem()
}
switch t {
case configDurationType, channelDurationType:
// Both marshal as the string time.ParseDuration reads, so the schema
// says so and a client can offer a duration affordance instead of a
// number box. This is the rule that reaches every duration field in the
// catalogue, not just the one that prompted it.
switch {
case durationLike(t):
// The type writes the string time.ParseDuration reads, so the schema says
// so and a client can offer a duration affordance instead of a number
// box. This rule reaches every duration field in the catalogue, not just
// the one that prompted it.
return map[string]any{"type": "string", "format": "duration"}
case timeType:
case t == timeType:
return map[string]any{"type": "string", "format": "date-time"}
}
switch t.Kind() {
+42
View File
@@ -136,6 +136,48 @@ func TestEveryResourceDescriptorDerivesProperties(t *testing.T) {
}
}
// TestSchedulesDescriptorFormatsTheInterval is the same payoff one document over:
// the schedules interval is a Duration now, so the derivation turns it into a
// formatted string with no schema authored for it (archie-core-4w0c).
func TestSchedulesDescriptorFormatsTheInterval(t *testing.T) {
server := testServer(t, nil)
catalog, err := server.Catalog(t.Context(), nil)
if err != nil {
t.Fatalf("Catalog: %v", err)
}
var schema struct {
Items struct {
Properties struct {
Schedule struct {
Properties struct {
Interval struct {
Type string `json:"type"`
Format string `json:"format"`
} `json:"interval"`
} `json:"properties"`
} `json:"schedule"`
} `json:"properties"`
} `json:"items"`
}
found := false
for _, descriptor := range catalog.Resources {
if descriptor.Kind != SchedulesKind {
continue
}
found = true
if err := json.Unmarshal([]byte(descriptor.SchemaJson), &schema); err != nil {
t.Fatalf("unmarshal schedules schema: %v", err)
}
}
if !found {
t.Fatal("schedules is not in the catalog")
}
interval := schema.Items.Properties.Schedule.Properties.Interval
if interval.Type != "string" || interval.Format != "duration" {
t.Errorf("schedule.interval = %+v, want a duration-formatted string: the document carries 30m0s, not nanoseconds", interval)
}
}
// TestChannelSettingsDescriptorFormatsTheRateLimitWindow is the concrete payoff:
// the field that asked an operator for a nanosecond count now carries a format a
// client can render an affordance for, and it arrives by derivation rather than