-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_svc.go
564 lines (473 loc) · 14.4 KB
/
task_svc.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
Copyright 2022-2023 The nagare media authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package svc
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"text/template"
"time"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/yaml"
backoff "github.com/cenkalti/backoff/v4"
cloudevents "github.com/cloudevents/sdk-go/v2"
enginev1 "github.com/nagare-media/engine/api/v1alpha1"
"github.com/nagare-media/engine/internal/pkg/uuid"
"github.com/nagare-media/engine/internal/task-shim/actions"
metaaction "github.com/nagare-media/engine/internal/task-shim/actions/meta"
"github.com/nagare-media/engine/pkg/events"
"github.com/nagare-media/engine/pkg/nbmp"
nbmpsvcv2 "github.com/nagare-media/engine/pkg/nbmp/svc/v2"
nbmputils "github.com/nagare-media/engine/pkg/nbmp/utils"
"github.com/nagare-media/engine/pkg/strobj"
"github.com/nagare-media/engine/pkg/tplfuncs"
nbmpv2 "github.com/nagare-media/models.go/iso/nbmp/v2"
)
const (
// TODO: make configurable
ReportTimeout = 10 * time.Second
)
type taskService struct {
cfg *enginev1.TaskShimTaskServiceConfiguration
rootCtx context.Context
terminateFunc func(error)
mtx sync.RWMutex
engineWorkflowID string // protected by mtx, but constant once Create was called
engineTaskID string // protected by mtx, but constant once Create was called
engineInstanceID string // protected by mtx, but constant once Create was called
tsk *nbmpv2.Task // protected by mtx
tskErr error // protected by mtx
tskDone chan struct{} // protected by mtx
tskCtx context.Context // protected by mtx
tskCtxCancel context.CancelFunc // protected by mtx
reportClient events.Client
}
var _ nbmpsvcv2.TaskService = &taskService{}
func NewTaskService(ctx context.Context, terminateFunc func(error), cfg *enginev1.TaskShimTaskServiceConfiguration) *taskService {
s := &taskService{
cfg: cfg,
rootCtx: ctx,
terminateFunc: terminateFunc,
}
s.init()
return s
}
func (s *taskService) init() {
// Go routine to handle root context cancellation
go func() {
// wait for cancellation
<-s.rootCtx.Done()
l := log.FromContext(s.rootCtx)
// check if task is running and wait for it to terminate
s.mtx.Lock()
tskDone := s.tskDone
s.mtx.Unlock()
if tskDone != nil {
<-tskDone
}
// terminate process
l.Info("terminate process")
s.terminateFunc(s.rootCtx.Err())
}()
}
func (s *taskService) Create(ctx context.Context, t *nbmpv2.Task) error {
s.mtx.Lock()
defer s.mtx.Unlock()
// create is only allowed if Task does not exist
if s.tsk != nil {
return nbmp.ErrAlreadyExists
}
l := log.FromContext(s.rootCtx)
l.Info("create task")
s.engineInstanceID = t.General.ID
if t.Configuration != nil {
s.engineWorkflowID, _ = nbmputils.GetStringParameterValue(t.Configuration.Parameters, nbmp.EngineWorkflowIDParameterKey)
s.engineTaskID, _ = nbmputils.GetStringParameterValue(t.Configuration.Parameters, nbmp.EngineTaskIDParameterKey)
}
l = l.WithValues(
"workflow", s.engineWorkflowID,
"task", s.engineTaskID,
"instance", s.engineInstanceID,
)
s.rootCtx = log.IntoContext(s.rootCtx, l)
// create reportClient if Task includes Reporting descriptors
if err := s.createReportClient(t); err != nil {
l.Error(err, "disable event reporting")
}
s.observeEvent(events.TaskCreated)
// run onCreate actions
ctx = log.IntoContext(ctx, l.WithValues("reason", "create"))
return s.execEventActions(ctx, s.cfg.OnCreateActions, t)
}
func (s *taskService) createReportClient(t *nbmpv2.Task) error {
// validate
if t.Reporting == nil {
return errors.New("not configured")
}
if t.Reporting.ReportType != nbmp.ReportTypeEngineCloudEvents {
return fmt.Errorf("unsupported report type '%s'", t.Reporting.ReportType)
}
if t.Reporting.DeliveryMethod != nbmpv2.HTTP_POSTDeliveryMethod {
return fmt.Errorf("unsupported delivery method '%s'", t.Reporting.ReportType)
}
// create report client
c := &events.HTTPClient{
URL: string(t.Reporting.URL),
Client: http.DefaultClient,
}
expBackOff := &backoff.ExponentialBackOff{
InitialInterval: 500 * time.Millisecond,
RandomizationFactor: 0.25,
Multiplier: 1.5,
MaxInterval: 2 * time.Second,
MaxElapsedTime: 0, // = indefinitely (we use contexts for that)
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}
s.reportClient = events.ClientWithBackoff(c, expBackOff)
return nil
}
func (s *taskService) Update(ctx context.Context, t *nbmpv2.Task) error {
s.mtx.Lock()
defer s.mtx.Unlock()
// update is only allowed if Task was created, IDs match...
if s.tsk == nil || s.engineInstanceID != t.General.ID {
return nbmp.ErrNotFound
}
// ...and it has not terminated
if s.hasTerminated() {
return nbmp.ErrInvalid
}
l := log.FromContext(s.rootCtx)
l.Info("update task")
s.observeEvent(events.TaskUpdated)
// run onUpdate actions
ctx = log.IntoContext(ctx, l.WithValues("reason", "update"))
return s.execEventActions(ctx, s.cfg.OnUpdateActions, t)
}
func (s *taskService) Delete(ctx context.Context, t *nbmpv2.Task) error {
l := log.FromContext(s.rootCtx)
l.Info("delete task")
s.observeEvent(events.TaskDeleted)
s.mtx.Lock()
defer s.mtx.Unlock()
// call to Delete should always result in termination of process
defer l.Info("terminate process")
defer s.terminateFunc(s.tskErr)
// if Task has never been created => just exit
if s.tsk == nil {
l.Info("no task running")
return nil
}
*t = *s.tsk
// Task is running or has terminated => run onDelete actions
ctx = log.IntoContext(ctx, l.WithValues("reason", "delete"))
if err := s.execEventActions(ctx, s.cfg.OnDeleteActions, t); err != nil {
return err
}
// if Task has terminated => just exit
if s.hasTerminated() {
l.Info("task has already terminated")
return nil
}
// Task is running => stop it and wait for termination
l.Info("terminate task")
s.tskCtxCancel()
<-s.tskDone
return nil
}
func (s *taskService) Retrieve(ctx context.Context, t *nbmpv2.Task) error {
l := log.FromContext(s.rootCtx)
l.V(1).Info("retrieve task")
if !s.mtx.TryRLock() {
return nbmp.ErrRetryLater
}
defer s.mtx.RUnlock()
// retrieve is only allowed if Task was created and IDs match
if s.tsk == nil || s.engineInstanceID != t.General.ID {
return nbmp.ErrNotFound
}
*t = *s.tsk
return nil
}
func (s *taskService) execEventActions(ctx context.Context, al []enginev1.TaskServiceAction, currentTask *nbmpv2.Task) error {
// assumption: s.mtx is already locked by caller
l := log.FromContext(s.rootCtx)
l.Info("execute event actions")
for _, a := range al {
l := l.WithValues(
"name", a.Name,
"action", a.Action,
)
// check for meta actions
if a.Action == metaaction.Name {
cfg, err := evaluateConfigToString(a, s.tsk, currentTask)
if err != nil {
return err
}
l := l.WithValues("config", cfg)
l.Info("execute action")
switch metaaction.ConfigType(cfg) {
case metaaction.ConfigTypeStartTask:
s.startTask(log.IntoContext(ctx, l), currentTask)
continue
case metaaction.ConfigTypeRestartTask:
s.restartTask(log.IntoContext(ctx, l), currentTask)
continue
case metaaction.ConfigTypeStopTask:
s.stopTask(log.IntoContext(ctx, l))
continue
}
}
// execute action
l.Info("execute action")
var err error
currentTask, err = execAction(log.IntoContext(ctx, l), a, s.tsk, currentTask)
if err != nil {
l.Error(err, "action failed")
return err
}
}
l.Info("event actions finished")
s.tsk = currentTask
return nil
}
func (s *taskService) startTask(ctx context.Context, currentTask *nbmpv2.Task) {
// assumption: s.mtx is already locked by caller
l := log.FromContext(s.rootCtx)
// create new task context
s.tsk = currentTask
s.tskDone = make(chan struct{})
s.tskCtx, s.tskCtxCancel = context.WithCancel(s.rootCtx)
// start task
l.Info("starting task")
s.tsk.General.State = &nbmpv2.RunningState
s.observeEvent(events.TaskStarted)
go func() {
s.tskErr = s.execTask(s.tskCtx, currentTask)
s.mtx.Lock()
if s.tskErr != nil {
l.Error(s.tskErr, "task failed")
s.tsk.General.State = &nbmpv2.InErrorState
s.observeEvent(events.TaskFailed)
} else {
s.tsk.General.State = &nbmpv2.DestroyedState
s.observeEvent(events.TaskSucceeded)
}
l.Info("task finished")
s.mtx.Unlock()
close(s.tskDone)
}()
}
func (s *taskService) restartTask(ctx context.Context, currentTask *nbmpv2.Task) {
// assumption: s.mtx is already locked by caller
l := log.FromContext(s.rootCtx)
l.Info("restarting task")
s.stopTask(ctx)
s.startTask(ctx, currentTask)
}
func (s *taskService) stopTask(ctx context.Context) {
// assumption: s.mtx is already locked by caller
l := log.FromContext(s.rootCtx)
l.Info("stopping task")
// check if stop is required
if s.tskDone == nil {
l.Info("task has never started")
return
}
if s.hasTerminated() {
l.Info("task has already stopped")
return
}
// cancel task context and wait for termination
s.tskCtxCancel()
l.Info("waiting for task termination")
<-s.tskDone
l.Info("task stopped")
s.observeEvent(events.TaskStopped)
}
func (s *taskService) execTask(ctx context.Context, currentTask *nbmpv2.Task) error {
// This is the main task execution function. It will run in its own Goroutine.
l := log.FromContext(s.rootCtx)
// remember pointer to last set task
s.mtx.Lock()
lastSetTsk := s.tsk
s.mtx.Unlock()
for _, a := range s.cfg.Actions {
// set new task if ctx is not done
select {
case <-ctx.Done():
// we should terminate
l.Info("termination requested; terminate task")
return nil
default:
s.mtx.Lock()
if s.tsk == lastSetTsk {
// there has been no update to the task during last action
s.tsk = currentTask
lastSetTsk = s.tsk
} else {
// task has been updated => ignore our currentTask and update pointers
currentTask = s.tsk
lastSetTsk = s.tsk
}
s.mtx.Unlock()
}
l := l.WithValues(
"name", a.Name,
"action", a.Action,
"reason", "task",
)
// check for meta actions
if a.Action == metaaction.Name {
cfg, err := evaluateConfigToString(a, nil, currentTask)
if err != nil {
return err
}
l := l.WithValues("config", cfg)
l.Info("execute action")
switch metaaction.ConfigType(cfg) {
case metaaction.ConfigTypeStartTask:
return errors.New("svc: failed to start task: cannot start task from running task")
case metaaction.ConfigTypeRestartTask:
return errors.New("svc: failed to restart task: cannot restart task from running task")
case metaaction.ConfigTypeStopTask:
return errors.New("svc: failed to stop task: cannot stop task from running task")
}
}
// execute action
l.Info("execute action")
var err error
tskCopy := &nbmpv2.Task{}
nbmputils.MustDeepCopyInto(currentTask, tskCopy)
currentTask, err = execAction(log.IntoContext(ctx, l), a, nil, tskCopy)
if err != nil {
l.Error(err, "action failed")
return err
}
}
return nil
}
func (s *taskService) hasTerminated() bool {
// assumption: s.mtx is already locked by caller
// tskDone is nil => task has not started yet
if s.tskDone == nil {
return false
}
select {
case <-s.tskDone:
// channel is closed => task has terminated
return true
default:
// channel is open => task is running
return false
}
}
func (s *taskService) observeEvent(t string) {
if s.reportClient == nil {
return
}
// TODO: implement event filtering
l := log.FromContext(s.rootCtx)
e := cloudevents.NewEvent()
e.SetID(uuid.UUIDv4())
e.SetType(t)
e.SetSource("/engine.nagare.media/task-shim")
e.SetSubject(fmt.Sprintf("/engine.nagare.media/workflow(%s)/task(%s)/instance(%s)", s.engineWorkflowID, s.engineTaskID, s.engineInstanceID))
e.SetTime(time.Now())
if err := e.Validate(); err != nil {
panic(fmt.Sprintf("Event creation results in invalid CloudEvent; fix implementation! error: %s", err))
}
// we start a new context because rootCtx might have been canceled
ctx, cancel := context.WithTimeout(context.Background(), ReportTimeout)
defer cancel()
if err := s.reportClient.Send(ctx, e); err != nil {
l.Error(err, "failed to report event")
return
}
}
func execAction(ctx context.Context, a enginev1.TaskServiceAction, oldTask, currentTask *nbmpv2.Task) (*nbmpv2.Task, error) {
// evaluate configuration
cfg, err := evaluateConfigToRawJSON(a, oldTask, currentTask)
if err != nil {
return nil, err
}
// get action builder
builder, ok := actions.ActionBuilders.Get(a.Action)
if !ok {
return nil, fmt.Errorf("action named %s not registered", a.Action)
}
// build action
actionCtx := actions.Context{
Ctx: ctx,
Config: cfg,
ContextData: actions.ContextData{
OldTask: oldTask,
Task: currentTask,
},
}
action, err := builder(actionCtx)
if err != nil {
return nil, err
}
// execute action
return action.Exec(actionCtx)
}
func evaluateConfigToString(a enginev1.TaskServiceAction, oldTask, currentTask *nbmpv2.Task) (string, error) {
j, err := evaluateConfigToRawJSON(a, oldTask, currentTask)
if err != nil {
return "", err
}
var str string
if err = json.Unmarshal(j, &str); err != nil {
return "", err
}
return str, nil
}
func evaluateConfigToRawJSON(a enginev1.TaskServiceAction, oldTask, currentTask *nbmpv2.Task) ([]byte, error) {
switch {
case a.Config == nil:
return []byte("null"), nil
case a.Config.Type == strobj.Object:
return a.Config.ObjectVal.Raw, nil
case a.Config.Type == strobj.String:
// evaluate string as a Go template...
tpl, err := template.
New("config").
Funcs(tplfuncs.DefaultFuncMap()).
Parse(a.Config.StrVal)
if err != nil {
return nil, err
}
buf := &bytes.Buffer{}
tplData := actions.ContextData{
OldTask: oldTask,
Task: currentTask,
}
if err = tpl.Execute(buf, tplData); err != nil {
return nil, err
}
// ...then parse it as YAML and convert it to a JSON string...
raw, err := yaml.YAMLToJSON(buf.Bytes())
if err != nil {
return nil, err
}
return raw, nil
default:
return nil, fmt.Errorf("svc: impossible StringOrObject.Type")
}
}