@@ -33,14 +33,13 @@ var info = capabilities.MustNewCapabilityInfo(
33
33
)
34
34
35
35
type capability struct {
36
- services.StateMachine
36
+ services.Service
37
+ eng * services.Engine
38
+
37
39
capabilities.CapabilityInfo
38
40
capabilities.Validator [config , inputs , requests.Response ]
39
41
40
42
reqHandler * requests.Handler
41
- stopCh services.StopChan
42
- wg sync.WaitGroup
43
- lggr logger.Logger
44
43
45
44
requestTimeout time.Duration
46
45
requestTimeoutLock sync.RWMutex
@@ -67,11 +66,8 @@ func NewCapability(s *requests.Store, clock clockwork.Clock, requestTimeout time
67
66
o := & capability {
68
67
CapabilityInfo : info ,
69
68
Validator : capabilities.NewValidator [config , inputs , requests.Response ](capabilities.ValidatorArgs {Info : info }),
70
- reqHandler : requests .NewHandler (lggr , s , clock , requestTimeout ),
71
69
clock : clock ,
72
70
requestTimeout : requestTimeout ,
73
- stopCh : make (chan struct {}),
74
- lggr : logger .Named (lggr , "OCR3CapabilityClient" ),
75
71
aggregatorFactory : aggregatorFactory ,
76
72
aggregators : map [string ]types.Aggregator {},
77
73
encoderFactory : encoderFactory ,
@@ -80,39 +76,16 @@ func NewCapability(s *requests.Store, clock clockwork.Clock, requestTimeout time
80
76
callbackChannelBufferSize : callbackChannelBufferSize ,
81
77
registeredWorkflowsIDs : map [string ]bool {},
82
78
}
79
+ o .Service , o .eng = services.Config {
80
+ Name : "OCR3CapabilityClient" ,
81
+ NewSubServices : func (l logger.Logger ) []services.Service {
82
+ o .reqHandler = requests .NewHandler (lggr , s , clock , requestTimeout )
83
+ return []services.Service {o .reqHandler }
84
+ },
85
+ }.NewServiceEngine (lggr )
83
86
return o
84
87
}
85
88
86
- func (o * capability ) Start (ctx context.Context ) error {
87
- return o .StartOnce ("OCR3Capability" , func () error {
88
- err := o .reqHandler .Start (ctx )
89
- if err != nil {
90
- return fmt .Errorf ("failed to start request handler: %w" , err )
91
- }
92
-
93
- return nil
94
- })
95
- }
96
-
97
- func (o * capability ) Close () error {
98
- return o .StopOnce ("OCR3Capability" , func () error {
99
- close (o .stopCh )
100
- o .wg .Wait ()
101
- err := o .reqHandler .Close ()
102
- if err != nil {
103
- return fmt .Errorf ("failed to close request handler: %w" , err )
104
- }
105
-
106
- return nil
107
- })
108
- }
109
-
110
- func (o * capability ) Name () string { return o .lggr .Name () }
111
-
112
- func (o * capability ) HealthReport () map [string ]error {
113
- return map [string ]error {o .Name (): o .Healthy ()}
114
- }
115
-
116
89
func (o * capability ) RegisterToWorkflow (ctx context.Context , request capabilities.RegisterToWorkflowRequest ) error {
117
90
c , err := o .ValidateConfig (request .Config )
118
91
if err != nil {
@@ -121,13 +94,13 @@ func (o *capability) RegisterToWorkflow(ctx context.Context, request capabilitie
121
94
122
95
o .mu .Lock ()
123
96
defer o .mu .Unlock ()
124
- agg , err := o .aggregatorFactory (c .AggregationMethod , * c .AggregationConfig , o .lggr )
97
+ agg , err := o .aggregatorFactory (c .AggregationMethod , * c .AggregationConfig , o .eng )
125
98
if err != nil {
126
99
return err
127
100
}
128
101
o .aggregators [request .Metadata .WorkflowID ] = agg
129
102
130
- encoder , err := o .encoderFactory (c .Encoder , c .EncoderConfig , o .lggr )
103
+ encoder , err := o .encoderFactory (c .Encoder , c .EncoderConfig , o .eng )
131
104
if err != nil {
132
105
return err
133
106
}
@@ -155,7 +128,7 @@ func (o *capability) GetEncoderByWorkflowID(workflowID string) (types.Encoder, e
155
128
}
156
129
157
130
func (o * capability ) GetEncoderByName (encoderName string , config * values.Map ) (types.Encoder , error ) {
158
- return o .encoderFactory (encoderName , config , o .lggr )
131
+ return o .encoderFactory (encoderName , config , o .eng )
159
132
}
160
133
161
134
func (o * capability ) GetRegisteredWorkflowsIDs () []string {
@@ -205,7 +178,7 @@ func (o *capability) Execute(ctx context.Context, r capabilities.CapabilityReque
205
178
}
206
179
err := r .Inputs .UnwrapTo (& m )
207
180
if err != nil {
208
- o .lggr .Warnf ("could not unwrap method from CapabilityRequest, using default: %v" , err )
181
+ o .eng .Warnf ("could not unwrap method from CapabilityRequest, using default: %v" , err )
209
182
}
210
183
211
184
switch m .Method {
@@ -214,10 +187,10 @@ func (o *capability) Execute(ctx context.Context, r capabilities.CapabilityReque
214
187
if err != nil {
215
188
return capabilities.CapabilityResponse {}, fmt .Errorf ("failed to create map for response inputs: %w" , err )
216
189
}
217
- o .lggr .Debugw ("Execute - sending response" , "workflowExecutionID" , r .Metadata .WorkflowExecutionID , "inputs" , inputs , "terminate" , m .Terminate )
190
+ o .eng .Debugw ("Execute - sending response" , "workflowExecutionID" , r .Metadata .WorkflowExecutionID , "inputs" , inputs , "terminate" , m .Terminate )
218
191
var responseErr error
219
192
if m .Terminate {
220
- o .lggr .Debugw ("Execute - terminating execution" , "workflowExecutionID" , r .Metadata .WorkflowExecutionID )
193
+ o .eng .Debugw ("Execute - terminating execution" , "workflowExecutionID" , r .Metadata .WorkflowExecutionID )
221
194
responseErr = capabilities .ErrStopExecution
222
195
}
223
196
out := requests.Response {
@@ -298,7 +271,7 @@ func (o *capability) queueRequestForProcessing(
298
271
ExpiresAt : o .clock .Now ().Add (requestTimeout ),
299
272
}
300
273
301
- o .lggr .Debugw ("Execute - adding to store" , "workflowID" , r .WorkflowID , "workflowExecutionID" , r .WorkflowExecutionID , "observations" , r .Observations )
274
+ o .eng .Debugw ("Execute - adding to store" , "workflowID" , r .WorkflowID , "workflowExecutionID" , r .WorkflowExecutionID , "observations" , r .Observations )
302
275
303
276
o .reqHandler .SendRequest (ctx , r )
304
277
return callbackCh , nil
0 commit comments