-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep.go
More file actions
459 lines (411 loc) · 13.4 KB
/
Copy pathstep.go
File metadata and controls
459 lines (411 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package durable
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log/slog"
"runtime/debug"
"strings"
"sync/atomic"
"time"
)
// StepStatus is the lifecycle state of a single step checkpoint.
type StepStatus string
const (
// StepStatusWaiting means the step is suspended and awaiting CompleteStep.
StepStatusWaiting StepStatus = "waiting"
// StepStatusCompleted means the step succeeded and its result is cached.
StepStatusCompleted StepStatus = "completed"
// StepStatusFailed means the step returned an error or panicked.
StepStatusFailed StepStatus = "failed"
)
// StepRecord is the persistent checkpoint for one memoised step.
type StepRecord struct {
StepID string
Seq int
InputHash string // unused in v1; RunStep has no input parameter
Result []byte
Error string
PanicTrace string
Status StepStatus
StartedAt time.Time
CompletedAt time.Time
}
type stepConfig struct {
timeout *time.Duration
maxRetries *int
}
// StepOption configures a single RunStep call.
type StepOption func(*stepConfig)
// WithStepTimeout is an inner bound: the step deadline is
// min(task deadline, step timeout). It cannot extend past the task deadline.
func WithStepTimeout(d time.Duration) StepOption {
return func(c *stepConfig) { c.timeout = &d }
}
// WithStepMaxRetries sets how many times this step's function is re-invoked
// on a non-panic, non-ErrStepPending error. Default is 0 (one attempt).
// Does not inherit from task or engine retry settings.
func WithStepMaxRetries(n int) StepOption {
return func(c *stepConfig) { c.maxRetries = &n }
}
// StepRun is the handle returned by RunStep. Get returns immediately —
// RunStep itself is synchronous (including the wait on ErrStepPending).
type StepRun[O any] struct {
stepID string
result O
err error
}
// StepID returns the stepID this handle corresponds to.
func (r *StepRun[O]) StepID() string { return r.stepID }
// Get returns the step result. ctx is reserved for future async support;
// the result is already available because RunStep waits before returning.
func (r *StepRun[O]) Get(ctx context.Context) (O, error) {
// ctx is reserved for future async step execution. RunStep is
// synchronous today, so the result is already available.
_ = ctx
return r.result, r.err
}
// StepRunner is scoped to a single run and provides RunStep, StepToken, and
// run-context accessors. It is not safe for concurrent use.
type StepRunner struct {
taskID string
runID string
cache map[string]StepRecord
seenSteps map[string]struct{}
seq int
currentStepID string
inUse atomic.Bool
logger *slog.Logger
engine *Engine
handle *runHandle
}
// TaskID returns the taskID of the current run.
func (s *StepRunner) TaskID() string { return s.taskID }
// RunID returns the runID of the current run.
func (s *StepRunner) RunID() string { return s.runID }
// StepSeq returns the number of steps executed so far. Zero before the first
// RunStep call; incremented after each RunStep returns (including cache hits).
func (s *StepRunner) StepSeq() int { return s.seq }
// Logger returns the engine logger pre-scoped with taskID and runID.
func (s *StepRunner) Logger() *slog.Logger { return s.logger }
// StepToken returns an opaque token encoding taskID, runID, and the current
// stepID. Must be called inside a RunStep function — panics if currentStepID
// is empty. Pass the token to an external caller (DB, email, webhook URL)
// so they can later call CompleteStep.
func (s *StepRunner) StepToken() string {
if s.currentStepID == "" {
panic("durable: StepToken must be called inside a RunStep function")
}
return encodeStepToken(s.taskID, s.runID, s.currentStepID)
}
// stepFailedError marks an error that originated from a step so the task
// retry loop does not re-invoke the whole closure — the step already spent
// its own retry budget.
type stepFailedError struct {
stepID string
err error
}
func (e *stepFailedError) Error() string { return e.err.Error() }
func (e *stepFailedError) Unwrap() error { return e.err }
// RunStep executes fn as a memoised checkpoint. On a completed cache hit,
// fn is not called. On a miss, fn runs synchronously and the result is
// persisted. stepID must be unique within the run — a duplicate panics.
// Concurrent calls on the same StepRunner panic.
func RunStep[O any](ctx context.Context, s *StepRunner, stepID string, fn func(ctx context.Context) (O, error), opts ...StepOption) *StepRun[O] {
failed := func(err error) *StepRun[O] {
return &StepRun[O]{stepID: stepID, err: err}
}
if stepID == "" {
panic("durable: step ID must not be empty")
}
if !s.inUse.CompareAndSwap(false, true) {
panic("durable: concurrent RunStep calls on the same StepRunner; collect results then call RunStep sequentially")
}
defer s.inUse.Store(false)
if _, dup := s.seenSteps[stepID]; dup {
panic(fmt.Sprintf("durable: duplicate step ID %q in run %s/%s", stepID, s.taskID, s.runID))
}
s.seenSteps[stepID] = struct{}{}
cfg := stepConfig{}
for _, o := range opts {
o(&cfg)
}
if rec, ok := s.cache[stepID]; ok && rec.Status == StepStatusCompleted {
var out O
if err := json.Unmarshal(rec.Result, &out); err != nil {
s.finishStep()
return failed(fmt.Errorf("durable: replay step %q: unmarshal: %w", stepID, err))
}
s.logger.Debug("step replayed from cache", "step_id", stepID, "seq", s.seq+1)
s.finishStep()
return &StepRun[O]{stepID: stepID, result: out}
}
if rec, ok := s.cache[stepID]; ok && rec.Status == StepStatusWaiting {
s.currentStepID = stepID
out, err := waitForSignal[O](ctx, s, stepID, rec.StartedAt)
s.currentStepID = ""
s.finishStep()
if err != nil {
return failed(err)
}
return &StepRun[O]{stepID: stepID, result: out}
}
s.currentStepID = stepID
defer func() { s.currentStepID = "" }()
stepCtx := ctx
var cancel context.CancelFunc
if cfg.timeout != nil && *cfg.timeout > 0 {
stepCtx, cancel = context.WithTimeout(ctx, *cfg.timeout)
defer cancel()
}
maxRetries := 0
if cfg.maxRetries != nil && *cfg.maxRetries > 0 {
maxRetries = *cfg.maxRetries
}
startedAt := time.Now().UTC()
var (
out O
fnErr error
panicTrace string
)
for attempt := 0; attempt <= maxRetries; attempt++ {
out, panicTrace, fnErr = invokeStep(stepCtx, stepID, fn)
if panicTrace != "" {
s.persistFailed(stepID, startedAt, fnErr, panicTrace)
s.finishStep()
return failed(&stepFailedError{stepID: stepID, err: fnErr})
}
if errors.Is(fnErr, ErrStepPending) {
out, fnErr = enterPending[O](stepCtx, s, stepID, startedAt)
s.finishStep()
if fnErr != nil {
return failed(fnErr)
}
return &StepRun[O]{stepID: stepID, result: out}
}
if fnErr != nil {
if stepCtx.Err() != nil || attempt == maxRetries {
s.persistFailed(stepID, startedAt, fnErr, "")
s.finishStep()
return failed(&stepFailedError{stepID: stepID, err: fnErr})
}
s.logger.Warn("step retrying", "step_id", stepID, "attempt", attempt+1, "error", fnErr)
continue
}
break
}
raw, err := json.Marshal(out)
if err != nil {
s.finishStep()
return failed(fmt.Errorf("durable: step %q: marshal result: %w", stepID, err))
}
completedAt := time.Now().UTC()
rec := StepRecord{
StepID: stepID,
Seq: s.seq + 1,
Status: StepStatusCompleted,
Result: raw,
StartedAt: startedAt,
CompletedAt: completedAt,
}
if err := s.engine.appendStep(s.taskID, s.runID, rec); err != nil {
s.finishStep()
return failed(err)
}
s.cache[stepID] = rec
s.logger.Debug("step completed", "step_id", stepID, "seq", rec.Seq, "duration", completedAt.Sub(startedAt))
s.finishStep()
return &StepRun[O]{stepID: stepID, result: out}
}
func (s *StepRunner) finishStep() {
s.seq++
}
func invokeStep[O any](ctx context.Context, stepID string, fn func(context.Context) (O, error)) (out O, panicTrace string, fnErr error) {
func() {
defer func() {
if r := recover(); r != nil {
panicTrace = fmt.Sprintf("%v\n%s", r, debug.Stack())
fnErr = fmt.Errorf("durable: step %q panicked: %v", stepID, r)
}
}()
out, fnErr = fn(ctx)
}()
return out, panicTrace, fnErr
}
func (s *StepRunner) persistFailed(stepID string, startedAt time.Time, fnErr error, panicTrace string) {
rec := StepRecord{
StepID: stepID,
Seq: s.seq + 1,
Status: StepStatusFailed,
Error: fnErr.Error(),
PanicTrace: panicTrace,
StartedAt: startedAt,
CompletedAt: time.Now().UTC(),
}
if err := s.engine.appendStep(s.taskID, s.runID, rec); err != nil {
s.logger.Error("save failed step failed", "step_id", stepID, "error", err)
}
if panicTrace != "" {
s.logger.Error("step panicked", "step_id", stepID, "panic_trace", panicTrace)
} else {
s.logger.Error("step failed", "step_id", stepID, "error", fnErr)
}
}
func enterPending[O any](ctx context.Context, s *StepRunner, stepID string, startedAt time.Time) (O, error) {
var zero O
waiting := StepRecord{
StepID: stepID,
Seq: s.seq + 1,
Status: StepStatusWaiting,
StartedAt: startedAt,
}
if err := s.engine.appendStep(s.taskID, s.runID, waiting); err != nil {
return zero, err
}
s.cache[stepID] = waiting
if err := s.setRunStatus(StatusWaiting); err != nil {
return zero, err
}
s.logger.Info("step waiting", "step_id", stepID)
return waitForSignal[O](ctx, s, stepID, startedAt)
}
func waitForSignal[O any](ctx context.Context, s *StepRunner, stepID string, startedAt time.Time) (O, error) {
var zero O
key := s.taskID + "/" + s.runID + "/" + stepID
ch := make(chan []byte, 1)
s.engine.signals.Store(key, ch)
defer s.engine.signals.Delete(key)
// CompleteStep may have written the SignalEntry between our WAITING
// append and channel registration. Re-read the journal before blocking
// so that payload is not lost.
_, signals, err := s.engine.loadJournal(s.taskID, s.runID)
if err != nil {
return zero, err
}
payload, ok := signals[stepID]
if !ok {
select {
case payload = <-ch:
case <-ctx.Done():
return zero, ctx.Err()
case <-s.engine.stopCh:
return zero, fmt.Errorf("durable: engine closed")
}
}
var out O
if err := json.Unmarshal(payload, &out); err != nil {
return zero, fmt.Errorf("durable: unmarshal signal for step %q: %w", stepID, err)
}
completedAt := time.Now().UTC()
rec := StepRecord{
StepID: stepID,
Seq: s.seq + 1,
Status: StepStatusCompleted,
Result: payload,
StartedAt: startedAt,
CompletedAt: completedAt,
}
if err := s.engine.appendStep(s.taskID, s.runID, rec); err != nil {
return zero, err
}
s.cache[stepID] = rec
if err := s.setRunStatus(StatusRunning); err != nil {
return zero, err
}
s.logger.Info("step resumed", "step_id", stepID)
return out, nil
}
func (s *StepRunner) setRunStatus(status TaskStatus) error {
info, ok, err := s.engine.loadMeta(s.taskID, s.runID)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("durable: meta missing for %s/%s", s.taskID, s.runID)
}
info.Status = status
info.UpdatedAt = time.Now().UTC()
if err := s.engine.saveMeta(s.taskID, s.runID, info); err != nil {
return err
}
if s.handle != nil {
s.handle.setStatus(status)
}
return nil
}
// encodeStepToken uses base64.RawURLEncoding of "taskID:runID:stepID".
// runID is a ULID (no colons); taskID is rejected if it contains ':'.
// stepID may contain colons because decode uses SplitN(..., 3).
func encodeStepToken(taskID, runID, stepID string) string {
raw := taskID + ":" + runID + ":" + stepID
return base64.RawURLEncoding.EncodeToString([]byte(raw))
}
func decodeStepToken(token string) (taskID, runID, stepID string, err error) {
b, err := base64.RawURLEncoding.DecodeString(token)
if err != nil {
return "", "", "", ErrInvalidToken
}
parts := strings.SplitN(string(b), ":", 3)
if len(parts) != 3 || parts[0] == "" || parts[1] == "" || parts[2] == "" {
return "", "", "", ErrInvalidToken
}
return parts[0], parts[1], parts[2], nil
}
// CompleteStep delivers result to a suspended step identified by token.
// The payload is appended as a SignalEntry (durable) before the in-process
// waiter is signalled, so a crash after this call still resumes on the next
// RunTask. A second call with the same token is a no-op once the SignalEntry
// is on disk. Returns ErrRunAlreadyFinished if the step or run is already
// terminal, and ErrInvalidToken if the token cannot be decoded.
func CompleteStep[O any](ctx context.Context, e *Engine, token string, result O) error {
if err := ctx.Err(); err != nil {
return err
}
taskID, runID, stepID, err := decodeStepToken(token)
if err != nil {
return err
}
info, ok, err := e.loadMeta(taskID, runID)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("durable: run %s/%s not found", taskID, runID)
}
if info.Status == StatusCompleted || info.Status == StatusFailed {
return ErrRunAlreadyFinished
}
cache, signals, err := e.loadJournal(taskID, runID)
if err != nil {
return err
}
if rec, exists := cache[stepID]; exists && rec.Status == StepStatusCompleted {
return ErrRunAlreadyFinished
}
if _, exists := signals[stepID]; exists {
return nil
}
raw, err := json.Marshal(result)
if err != nil {
return err
}
if err := e.appendSignal(taskID, runID, stepID, raw); err != nil {
return err
}
info.Status = StatusRunning
info.UpdatedAt = time.Now().UTC()
if err := e.saveMeta(taskID, runID, info); err != nil {
return err
}
key := taskID + "/" + runID + "/" + stepID
if v, ok := e.signals.Load(key); ok {
ch := v.(chan []byte)
select {
case ch <- raw:
default:
}
}
return nil
}