-
Notifications
You must be signed in to change notification settings - Fork 41
/
invocation.go
151 lines (135 loc) · 4.1 KB
/
invocation.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
package projectors
import (
"fmt"
"github.com/fission/fission-workflows/pkg/api/events"
"github.com/fission/fission-workflows/pkg/fes"
"github.com/fission/fission-workflows/pkg/types"
"github.com/golang/protobuf/ptypes"
)
type WorkflowInvocation struct {
taskRunProjector *TaskRun
}
func NewWorkflowInvocation() *WorkflowInvocation {
return &WorkflowInvocation{
taskRunProjector: NewTaskRun(),
}
}
func (i *WorkflowInvocation) Project(base fes.Entity, events ...*fes.Event) (updated fes.Entity, err error) {
var invocation *types.WorkflowInvocation
if base == nil {
invocation = &types.WorkflowInvocation{}
} else {
var ok bool
invocation, ok = base.(*types.WorkflowInvocation)
if !ok {
return nil, fmt.Errorf("entity expected workflow, but was %T", base)
}
invocation = invocation.Copy()
}
for _, event := range events {
err := i.project(invocation, event)
if err != nil {
return nil, err
}
}
return invocation, nil
}
func (i *WorkflowInvocation) project(wi *types.WorkflowInvocation, event *fes.Event) error {
// If the event is a task event, use the Task Aggregate to resolve it.
if event.Aggregate.Type == types.TypeTaskRun {
return i.applyTaskEvent(wi, event)
}
if err := i.ensureNextEvent(event); err != nil {
return err
}
eventData, err := fes.ParseEventData(event)
if err != nil {
return err
}
switch m := eventData.(type) {
case *events.InvocationCreated:
wi.Metadata = &types.ObjectMetadata{
Id: event.Aggregate.Id,
CreatedAt: event.Timestamp,
}
wi.Spec = m.GetSpec()
wi.Status = &types.WorkflowInvocationStatus{
Status: types.WorkflowInvocationStatus_IN_PROGRESS,
Tasks: map[string]*types.TaskInvocation{},
DynamicTasks: map[string]*types.Task{},
}
case *events.InvocationCanceled:
wi.Status.Status = types.WorkflowInvocationStatus_ABORTED
wi.Status.Error = m.GetError()
case *events.InvocationCompleted:
wi.Status.Status = types.WorkflowInvocationStatus_SUCCEEDED
wi.Status.Output = m.GetOutput()
wi.Status.OutputHeaders = m.GetOutputHeaders()
case *events.InvocationTaskAdded:
task := m.GetTask()
if wi.Status.DynamicTasks == nil {
wi.Status.DynamicTasks = map[string]*types.Task{}
}
wi.Status.DynamicTasks[task.ID()] = task
case *events.InvocationFailed:
wi.Status.Error = m.GetError()
wi.Status.Status = types.WorkflowInvocationStatus_FAILED
default:
//key := wi.Aggregate()
return fes.ErrUnsupportedEntityEvent.WithEvent(event)
}
wi.Metadata.Generation++
wi.Status.UpdatedAt = event.GetTimestamp()
return err
}
func (i *WorkflowInvocation) NewProjection(key fes.Aggregate) (fes.Entity, error) {
if key.Type != types.TypeInvocation {
return nil, fes.ErrInvalidAggregate.WithAggregate(&key)
}
return &types.WorkflowInvocation{
Metadata: &types.ObjectMetadata{
Id: key.Id,
CreatedAt: ptypes.TimestampNow(),
},
Spec: &types.WorkflowInvocationSpec{},
Status: &types.WorkflowInvocationStatus{},
}, nil
}
func (i *WorkflowInvocation) ensureNextEvent(event *fes.Event) error {
if err := fes.ValidateEvent(event); err != nil {
return err
}
if event.Aggregate.Type != types.TypeInvocation {
return fes.ErrUnsupportedEntityEvent.WithEvent(event)
}
// TODO check sequence of event
return nil
}
func (i *WorkflowInvocation) applyTaskEvent(invocation *types.WorkflowInvocation, event *fes.Event) error {
wiAggregate := fes.GetAggregate(invocation)
if wiAggregate != *event.Parent {
return fmt.Errorf("event does not belong to invocation: (expected: %v, value: %v)", wiAggregate, *event.Parent)
}
taskID := event.Aggregate.Id
task, ok := invocation.Status.Tasks[taskID]
if !ok {
entity, _ := i.taskRunProjector.NewProjection(*event.Aggregate)
task, _ = entity.(*types.TaskInvocation)
}
task = task.Copy()
err := i.taskRunProjector.project(task, event)
if err != nil {
return err
}
if invocation.Status.Tasks == nil {
invocation.Status.Tasks = map[string]*types.TaskInvocation{}
}
invocation.Status.Tasks[taskID] = task
return nil
}
func NewInvocationAggregate(invocationID string) fes.Aggregate {
return fes.Aggregate{
Id: invocationID,
Type: types.TypeInvocation,
}
}