Repository navigation
Expand file tree
/
Copy pathstep_context.go
More file actions
133 lines (115 loc) · 4.24 KB
/
Copy pathstep_context.go
File metadata and controls
133 lines (115 loc) · 4.24 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
package gush
import (
"context"
"go.temporal.io/sdk/activity"
)
// abortedStepErrorType is the ApplicationError type of a step's Abort.
const abortedStepErrorType = "StepAborted"
type contextKey string
// stepContextKey finds the StepContext a plain context.Context was derived from.
const stepContextKey contextKey = "gush.step_context"
// StepContext is what a step receives. It is a context.Context, so pass it to anything
// that takes one, and the step's cancellation and deadline follow.
//
// func ProcessOrder(ctx *gush.StepContext) (any, error) {
// var order Order
// err := ctx.Decode(&order)
// if err != nil {
// return nil, err
// }
// return processOrder(ctx, order)
// }
type StepContext struct {
context.Context
StepInfo
*Logger
// Input is the raw input; Decode deserializes it.
Input []byte
// LogAttributes are inherited from the parent flow.
LogAttributes map[string]any
}
// NewStepContext creates a StepContext from any context. Outside a running step, such as
// in a test, its step information is empty.
func NewStepContext(ctx context.Context, logAttributes map[string]any) *StepContext {
stepCtx := &StepContext{Logger: NewLogger(), LogAttributes: logAttributes}
stepCtx.Context = context.WithValue(ctx, stepContextKey, stepCtx)
if activity.IsActivity(ctx) {
info := activity.GetInfo(ctx)
stepCtx.ID = info.ActivityID
stepCtx.FlowID = info.WorkflowExecution.ID
stepCtx.Queue = info.TaskQueue
stepCtx.Priority = NewPriority(info.Priority.PriorityKey)
stepCtx.FairKey = info.Priority.FairnessKey
stepCtx.ResumeToken = encodeResumeToken(info.TaskToken)
stepCtx.Attempt = info.Attempt
}
for key, value := range logAttributes {
stepCtx.Logger.SetAttributes(Attr(key, value))
}
if stepCtx.FlowID != "" {
stepCtx.Logger.SetAttributes(Attr("flow_id", stepCtx.FlowID))
}
return stepCtx
}
// AsStepContext returns ctx as a StepContext, so helpers that take a plain context.Context
// can log with the step's context. A context derived from a StepContext, such as with
// context.WithTimeout, keeps ctx's cancellation and the step's logger and attributes.
// Any other context is wrapped.
func AsStepContext(ctx context.Context) *StepContext {
stepCtx, ok := ctx.(*StepContext)
if ok {
return stepCtx
}
origin, ok := ctx.Value(stepContextKey).(*StepContext)
if ok {
derived := *origin
derived.Context = ctx
return &derived
}
return NewStepContext(ctx, nil)
}
// Suspend ends the attempt without finishing the step, freeing the worker while something
// outside the flow, such as a person or a webhook, decides the result and calls Resume with
// ctx.ResumeToken.
//
// The attempt timeout keeps running while the step waits. If it expires first, the step
// retries and runs again, which by default happens after 2 minutes. So set
// WithAttemptTimeout to the longest acceptable wait, and WithMaxAttempts(1) unless running
// the step's code twice is safe.
//
// func ApproveExpense(ctx *gush.StepContext) (any, error) {
// err := sendApprovalRequest(ctx, ctx.ResumeToken, ctx.Input)
// if err != nil {
// return nil, err
// }
// return ctx.Suspend()
// }
//
// err := ctx.RunStep(ApproveExpense, expense, &decision,
// gush.WithAttemptTimeout(24*time.Hour),
// gush.WithMaxAttempts(1),
// )
func (stepCtx *StepContext) Suspend() (any, error) {
return nil, activity.ErrResultPending
}
// Heartbeat reports that the step is alive, which WithHeartbeatTimeout requires. It is also
// how a running step learns of cancellation: ctx is canceled only after a heartbeat. Details are
// recorded for the next attempt to read with the SDK's activity.GetHeartbeatDetails. Outside
// a running step it does nothing.
func (stepCtx *StepContext) Heartbeat(details ...any) {
if activity.IsActivity(stepCtx.Context) {
activity.RecordHeartbeat(stepCtx.Context, details...)
}
}
// Decode deserializes the step's Input into target, aborting the step if it cannot.
func (stepCtx *StepContext) Decode(target any) error {
err := Decode(stepCtx.Input, target)
if err != nil {
return stepCtx.Abort(err)
}
return nil
}
// Abort returns an error that fails the step permanently, with no further retries.
func (stepCtx *StepContext) Abort(cause ...error) error {
return abortError(abortedStepErrorType, cause)
}