-
Notifications
You must be signed in to change notification settings - Fork 43
/
workflow.go
172 lines (143 loc) · 5.42 KB
/
workflow.go
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
package worker
import (
"context"
"errors"
"fmt"
"log/slog"
"time"
"github.com/benbjohnson/clock"
"github.com/cschleiden/go-workflows/backend"
"github.com/cschleiden/go-workflows/backend/history"
"github.com/cschleiden/go-workflows/backend/metrics"
"github.com/cschleiden/go-workflows/core"
"github.com/cschleiden/go-workflows/internal/metrickeys"
im "github.com/cschleiden/go-workflows/internal/metrics"
"github.com/cschleiden/go-workflows/internal/workflow"
"github.com/cschleiden/go-workflows/internal/workflow/cache"
"github.com/cschleiden/go-workflows/registry"
)
type WorkflowWorkerOptions struct {
WorkerOptions
WorkflowExecutorCache workflow.ExecutorCache
WorkflowExecutorCacheSize int
WorkflowExecutorCacheTTL time.Duration
}
func NewWorkflowWorker(
b backend.Backend,
registry *registry.Registry,
options WorkflowWorkerOptions,
) *Worker[backend.WorkflowTask, workflow.ExecutionResult] {
if options.WorkflowExecutorCache == nil {
options.WorkflowExecutorCache = cache.NewWorkflowExecutorLRUCache(b.Metrics(), options.WorkflowExecutorCacheSize, options.WorkflowExecutorCacheTTL)
}
tw := &WorkflowTaskWorker{
backend: b,
registry: registry,
cache: options.WorkflowExecutorCache,
logger: b.Logger(),
}
return NewWorker[backend.WorkflowTask, workflow.ExecutionResult](b, tw, &options.WorkerOptions)
}
type WorkflowTaskWorker struct {
backend backend.Backend
registry *registry.Registry
cache workflow.ExecutorCache
logger *slog.Logger
}
// Complete implements TaskWorker.
func (wtw *WorkflowTaskWorker) Complete(ctx context.Context, result *workflow.ExecutionResult, t *backend.WorkflowTask) error {
state := result.State
if state == core.WorkflowInstanceStateFinished || state == core.WorkflowInstanceStateContinuedAsNew {
if t.WorkflowInstanceState != state {
// If the workflow is now finished, record
wtw.backend.Metrics().Counter(metrickeys.WorkflowInstanceFinished, metrics.Tags{
metrickeys.SubWorkflow: fmt.Sprint(t.WorkflowInstance.SubWorkflow()),
metrickeys.ContinuedAsNew: fmt.Sprint(state == core.WorkflowInstanceStateContinuedAsNew),
}, 1)
}
// Workflow is finished, explicitly evict from cache (if one is used)
if wtw.cache != nil {
if err := wtw.cache.Evict(ctx, t.WorkflowInstance); err != nil {
wtw.logger.ErrorContext(ctx, "could not evict workflow executor from cache", "error", err)
}
}
}
wtw.backend.Metrics().Counter(metrickeys.ActivityTaskScheduled, metrics.Tags{}, int64(len(result.ActivityEvents)))
if err := wtw.backend.CompleteWorkflowTask(
ctx, t, t.WorkflowInstance, state, result.Executed, result.ActivityEvents, result.TimerEvents, result.WorkflowEvents); err != nil {
wtw.logger.ErrorContext(ctx, "could not complete workflow task", "error", err)
return fmt.Errorf("completing workflow task: %w", err)
}
return nil
}
func (wtw *WorkflowTaskWorker) Execute(ctx context.Context, t *backend.WorkflowTask) (*workflow.ExecutionResult, error) {
// Record how long this task was in the queue
firstEvent := t.NewEvents[0]
var scheduledAt time.Time
if firstEvent.Type == history.EventType_TimerFired {
timerFiredAttributes := firstEvent.Attributes.(*history.TimerFiredAttributes)
scheduledAt = timerFiredAttributes.At // Use the timestamp of the timer fired event as the schedule time
} else {
scheduledAt = firstEvent.Timestamp // Use the timestamp of the first event as the schedule time
}
eventName := fmt.Sprint(firstEvent.Type)
timeInQueue := time.Since(scheduledAt)
wtw.backend.Metrics().Distribution(metrickeys.WorkflowTaskDelay, metrics.Tags{
metrickeys.EventName: eventName,
}, float64(timeInQueue/time.Millisecond))
timer := im.NewTimer(wtw.backend.Metrics(), metrickeys.WorkflowTaskProcessed, metrics.Tags{
metrickeys.EventName: eventName,
})
executor, err := wtw.getExecutor(ctx, t)
if err != nil {
return nil, fmt.Errorf("getting executor: %w", err)
}
result, err := executor.ExecuteTask(ctx, t)
if err != nil {
return nil, fmt.Errorf("executing task: %w", err)
}
// Only record the time spent in the workflow code
timer.Stop()
return result, nil
}
func (wtw *WorkflowTaskWorker) Extend(ctx context.Context, t *backend.WorkflowTask) error {
return wtw.backend.ExtendWorkflowTask(ctx, t.ID, t.WorkflowInstance)
}
func (wtw *WorkflowTaskWorker) Get(ctx context.Context) (*backend.WorkflowTask, error) {
t, err := wtw.backend.GetWorkflowTask(ctx)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return nil, nil
}
return nil, err
}
return t, nil
}
func (wtw *WorkflowTaskWorker) getExecutor(ctx context.Context, t *backend.WorkflowTask) (workflow.WorkflowExecutor, error) {
// Try to get a cached executor
executor, ok, err := wtw.cache.Get(ctx, t.WorkflowInstance)
if err != nil {
wtw.logger.ErrorContext(ctx, "could not get cached workflow task executor", "error", err)
}
if !ok {
executor, err = workflow.NewExecutor(
wtw.backend.Logger(),
wtw.backend.Tracer(),
wtw.registry,
wtw.backend.Converter(),
wtw.backend.ContextPropagators(),
wtw.backend,
t.WorkflowInstance,
t.Metadata,
clock.New(),
)
if err != nil {
return nil, fmt.Errorf("creating workflow task executor: %w", err)
}
}
// Cache executor instance for future continuation tasks, or refresh last access time
if err := wtw.cache.Store(ctx, t.WorkflowInstance, executor); err != nil {
wtw.logger.ErrorContext(ctx, "error while caching workflow task executor:", "error", err)
}
return executor, nil
}