-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathworkers.go
More file actions
189 lines (159 loc) · 6.31 KB
/
Copy pathworkers.go
File metadata and controls
189 lines (159 loc) · 6.31 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
package aggregatedpool
import (
"fmt"
"strings"
"github.com/google/uuid"
"github.com/roadrunner-server/errors"
"github.com/temporalio/roadrunner-temporal/v6/api"
"github.com/temporalio/roadrunner-temporal/v6/internal"
tActivity "go.temporal.io/sdk/activity"
temporalClient "go.temporal.io/sdk/client"
"go.temporal.io/sdk/converter"
sdkinterceptor "go.temporal.io/sdk/interceptor"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
"go.uber.org/zap"
)
const tq = "taskqueue"
// ResolveInterceptors returns the list of WorkerInterceptors to apply.
// The built-in header context-bridging interceptor is always first.
// When enabledOrder is non-empty, only those named interceptors are used (in the specified order);
// an error is returned if any name is not found in the map.
// When enabledOrder is empty, all collected interceptors are applied.
func ResolveInterceptors(
interceptors map[string]api.Interceptor,
enabledOrder []string,
) ([]sdkinterceptor.WorkerInterceptor, error) {
// +1 for the built-in interceptor at position 0
result := make([]sdkinterceptor.WorkerInterceptor, 1, max(len(enabledOrder), len(interceptors))+1)
result[0] = NewWorkerInterceptor()
if len(enabledOrder) > 0 {
for _, name := range enabledOrder {
intcpt, ok := interceptors[name]
if !ok {
return nil, errors.E(
errors.Op("temporal_resolve_interceptors"),
errors.Errorf("interceptor %q is not registered", name),
)
}
result = append(result, intcpt.WorkerInterceptor())
}
} else {
for _, intcpt := range interceptors {
result = append(result, intcpt.WorkerInterceptor())
}
}
return result, nil
}
// ResolveDataConverters returns the list of custom PayloadConverters to apply.
// When both inputs are empty, nil, nil is returned (no custom converters needed).
// When enabledOrder is non-empty, only those converters are used (in the specified order);
// an error is returned if any encoding is not found in the map.
// When enabledOrder is empty, all collected converters are applied.
func ResolveDataConverters(
converters map[string]converter.PayloadConverter,
enabledOrder []string,
) ([]converter.PayloadConverter, error) {
if len(converters) == 0 && len(enabledOrder) == 0 {
return nil, nil
}
if len(enabledOrder) > 0 {
result := make([]converter.PayloadConverter, 0, len(enabledOrder))
for _, encoding := range enabledOrder {
dc, ok := converters[encoding]
if !ok {
return nil, errors.E(
errors.Op("temporal_resolve_data_converters"),
errors.Errorf("data converter with encoding %q is not registered", encoding),
)
}
result = append(result, dc)
}
return result, nil
}
result := make([]converter.PayloadConverter, 0, len(converters))
for _, dc := range converters {
result = append(result, dc)
}
return result, nil
}
// registerWorkflow runs the SDK workflow registration and converts any panic it raises
// into a normal error. The Temporal SDK panics (rather than returning an error) on
// invalid registration config; because RR's config comes from the PHP worker at
// runtime, that must fail worker init cleanly instead of crashing the process. No
// SDK-internal condition is mirrored, so nothing here needs to track SDK changes.
func registerWorkflow(register func(), name, taskQueue string) (err error) {
defer func() {
r := recover()
if r == nil {
return
}
op := errors.Op("temporal_register_workflow")
// Best-effort friendly hint for the common case (missing versioning behavior).
// Purely cosmetic: if the SDK reworks this message we still return the generic
// error below, so correctness never depends on the matched string.
if msg, ok := r.(string); ok && strings.Contains(msg, "versioning behavior") {
err = errors.E(op, errors.Errorf("workflow %q on task queue %q has no versioning behavior set while worker versioning is enabled; set a VersioningBehavior on the workflow or a DefaultVersioningBehavior on the worker", name, taskQueue))
return
}
err = errors.E(op, errors.Errorf("failed to register workflow %q on task queue %q: %v", name, taskQueue, r))
}()
register()
return nil
}
func TemporalWorkers(wDef *Workflow, actDef *Activity, wi []*internal.WorkerInfo, log *zap.Logger, tc temporalClient.Client, interceptors map[string]api.Interceptor, configuredInterceptors []string) ([]worker.Worker, error) {
resolved, err := ResolveInterceptors(interceptors, configuredInterceptors)
if err != nil {
return nil, err
}
workers := make([]worker.Worker, 0, len(wi))
for i := range wi {
log.Debug("worker info", zap.Any("worker_info", wi[i]))
// Override to 0: RoadRunner manages worker lifecycle independently
wi[i].Options.WorkerStopTimeout = 0
if wi[i].TaskQueue == "" {
wi[i].TaskQueue = temporalClient.DefaultNamespace
}
if wi[i].Options.Identity == "" {
wi[i].Options.Identity = fmt.Sprintf(
"roadrunner:%s:%s",
wi[i].TaskQueue,
uuid.NewString(),
)
}
wi[i].Options.Interceptors = append(wi[i].Options.Interceptors, resolved...)
wrk := worker.New(tc, wi[i].TaskQueue, wi[i].Options)
for j := 0; j < len(wi[i].Workflows); j++ {
wf := wi[i].Workflows[j]
err := registerWorkflow(func() {
wrk.RegisterWorkflowWithOptions(wDef, workflow.RegisterOptions{
Name: wf.Name,
VersioningBehavior: wf.VersioningBehavior,
DisableAlreadyRegisteredCheck: false,
})
}, wf.Name, wi[i].TaskQueue)
if err != nil {
return nil, err
}
log.Debug("workflow registered", zap.String(tq, wi[i].TaskQueue), zap.Any("workflow name", wf.Name), zap.Int("versioning_behavior", int(wf.VersioningBehavior)))
}
if actDef.disableActivityWorkers {
log.Debug("activity workers disabled", zap.String(tq, wi[i].TaskQueue))
// add worker to the pool without activities
workers = append(workers, wrk)
continue
}
for j := 0; j < len(wi[i].Activities); j++ {
wrk.RegisterActivityWithOptions(actDef.execute, tActivity.RegisterOptions{
Name: wi[i].Activities[j].Name,
DisableAlreadyRegisteredCheck: false,
SkipInvalidStructFunctions: false,
})
log.Debug("activity registered", zap.String(tq, wi[i].TaskQueue), zap.Any("workflow name", wi[i].Activities[j].Name))
}
// add worker to the pool
workers = append(workers, wrk)
}
log.Debug("workers initialized", zap.Int("num_workers", len(workers)))
return workers, nil
}