From 8b64ab0afe047749e8adaaf352cfd1dc9cf96243 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 15 Nov 2026 17:17:09 +1100 Subject: [PATCH] refactor(runs): Step, Sleep and Wait are generic methods on the run context (#1977) --- internal/platform/pg/pg.go | 2 +- modules/runs/context.go | 16 ++++++++-------- modules/runs/runs_test.go | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/platform/pg/pg.go b/internal/platform/pg/pg.go index d42af38bc..3b841e71f 100644 --- a/internal/platform/pg/pg.go +++ b/internal/platform/pg/pg.go @@ -61,7 +61,7 @@ func Migrate(ctx context.Context, log *slog.Logger, ownerURL, runtimeURL string, return fmt.Errorf("apply migrations: %w", err) } for _, r := range results { - log.InfoContext(ctx, "migration applied", "version", r.Source.Version, "file", r.Source.Path, "took", r.Duration) + log.InfoContext(ctx, "migration applied", "migration", r.Source.Version, "file", r.Source.Path, "took", r.Duration) } return grantRuntime(ctx, db, runtimeURL, schemas) } diff --git a/modules/runs/context.go b/modules/runs/context.go index 24b046fd7..a26dd0ac7 100644 --- a/modules/runs/context.go +++ b/modules/runs/context.go @@ -58,7 +58,7 @@ func (rc *Context) OrgID() string { return rc.run.OrgID } // Step runs fn once for this position in the workflow and saves its result. // On replay it returns the saved result without calling fn. fn receives an // idempotency key unique to this run and step, for connectors to honour. -func Step[T any](rc *Context, name string, fn func(ctx context.Context, key string) (T, error)) (T, error) { +func (rc *Context) Step[T any](name string, fn func(ctx context.Context, key string) (T, error)) (T, error) { var zero T seq := rc.advance() if saved, ok := rc.saved[seq]; ok { @@ -72,7 +72,7 @@ func Step[T any](rc *Context, name string, fn func(ctx context.Context, key stri if errors.Is(recErr, store.ErrStepExists) { // Another holder recorded this step first; its result is the one that // counts. - return reload[T](rc, seq, name) + return rc.reload[T](seq, name) } if recErr != nil { return zero, recErr @@ -85,8 +85,8 @@ func Step[T any](rc *Context, name string, fn func(ctx context.Context, key stri // Sleep suspends the run durably until d has passed since the first time this // step ran. -func Sleep(rc *Context, d time.Duration) error { - wake, err := Step(rc, "sleep", func(context.Context, string) (time.Time, error) { +func (rc *Context) Sleep(d time.Duration) error { + wake, err := rc.Step("sleep", func(context.Context, string) (time.Time, error) { return time.Now().Add(d).UTC(), nil }) if err != nil { @@ -100,14 +100,14 @@ func Sleep(rc *Context, d time.Duration) error { // Wait returns the payload signalled to this run under key, suspending the run // durably until it arrives or timeout passes from the first time it waited. -func Wait(rc *Context, key string, timeout time.Duration) (json.RawMessage, error) { - deadline, err := Step(rc, "wait-start:"+key, func(context.Context, string) (time.Time, error) { +func (rc *Context) Wait(key string, timeout time.Duration) (json.RawMessage, error) { + deadline, err := rc.Step("wait-start:"+key, func(context.Context, string) (time.Time, error) { return time.Now().Add(timeout).UTC(), nil }) if err != nil { return nil, err } - return Step(rc, "wait:"+key, func(ctx context.Context, _ string) (json.RawMessage, error) { + return rc.Step("wait:"+key, func(ctx context.Context, _ string) (json.RawMessage, error) { payload, ok, err := rc.work.Signalled(ctx, rc.run.ID, key) switch { case err != nil: @@ -166,7 +166,7 @@ func (rc *Context) record(seq int, name string, out any, stepErr error) (store.S } // reload reads the step another holder recorded at seq and returns its result. -func reload[T any](rc *Context, seq int, name string) (T, error) { +func (rc *Context) reload[T any](seq int, name string) (T, error) { var zero T steps, err := rc.work.Steps(rc.ctx, rc.run.ID) if err != nil { diff --git a/modules/runs/runs_test.go b/modules/runs/runs_test.go index 846c0f76d..70fdc9663 100644 --- a/modules/runs/runs_test.go +++ b/modules/runs/runs_test.go @@ -123,13 +123,13 @@ func TestLeaseFencesStepWrites(t *testing.T) { engine := newEngine(nil, tt.work, slog.New(slog.DiscardHandler), Options{}) effects := 0 engine.Register("probe", func(rc *Context, _ json.RawMessage) (json.RawMessage, error) { - out, err := Step(rc, "probe", func(context.Context, string) (string, error) { + out, err := rc.Step("probe", func(context.Context, string) (string, error) { return "mine", nil }) if err != nil { return nil, err } - if _, err := Step(rc, "effect", func(context.Context, string) (bool, error) { + if _, err := rc.Step("effect", func(context.Context, string) (bool, error) { effects++ return true, nil }); err != nil {