/
callrole.go
337 lines (295 loc) · 8.68 KB
/
callrole.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* === This file is part of ALICE O² ===
*
* Copyright 2021 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package workflow
import (
"errors"
"strings"
texttemplate "text/template"
"time"
"github.com/AliceO2Group/Control/common/event"
"github.com/AliceO2Group/Control/common/event/topic"
pb "github.com/AliceO2Group/Control/common/protos"
"github.com/AliceO2Group/Control/configuration/template"
"github.com/AliceO2Group/Control/core/repos"
"github.com/AliceO2Group/Control/core/task"
"github.com/AliceO2Group/Control/core/the"
"github.com/AliceO2Group/Control/core/workflow/callable"
"github.com/gobwas/glob"
)
type callRole struct {
roleBase
task.Traits
FuncCall string `yaml:"-,omitempty"`
ReturnVar string `yaml:"-,omitempty"`
}
func (t *callRole) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
aux := struct {
Call struct {
Func string
Return string
Trigger *string
Await *string
Timeout *string
Critical *bool
}
}{}
type _callRole callRole
role := _callRole{}
err = unmarshal(&role)
if err != nil {
return
}
err = unmarshal(&aux)
if err != nil {
return
}
role.FuncCall = aux.Call.Func
role.ReturnVar = aux.Call.Return
// Set up basicTaskTraits defaults
if aux.Call.Trigger != nil && len(*aux.Call.Trigger) > 0 { // hook
role.Trigger = *aux.Call.Trigger
if aux.Call.Timeout != nil && len(*aux.Call.Timeout) > 0 {
role.Timeout = *aux.Call.Timeout
} else {
role.Timeout = (30 * time.Second).String()
}
if aux.Call.Await != nil && len(*aux.Call.Await) > 0 {
role.Await = *aux.Call.Await
} else {
// if no await is specified, await := trigger
role.Await = *aux.Call.Trigger
}
} else { // basic task
if aux.Call.Timeout != nil && len(*aux.Call.Timeout) > 0 {
role.Timeout = *aux.Call.Timeout
} else {
role.Timeout = "0s"
}
}
if aux.Call.Critical != nil { // default for critical is always true
role.Critical = *aux.Call.Critical
} else {
role.Critical = true
}
t.status.status = task.ACTIVE
*t = callRole(role)
return
}
func (t *callRole) MarshalYAML() (interface{}, error) {
callRole := make(map[string]interface{})
if t.Traits.Trigger != "" {
callRole["trigger"] = t.Traits.Trigger
}
if t.Traits.Await != "" {
callRole["await"] = t.Traits.Await
}
if t.Traits.Timeout != "" {
callRole["timeout"] = t.Traits.Timeout
}
callRole["critical"] = t.Traits.Critical
callRole["func"] = t.FuncCall
callRole["return"] = t.ReturnVar
auxRoleBase, err := t.roleBase.MarshalYAML()
aux := auxRoleBase.(map[string]interface{})
aux["call"] = callRole
return aux, err
}
func (t *callRole) GlobFilter(g glob.Glob) (rs []Role) {
if g.Match(t.GetPath()) {
rs = []Role{t}
}
return
}
func (t *callRole) ProcessTemplates(workflowRepo repos.IRepo, _ LoadSubworkflowFunc, baseConfigStack map[string]string) (err error) {
if t == nil {
return errors.New("role tree error when processing templates")
}
templSequence := template.Sequence{
template.STAGE0: template.Fields{
template.WrapPointer(&t.Enabled),
},
template.STAGE1: template.WrapMapItems(t.Defaults.Raw()),
template.STAGE2: template.WrapMapItems(t.Vars.Raw()),
template.STAGE3: template.WrapMapItems(t.UserVars.Raw()),
template.STAGE4: template.Fields{
template.WrapPointer(&t.Name),
template.WrapPointer(&t.FuncCall),
template.WrapPointer(&t.ReturnVar),
template.WrapPointer(&t.Timeout),
template.WrapPointer(&t.Trigger),
template.WrapPointer(&t.Await),
},
template.STAGE5: append(append(
WrapConstraints(t.Constraints),
t.wrapBindAndConnectFields()...),
template.WrapPointer(&t.Enabled)),
}
// FIXME: push cached templates here
err = templSequence.Execute(
the.ConfSvc(),
t.GetPath(),
template.VarStack{
Locals: t.Locals,
Defaults: t.Defaults,
Vars: t.Vars,
UserVars: t.UserVars,
},
t.makeBuildObjectStackFunc(),
baseConfigStack,
make(map[string]texttemplate.Template),
workflowRepo,
MakeDisabledRoleCallback(t),
)
if err != nil {
var roleDisabledErrorType *template.RoleDisabledError
if isRoleDisabled := errors.As(err, &roleDisabledErrorType); isRoleDisabled {
log.WithField("partition", t.GetEnvironmentId().String()).Trace(err.Error())
err = nil // we don't want a disabled role to be considered an error
} else {
return
}
}
// After template processing we write the Locals to Vars in order to make them available to children
for k, v := range t.Locals {
t.Vars.Set(k, v)
}
t.Enabled = strings.TrimSpace(t.Enabled)
return
}
func (t *callRole) UpdateStatus(s task.Status) {
t.updateStatus(s)
}
func (t *callRole) UpdateState(s task.State) {
t.updateState(s)
}
func (t *callRole) updateStatus(s task.Status) {
oldStatus := t.status.get()
if t.parent == nil {
log.WithField("status", s.String()).
WithField("partition", t.GetEnvironmentId().String()).
Error("cannot update status with nil parent")
}
t.status.merge(s, t)
if oldStatus != t.status.get() {
the.EventWriterWithTopic(topic.Role).WriteEvent(&pb.Ev_RoleEvent{
Name: t.Name,
Status: t.status.get().String(),
RolePath: t.GetPath(),
EnvironmentId: t.GetEnvironmentId().String(),
})
}
t.SendEvent(&event.RoleEvent{Name: t.Name, Status: t.status.get().String(), RolePath: t.GetPath()})
t.parent.updateStatus(s)
}
func (t *callRole) updateState(s task.State) {
oldState := t.state.get()
if t.parent == nil {
log.WithField("state", s.String()).
WithField("partition", t.GetEnvironmentId().String()).
Error("cannot update state with nil parent")
}
t.state.merge(s, t)
log.WithField("role", t.Name).
WithField("partition", t.GetEnvironmentId().String()).
Tracef("updated state to %s upon input state %s", t.state.get().String(), s.String())
if oldState != t.state.get() {
the.EventWriterWithTopic(topic.Role).WriteEvent(&pb.Ev_RoleEvent{
Name: t.Name,
State: t.state.get().String(),
RolePath: t.GetPath(),
EnvironmentId: t.GetEnvironmentId().String(),
})
}
t.SendEvent(&event.RoleEvent{Name: t.Name, State: t.state.get().String(), RolePath: t.GetPath()})
if t.Critical == true {
t.parent.updateState(s)
}
}
func (t *callRole) copy() copyable {
rCopy := callRole{
roleBase: *t.roleBase.copy().(*roleBase),
FuncCall: t.FuncCall,
ReturnVar: t.ReturnVar,
Traits: t.Traits,
}
rCopy.status = SafeStatus{status: rCopy.GetStatus()}
rCopy.state = SafeState{state: rCopy.GetState()}
return &rCopy
}
func (t *callRole) GenerateTaskDescriptors() (ds task.Descriptors) {
return nil
}
func (t *callRole) GetTasks() task.Tasks {
return []*task.Task{}
}
func (t *callRole) GetHooksMapForTrigger(trigger string) (hooks callable.HooksMap) {
if len(trigger) == 0 {
return make(callable.HooksMap)
}
defer t.updateState(task.INVARIANT)
// If a trigger is defined for this role &&
// If the input trigger is empty OR a positive match...
if len(t.Trigger) > 0 {
triggerName, triggerWeight := callable.ParseTriggerExpression(t.Trigger)
if trigger == triggerName {
return callable.HooksMap{
triggerWeight: callable.Hooks{callable.NewCall(t.FuncCall, t.ReturnVar, t)},
}
}
}
return make(callable.HooksMap)
}
func (t *callRole) GetAllHooks() callable.Hooks {
defer t.updateState(task.INVARIANT)
// If a trigger is defined for this role
if len(t.Trigger) > 0 {
return []callable.Hook{
callable.NewCall(t.FuncCall, t.ReturnVar, t),
}
}
return callable.Hooks{}
}
func (t *callRole) GetTaskTraits() task.Traits {
if t == nil {
return task.Traits{
Trigger: "",
Await: "",
Timeout: "0s",
Critical: false,
}
}
return t.Traits
}
func (t *callRole) GetTaskClasses() []string {
return nil
}
func (*callRole) GetRoles() []Role {
return nil
}
func (t *callRole) setParent(role Updatable) {
t.parent = role
t.Defaults.Wrap(role.GetDefaults())
t.Vars.Wrap(role.GetVars())
t.UserVars.Wrap(role.GetUserVars())
}