-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathinvocation.go
129 lines (113 loc) · 3.87 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
package apiserver
import (
"github.com/fission/fission-workflows/pkg/api"
"github.com/fission/fission-workflows/pkg/api/aggregates"
"github.com/fission/fission-workflows/pkg/api/store"
"github.com/fission/fission-workflows/pkg/fnenv"
"github.com/fission/fission-workflows/pkg/fnenv/workflows"
"github.com/fission/fission-workflows/pkg/types"
"github.com/fission/fission-workflows/pkg/types/validate"
"github.com/golang/protobuf/ptypes/empty"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
// Invocation is responsible for all functionality related to managing invocations.
type Invocation struct {
api *api.Invocation
invocations *store.Invocations
workflows *store.Workflows
fnenv *workflows.Runtime
}
func NewInvocation(api *api.Invocation, invocations *store.Invocations, workflows2 *store.Workflows) WorkflowInvocationAPIServer {
return &Invocation{
api: api,
invocations: invocations,
workflows: workflows2,
fnenv: workflows.NewRuntime(api, invocations, workflows2),
}
}
func (gi *Invocation) Validate(ctx context.Context, spec *types.WorkflowInvocationSpec) (*empty.Empty, error) {
err := validate.WorkflowInvocationSpec(spec)
if err != nil {
return nil, toErrorStatus(err)
}
return &empty.Empty{}, nil
}
func (gi *Invocation) Invoke(ctx context.Context, spec *types.WorkflowInvocationSpec) (*types.ObjectMetadata, error) {
// TODO go through same runtime as InvokeSync
// Check if the workflow required by the invocation exists
if gi.workflows != nil {
_, err := gi.workflows.GetWorkflow(spec.GetWorkflowId())
if err != nil {
return nil, err
}
}
eventID, err := gi.api.Invoke(spec, api.WithContext(ctx))
if err != nil {
return nil, toErrorStatus(err)
}
return &types.ObjectMetadata{Id: eventID}, nil
}
func (gi *Invocation) InvokeSync(ctx context.Context, spec *types.WorkflowInvocationSpec) (*types.WorkflowInvocation, error) {
wfi, err := gi.fnenv.InvokeWorkflow(spec, fnenv.WithContext(ctx))
if err != nil {
return nil, toErrorStatus(err)
}
return wfi, nil
}
func (gi *Invocation) Cancel(ctx context.Context, objectMetadata *types.ObjectMetadata) (*empty.Empty, error) {
err := gi.api.Cancel(objectMetadata.GetId())
if err != nil {
return nil, toErrorStatus(err)
}
return &empty.Empty{}, nil
}
func (gi *Invocation) Get(ctx context.Context, objectMetadata *types.ObjectMetadata) (*types.WorkflowInvocation, error) {
wi, err := gi.invocations.GetInvocation(objectMetadata.GetId())
if err != nil {
return nil, toErrorStatus(err)
}
return wi, nil
}
func (gi *Invocation) List(ctx context.Context, query *InvocationListQuery) (*WorkflowInvocationList, error) {
var invocations []string
as := gi.invocations.List()
for _, aggregate := range as {
if aggregate.Type != aggregates.TypeWorkflowInvocation {
logrus.Errorf("Invalid type in invocation invocations: %v", aggregate.Format())
continue
}
if len(query.Workflows) > 0 {
// TODO make more efficient (by moving list queries to invocations)
entity, err := gi.invocations.GetAggregate(aggregate)
if err != nil {
logrus.Errorf("List: failed to fetch %v from invocations: %v", aggregate, err)
continue
}
wfi := entity.(*aggregates.WorkflowInvocation)
if !contains(query.Workflows, wfi.GetSpec().GetWorkflowId()) {
continue
}
}
invocations = append(invocations, aggregate.Id)
}
return &WorkflowInvocationList{invocations}, nil
}
func (gi *Invocation) AddTask(ctx context.Context, req *AddTaskRequest) (*empty.Empty, error) {
invocation, err := gi.invocations.GetInvocation(req.GetInvocationID())
if err != nil {
return nil, toErrorStatus(err)
}
if err := gi.api.AddTask(invocation.ID(), req.Task); err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func contains(haystack []string, needle string) bool {
for i := 0; i < len(haystack); i++ {
if haystack[i] == needle {
return true
}
}
return false
}