-
Notifications
You must be signed in to change notification settings - Fork 9
/
internal.go
182 lines (149 loc) · 4.57 KB
/
internal.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
173
174
175
176
177
178
179
180
181
182
package rrtemporal
import (
"context"
"time"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/pool/pool"
"github.com/temporalio/roadrunner-temporal/v5/aggregatedpool"
"github.com/temporalio/roadrunner-temporal/v5/data_converter"
"github.com/temporalio/roadrunner-temporal/v5/internal/codec/proto"
"github.com/temporalio/roadrunner-temporal/v5/internal/logger"
tclient "go.temporal.io/sdk/client"
"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/worker"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
const (
APIKey string = "ApiKey"
)
func (p *Plugin) initPool() error {
var err error
ap, err := p.server.NewPool(context.Background(), p.config.Activities, map[string]string{RrMode: pluginName, RrCodec: RrCodecVal}, p.log)
if err != nil {
return err
}
dc := data_converter.NewDataConverter(converter.GetDefaultDataConverter())
codec := proto.NewCodec(p.log, dc)
// LA + A definitions
actDef := aggregatedpool.NewActivityDefinition(codec, ap, p.log)
laDef := aggregatedpool.NewLocalActivityFn(codec, ap, p.log)
// ------------------
// ---------- WORKFLOW POOL -------------
wp, err := p.server.NewPool(
context.Background(),
&pool.Config{
NumWorkers: 1,
Command: p.config.Activities.Command,
AllocateTimeout: time.Hour * 240,
// use the same timeout
DestroyTimeout: p.config.Activities.DestroyTimeout,
// no supervisor for the workflow worker
Supervisor: nil,
},
map[string]string{RrMode: pluginName, RrCodec: RrCodecVal},
nil,
)
if err != nil {
return err
}
wfDef := aggregatedpool.NewWorkflowDefinition(codec, laDef.ExecuteLA, wp, p.log)
// get worker information
wi, err := WorkerInfo(codec, wp, p.rrVersion)
if err != nil {
return err
}
if len(wi) == 0 {
return errors.Str("worker info should contain at least 1 worker")
}
err = p.initTemporalClient(wi[0].PhpSdkVersion, wi[0].Flags, dc)
if err != nil {
return err
}
workers, err := aggregatedpool.TemporalWorkers(wfDef, actDef, wi, p.log, p.temporal.client, p.temporal.interceptors)
if err != nil {
return err
}
for i := 0; i < len(workers); i++ {
err = workers[i].Start()
if err != nil {
return err
}
}
if len(wp.Workers()) < 1 {
return errors.E(errors.Str("failed to allocate a workflow worker"))
}
// set all fields
// we have only 1 worker for the workflow pool
p.wwPID = int(wp.Workers()[0].Pid())
p.temporal.rrWorkflowDef = wfDef
p.temporal.rrActivityDef = actDef
p.temporal.workers = workers
p.codec = codec
p.temporal.activities = ActivitiesInfo(wi)
p.temporal.workflows = WorkflowsInfo(wi)
p.actP = ap
p.wfP = wp
return nil
}
func (p *Plugin) getWfDef() *aggregatedpool.Workflow {
p.mu.RLock()
defer p.mu.RUnlock()
return p.temporal.rrWorkflowDef
}
func (p *Plugin) getActDef() *aggregatedpool.Activity {
p.mu.RLock()
defer p.mu.RUnlock()
return p.temporal.rrActivityDef
}
func (p *Plugin) initTemporalClient(phpSdkVersion string, flags map[string]string, dc converter.DataConverter) error {
if phpSdkVersion == "" {
phpSdkVersion = clientBaselineVersion
}
if val, ok := flags[APIKey]; ok {
if val != "" {
p.apiKey.Store(ptrTo(val))
}
}
p.log.Debug("PHP-SDK version: " + phpSdkVersion)
worker.SetStickyWorkflowCacheSize(p.config.CacheSize)
opts := tclient.Options{
HostPort: p.config.Address,
MetricsHandler: p.temporal.mh,
Namespace: p.config.Namespace,
Logger: logger.NewZapAdapter(p.log),
DataConverter: dc,
ConnectionOptions: tclient.ConnectionOptions{
TLS: p.temporal.tlsCfg,
DialOptions: []grpc.DialOption{
grpc.WithUnaryInterceptor(rewriteNameAndVersion(phpSdkVersion)),
},
},
Credentials: tclient.NewAPIKeyDynamicCredentials(func(context.Context) (string, error) {
if p.apiKey.Load() != nil {
return *p.apiKey.Load(), nil
}
return "", nil
}),
}
var err error
p.temporal.client, err = tclient.Dial(opts)
if err != nil {
return err
}
p.log.Info("connected to temporal server", zap.String("address", p.config.Address))
return nil
}
func rewriteNameAndVersion(phpSdkVersion string) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
md, ok := metadata.FromOutgoingContext(ctx)
if md == nil || !ok {
return invoker(ctx, method, req, reply, cc, opts...)
}
md.Set(clientNameHeaderName, clientNameHeaderValue)
md.Set(clientVersionHeaderName, phpSdkVersion)
ctx = metadata.NewOutgoingContext(ctx, md)
return invoker(ctx, method, req, reply, cc, opts...)
}
}