refactor(runs): Step, Sleep and Wait are generic methods on the run context (#1977)

This commit is contained in:
sam committed 2026-11-15 17:17:09 +11:00
1 parent e8366683a9
commit 8b64ab0afe
3 files changed
+11 -11

No files matched your search

+1 -1
View File
@@ -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)
}
+8 -8
View File
@@ -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 {
+2 -2
View File
@@ -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 {