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:
1 parent
04d4f784b2
commit
f29169c2cf
11 files changed
+229
-33
No files matched your search
@@ -279,7 +279,7 @@ func chatSpec(id, chatID, text string) cronstore.JobSpec {
|
||||
ID: id,
|
||||
Detail: "test job " + id,
|
||||
Kind: cronstore.KindChat,
|
||||
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: time.Hour},
|
||||
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)},
|
||||
Target: cronstore.Target{ChatID: chatID},
|
||||
Payload: cronstore.Payload{Text: text},
|
||||
}
|
||||
@@ -291,7 +291,7 @@ func workflowSpec(id, detail, body string) cronstore.JobSpec {
|
||||
ID: id,
|
||||
Detail: detail,
|
||||
Kind: cronstore.KindWorkflow,
|
||||
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: time.Hour},
|
||||
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)},
|
||||
Payload: cronstore.Payload{Text: body},
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ func routerFixture(t *testing.T, kind string) (*Router, *countingRunner, *counti
|
||||
ID: "routed",
|
||||
Detail: "routed job",
|
||||
Kind: kind,
|
||||
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: time.Hour},
|
||||
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)},
|
||||
})
|
||||
chat, workflow := &countingRunner{}, &countingRunner{}
|
||||
sink := &recordingSink{}
|
||||
@@ -120,7 +120,7 @@ func TestRouterNilSinkDoesNotPanicOnUnknownKind(t *testing.T) {
|
||||
createJob(t, s, cronstore.JobSpec{
|
||||
ID: "routed",
|
||||
Kind: "teleport",
|
||||
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: time.Hour},
|
||||
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(time.Hour)},
|
||||
})
|
||||
chat, workflow := &countingRunner{}, &countingRunner{}
|
||||
r, err := NewRouter(s, map[string]scheduling.Runner{
|
||||
@@ -258,7 +258,7 @@ func TestDailyStatusSummaryEndToEnd(t *testing.T) {
|
||||
ID: "daily-status",
|
||||
Detail: "daily status summary",
|
||||
Kind: cronstore.KindChat,
|
||||
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: 24 * time.Hour},
|
||||
Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: cronstore.Duration(24 * time.Hour)},
|
||||
Target: cronstore.Target{ChatID: chatID},
|
||||
Payload: cronstore.Payload{Text: dailyStatusSummary},
|
||||
// Due now: create it with a next_run in the past, the state a
|
||||
|
||||
@@ -1,10 +1,52 @@
|
||||
package cronstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Duration is a schedule interval as the document writes it: the human string
|
||||
// time.ParseDuration reads, never a nanosecond count. It reads the count too,
|
||||
// because every schedule stored before this shape existed carries one -- both in
|
||||
// the control plane's schedules resource and in this package's own jobs table,
|
||||
// which persists the spec as JSON.
|
||||
//
|
||||
// The type is deliberately local, as config.Duration is for the file document
|
||||
// and channelDuration is in internal/app/controlplane: each document owns its own
|
||||
// wire form, and what is actually worth unifying later is the legacy-count
|
||||
// tolerance rather than the tag shape.
|
||||
type Duration time.Duration
|
||||
|
||||
func (d Duration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(time.Duration(d).String())
|
||||
}
|
||||
|
||||
func (d *Duration) UnmarshalJSON(data []byte) error {
|
||||
var text string
|
||||
if err := json.Unmarshal(data, &text); err == nil {
|
||||
value, err := time.ParseDuration(text)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: interval must be a Go duration string such as %q: %w", ErrInvalidSpec, "30m", err)
|
||||
}
|
||||
*d = Duration(value)
|
||||
return nil
|
||||
}
|
||||
var nanos int64
|
||||
if err := json.Unmarshal(data, &nanos); err != nil {
|
||||
return fmt.Errorf("%w: interval must be a Go duration string such as %q or a nanosecond count: %w", ErrInvalidSpec, "30m", err)
|
||||
}
|
||||
*d = Duration(nanos)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Std is the standard-library duration, for arithmetic and comparison.
|
||||
func (d Duration) Std() time.Duration { return time.Duration(d) }
|
||||
|
||||
// String renders the interval the way the document writes it, so an error
|
||||
// message or a log line reads "30m0s" rather than 1800000000000.
|
||||
func (d Duration) String() string { return time.Duration(d).String() }
|
||||
|
||||
// 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
|
||||
@@ -15,8 +57,10 @@ type Schedule struct {
|
||||
Kind string `json:"kind,omitempty"`
|
||||
|
||||
// Interval is the period for ScheduleInterval. Required when Kind
|
||||
// is ScheduleInterval; ignored otherwise.
|
||||
Interval time.Duration `json:"interval,omitempty"`
|
||||
// is ScheduleInterval; ignored otherwise. It is a Duration rather
|
||||
// than a time.Duration so the document carries "30m0s" and not the
|
||||
// nanosecond count that asked an operator to type 1800000000000.
|
||||
Interval Duration `json:"interval,omitempty"`
|
||||
|
||||
// Cron is the 5-field cron expression for ScheduleCron. Required
|
||||
// when Kind is ScheduleCron; ignored otherwise.
|
||||
@@ -98,9 +142,9 @@ 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 s.Start.Add(s.Interval.Std()), nil
|
||||
}
|
||||
return now.Add(s.Interval), nil
|
||||
return now.Add(s.Interval.Std()), nil
|
||||
case ScheduleOnce:
|
||||
if s.At == nil {
|
||||
return time.Time{}, fmt.Errorf("%w: once schedule has no at time", ErrScheduleUnsupported)
|
||||
@@ -127,7 +171,7 @@ func (s Schedule) Resolved() Schedule { return s.resolve() }
|
||||
func (s Schedule) nextRun(lastRun time.Time) (time.Time, error) {
|
||||
switch s.resolve().Kind {
|
||||
case ScheduleInterval:
|
||||
return lastRun.Add(s.Interval), nil
|
||||
return lastRun.Add(s.Interval.Std()), nil
|
||||
case ScheduleOnce:
|
||||
return time.Time{}, fmt.Errorf("%w: once schedule has no next run", ErrScheduleUnsupported)
|
||||
case ScheduleCron:
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package cronstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestScheduleIntervalWritesADurationString pins the document form: the interval
|
||||
// an operator edits must read as a duration, not as the nanosecond count that
|
||||
// asked them to type 1800000000000.
|
||||
func TestScheduleIntervalWritesADurationString(t *testing.T) {
|
||||
encoded, err := json.Marshal(Schedule{Kind: ScheduleInterval, Interval: Duration(30 * time.Minute)})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
if got := string(encoded); !strings.Contains(got, `"interval":"30m0s"`) {
|
||||
t.Errorf("marshalled schedule = %s, want interval as the string \"30m0s\"", got)
|
||||
}
|
||||
if strings.Contains(string(encoded), "1800000000000") {
|
||||
t.Errorf("marshalled schedule = %s, want no nanosecond count", encoded)
|
||||
}
|
||||
}
|
||||
|
||||
// TestScheduleIntervalReadsEveryStoredForm covers what the store already holds.
|
||||
// Every schedule written before this shape existed carries a nanosecond count, in
|
||||
// both the control plane's schedules resource and this package's own jobs table,
|
||||
// and both have to keep working without being re-saved first.
|
||||
func TestScheduleIntervalReadsEveryStoredForm(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
document string
|
||||
expected time.Duration
|
||||
}{
|
||||
{"legacy nanosecond count", `{"kind":"interval","interval":1800000000000}`, 30 * time.Minute},
|
||||
{"duration string", `{"kind":"interval","interval":"30m"}`, 30 * time.Minute},
|
||||
{"duration string, canonical", `{"kind":"interval","interval":"30m0s"}`, 30 * time.Minute},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var schedule Schedule
|
||||
if err := json.Unmarshal([]byte(tt.document), &schedule); err != nil {
|
||||
t.Fatalf("unmarshal %s: %v", tt.document, err)
|
||||
}
|
||||
if schedule.Interval.Std() != tt.expected {
|
||||
t.Errorf("Interval = %v, want %v", schedule.Interval, tt.expected)
|
||||
}
|
||||
if err := schedule.Validate(); err != nil {
|
||||
t.Errorf("Validate() = %v, want the decoded schedule to be usable", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestScheduleIntervalRefusesWhatItCannotUse keeps the tolerance above from
|
||||
// becoming indifference: a duration that does not parse must fail loudly rather
|
||||
// than decode as zero, because a zero interval is a job that never fires.
|
||||
func TestScheduleIntervalRefusesWhatItCannotUse(t *testing.T) {
|
||||
for _, tt := range []struct{ name, document string }{
|
||||
{"an unparseable duration", `{"kind":"interval","interval":"soon"}`},
|
||||
{"a fractional count", `{"kind":"interval","interval":1.5}`},
|
||||
{"a null interval", `{"kind":"interval","interval":null}`},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var schedule Schedule
|
||||
if err := json.Unmarshal([]byte(tt.document), &schedule); err == nil {
|
||||
t.Fatalf("unmarshal(%s) = nil, want a refusal: a silent zero interval never fires", tt.document)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestScheduleIntervalRoundTripsThroughTheStore pins the shape the jobs table
|
||||
// keeps, since it persists the spec as JSON rather than as columns.
|
||||
func TestScheduleIntervalRoundTripsThroughTheStore(t *testing.T) {
|
||||
spec := JobSpec{ID: "nightly", Schedule: Schedule{Kind: ScheduleInterval, Interval: Duration(90 * time.Minute)}}
|
||||
encoded, err := json.Marshal(spec)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(encoded), `"interval":"1h30m0s"`) {
|
||||
t.Fatalf("marshalled spec = %s, want the interval as \"1h30m0s\"", encoded)
|
||||
}
|
||||
var decoded JobSpec
|
||||
if err := json.Unmarshal(encoded, &decoded); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if decoded.Schedule.Interval.Std() != 90*time.Minute {
|
||||
t.Errorf("round-tripped Interval = %v, want 1h30m0s", decoded.Schedule.Interval)
|
||||
}
|
||||
}
|
||||
@@ -259,7 +259,7 @@ func TestCreateAndGetRoundTripWithUnicodeAndQuotes(t *testing.T) {
|
||||
Detail: "Hello\n\"世界\"\\back",
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleInterval,
|
||||
Interval: 30 * time.Minute,
|
||||
Interval: Duration(30 * time.Minute),
|
||||
},
|
||||
Payload: Payload{Text: "<script>alert(1)</script> & 中文"},
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func fixedIntervalSpec(id string, every time.Duration) JobSpec {
|
||||
Detail: "test job " + id,
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleInterval,
|
||||
Interval: every,
|
||||
Interval: Duration(every),
|
||||
},
|
||||
Payload: Payload{Text: "hello " + id},
|
||||
}
|
||||
@@ -138,7 +138,7 @@ func TestCreateAndGetRoundTrip(t *testing.T) {
|
||||
Detail: "morning status",
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleInterval,
|
||||
Interval: 30 * time.Minute,
|
||||
Interval: Duration(30 * time.Minute),
|
||||
},
|
||||
Payload: Payload{Text: "status please"},
|
||||
}
|
||||
@@ -228,7 +228,7 @@ func TestUpdatePreservesFieldsNotInPatch(t *testing.T) {
|
||||
Detail: "original detail",
|
||||
Schedule: Schedule{
|
||||
Kind: ScheduleInterval,
|
||||
Interval: 10 * time.Minute,
|
||||
Interval: Duration(10 * time.Minute),
|
||||
},
|
||||
Payload: Payload{Text: "original payload"},
|
||||
})
|
||||
@@ -247,7 +247,7 @@ func TestUpdatePreservesFieldsNotInPatch(t *testing.T) {
|
||||
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 {
|
||||
if got.Schedule.Interval.Std() != 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" {
|
||||
@@ -280,12 +280,12 @@ func TestUpdateRecomputesNextRunWhenScheduleChanges(t *testing.T) {
|
||||
|
||||
// Change the interval; NextRun must move.
|
||||
if err := s.Update(context.Background(), "rs", Patch{
|
||||
Schedule: &Schedule{Kind: ScheduleInterval, Interval: 2 * time.Minute},
|
||||
Schedule: &Schedule{Kind: ScheduleInterval, Interval: Duration(2 * time.Minute)},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update: %v", err)
|
||||
}
|
||||
got, _, _ := s.Get(context.Background(), "rs")
|
||||
if got.Schedule.Interval != 2*time.Minute {
|
||||
if got.Schedule.Interval.Std() != 2*time.Minute {
|
||||
t.Errorf("Schedule.Interval = %v, want %v", got.Schedule.Interval, 2*time.Minute)
|
||||
}
|
||||
if !got.NextRun.Before(first.NextRun) {
|
||||
|
||||
Reference in new issue
Block a user