diff --git a/internal/infrastructure/crondelivery/contract.go b/internal/infrastructure/crondelivery/contract.go index db36e0148..0496c772b 100644 --- a/internal/infrastructure/crondelivery/contract.go +++ b/internal/infrastructure/crondelivery/contract.go @@ -26,6 +26,7 @@ import ( "context" "errors" "fmt" + "time" "github.com/samcharles93/archie-core/internal/domain/scheduling" "github.com/samcharles93/archie-core/internal/infrastructure/cronstore" @@ -43,6 +44,32 @@ type SpecLookup interface { Get(ctx context.Context, id string) (cronstore.JobSpec, bool, error) } +// RunRecorder advances a job's schedule once a run has completed. It is +// declared here (consumer-owned) for the same reason SpecLookup is, and +// *cronstore.Store satisfies it as written. +// +// MarkRun is the only step that moves a job's NextRun, so a store whose Due +// keeps reporting an already-completed job keeps handing it back on every +// tick: an interval job fires once per tick instead of once per interval. The +// record is written only for a successful run, matching the store's own +// contract that an interval or cron schedule fires "after the last successful +// run" -- a failed run retries at the tick rate rather than silently skipping +// its slot. +type RunRecorder interface { + // MarkRun records that id ran at runAt and advances NextRun to the + // schedule's next firing. + MarkRun(ctx context.Context, id string, runAt time.Time) error +} + +// RouterStore is what the Router needs from persistence: resolve a job id to +// its spec, and record the run once it succeeds. The per-kind runners still +// take the narrower SpecLookup -- they dispatch one already-hydrated job and +// have no business moving its schedule. +type RouterStore interface { + SpecLookup + RunRecorder +} + // Courier sends one chat message. It is the whole outbound surface this // package needs, and it is injected: the concrete channel client lives in the // deployment, so an operator switching transports changes nothing here. That @@ -83,4 +110,8 @@ func hydrate(ctx context.Context, specs SpecLookup, job scheduling.Job) (cronsto } // compile-time check: the store is the lookup a deployment hands in unchanged. -var _ SpecLookup = (*cronstore.Store)(nil) +var ( + _ SpecLookup = (*cronstore.Store)(nil) + _ RunRecorder = (*cronstore.Store)(nil) + _ RouterStore = (*cronstore.Store)(nil) +) diff --git a/internal/infrastructure/crondelivery/helpers_test.go b/internal/infrastructure/crondelivery/helpers_test.go index de7e358c6..fcf528da4 100644 --- a/internal/infrastructure/crondelivery/helpers_test.go +++ b/internal/infrastructure/crondelivery/helpers_test.go @@ -103,6 +103,12 @@ func (f *fakeLookup) Get(_ context.Context, id string) (cronstore.JobSpec, bool, return spec, ok, nil } +// MarkRun satisfies RouterStore. It deliberately does nothing: these tests +// exercise the runner's and the router's own dispatch guards, and the record +// step is covered against the real store in +// TestRouterAdvancesTheScheduleAfterASuccessfulRun. +func (f *fakeLookup) MarkRun(_ context.Context, _ string, _ time.Time) error { return nil } + // fakeCourier records what was sent and can be made to fail or to block until // cancelled. type fakeCourier struct { diff --git a/internal/infrastructure/crondelivery/router.go b/internal/infrastructure/crondelivery/router.go index fa9c8784d..d2421e8a3 100644 --- a/internal/infrastructure/crondelivery/router.go +++ b/internal/infrastructure/crondelivery/router.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "strings" + "time" "github.com/samcharles93/archie-core/internal/domain/scheduling" "github.com/samcharles93/archie-core/internal/events" @@ -21,7 +22,9 @@ const phaseDispatch = "dispatch" // Router is the single Runner the engine is given. It resolves a due job's // kind from its persisted spec and hands the job to that kind's runner, -// keeping the engine unaware that more than one delivery exists. +// keeping the engine unaware that more than one delivery exists. Once a run +// has succeeded it also records the run, which is what moves a recurring job's +// NextRun. // // An unknown kind is refused, not failed. It is reported on the event stream // as KindJobError with phase "dispatch" — naming the gate, so an operator can @@ -30,7 +33,7 @@ const phaseDispatch = "dispatch" // (phase "run"), conflating the two, and would leave the job looking like it // tried and broke on every tick. type Router struct { - specs SpecLookup + specs RouterStore runners map[string]scheduling.Runner sink scheduling.Sink } @@ -42,7 +45,7 @@ type Router struct { // // An empty kind resolves to cronstore.KindChat, so a deployment only needs a // mapping for the kinds it actually uses. -func NewRouter(specs SpecLookup, runners map[string]scheduling.Runner, sink scheduling.Sink) (*Router, error) { +func NewRouter(specs RouterStore, runners map[string]scheduling.Runner, sink scheduling.Sink) (*Router, error) { if specs == nil { return nil, errors.New("crondelivery: spec lookup must not be nil") } @@ -71,7 +74,27 @@ func (r *Router) Run(ctx context.Context, job scheduling.Job) error { r.refuse(job, kind) return nil } - return runner.Run(ctx, job) + if err := runner.Run(ctx, job); err != nil { + return err + } + return r.recordRun(ctx, job) +} + +// recordRun advances the job's schedule after a successful run. Without it the +// store has no record that the job ran, so its NextRun stays in the past and +// Due reports the job again on every tick: a job scheduled hourly fires once +// per tick instead. +// +// A schedule kind with no recurring next run -- a one-shot -- reports +// cronstore.ErrScheduleUnsupported, which is the job's definition rather than a +// run failure, so it is swallowed. Anything else is returned: the run happened +// but the bookkeeping did not, and hiding that would leave the job silently +// re-firing. +func (r *Router) recordRun(ctx context.Context, job scheduling.Job) error { + if err := r.specs.MarkRun(ctx, job.ID, time.Now()); err != nil && !errors.Is(err, cronstore.ErrScheduleUnsupported) { + return fmt.Errorf("crondelivery: record run of job %q: %w", job.ID, err) + } + return nil } // refuse reports a job whose kind has no runner. The data map matches the diff --git a/internal/infrastructure/crondelivery/router_test.go b/internal/infrastructure/crondelivery/router_test.go index 328acb51b..1ef8940e5 100644 --- a/internal/infrastructure/crondelivery/router_test.go +++ b/internal/infrastructure/crondelivery/router_test.go @@ -361,3 +361,71 @@ func TestImplementsSchedulingRunner(t *testing.T) { _ scheduling.Runner = (*Router)(nil) ) } + +// TestRouterAdvancesTheScheduleAfterASuccessfulRun is the acceptance criterion +// for a recurring job's timing: cronstore.Store.MarkRun is the only step that +// moves a job's NextRun, and the Router is the single Runner the engine hands +// every job. With no caller the store keeps reporting the job due on every +// tick, so a job scheduled every hour fires once per tick instead. +func TestRouterAdvancesTheScheduleAfterASuccessfulRun(t *testing.T) { + s := newStore(t) + job := createJob(t, s, chatSpec("advance", "chat-1", "hello")) + + before, ok, err := s.Get(t.Context(), job.ID) + if err != nil || !ok { + t.Fatalf("Get before = (%+v, %v, %v)", before, ok, err) + } + if !before.NextRun.Before(time.Now()) { + t.Fatalf("fixture is not due: NextRun = %v", before.NextRun) + } + + r, err := NewRouter(s, map[string]scheduling.Runner{ + cronstore.KindChat: &countingRunner{}, + }, &recordingSink{}) + if err != nil { + t.Fatalf("NewRouter: %v", err) + } + if err := r.Run(t.Context(), job); err != nil { + t.Fatalf("Run: %v", err) + } + + after, ok, err := s.Get(t.Context(), job.ID) + if err != nil || !ok { + t.Fatalf("Get after = (%+v, %v, %v)", after, ok, err) + } + if after.LastRun == nil { + t.Error("LastRun is nil after a successful run: the run was never recorded") + } + if !after.NextRun.After(time.Now()) { + t.Fatalf("NextRun = %v (was %v): a completed interval job is still due, so it re-fires on every tick", + after.NextRun, before.NextRun) + } +} + +// TestRouterToleratesAOnceScheduleWithoutANextRun pins the other half: a +// one-shot job has no recurring next run by definition, so MarkRun reports +// cronstore.ErrScheduleUnsupported. That is not a run failure -- the job ran -- +// and returning it would make the engine emit KindJobError for a job that +// succeeded. +func TestRouterToleratesAOnceScheduleWithoutANextRun(t *testing.T) { + s := newStore(t) + at := time.Now().Add(-time.Hour) + job := createJob(t, s, cronstore.JobSpec{ + ID: "one-shot", + Detail: "one-shot job", + Kind: cronstore.KindChat, + Schedule: cronstore.Schedule{Kind: cronstore.ScheduleOnce, At: &at}, + Target: cronstore.Target{ChatID: "chat-1"}, + Payload: cronstore.Payload{Text: "once"}, + }) + + r, err := NewRouter(s, map[string]scheduling.Runner{ + cronstore.KindChat: &countingRunner{}, + }, &recordingSink{}) + if err != nil { + t.Fatalf("NewRouter: %v", err) + } + if err := r.Run(t.Context(), job); err != nil { + t.Fatalf("Run on a once schedule = %v, want nil (no recurring next run is not a failure)", err) + } +}