-
Notifications
You must be signed in to change notification settings - Fork 12
/
invokedoperation.go
277 lines (232 loc) · 7.44 KB
/
invokedoperation.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
package model
import (
"errors"
"fmt"
"time"
"github.com/google/uuid"
"github.com/microsoft/commercial-marketplace-offer-deploy/sdk"
)
const DefaultNumberOfRetries = 3
// InvokedOperation is a record of an operation that was invoked by the operator
//
// remarks:
// attributes - specify information about or that control the operation's use and behavior
type InvokedOperation struct {
BaseWithGuidPrimaryKey
ParentID *uuid.UUID `json:"parentId" gorm:"type:uuid"`
Name string `json:"name"`
DeploymentId uint `json:"deploymentId"`
Attributes []InvokedOperationAttribute `json:"attributes"`
Retries uint `json:"retries"`
Attempts uint `json:"attempts"`
Parameters map[string]any `json:"parameters" gorm:"json"`
Results map[uint]*InvokedOperationResult `json:"results" gorm:"json"`
Status string `json:"status"` // the current or final status of the operation
Completed bool `json:"completed"`
}
type InvokedOperationResult struct {
Attempt uint `json:"attempt"`
Error string `json:"error"`
Value any `json:"value" gorm:"json"`
Status string `json:"status"`
StartedAt time.Time `json:"startedAt"`
CompletedAt time.Time `json:"completedAt"`
}
// can the operation be executed? if not, then reasons are returned
func (o *InvokedOperation) IsExecutable() ([]string, bool) {
reasons := []string{}
isCompleted := o.IsCompleted()
if isCompleted {
reasons = append(reasons, fmt.Sprintf("operation already completed on [%s]", o.LatestResult().CompletedAt.String()))
}
attemptsExceeded := o.AttemptsExceeded()
if attemptsExceeded {
reasons = append(reasons, fmt.Sprintf("operation has exceeded the maximum number of attempts (%d)", o.Retries))
}
return reasons, (!isCompleted && !attemptsExceeded)
}
func (o *InvokedOperation) IsRetry() bool {
return !o.IsCompleted() && o.Attempts > 1
}
func (o *InvokedOperation) IsFirstAttempt() bool {
return !o.IsCompleted() && o.Attempts == 1
}
func (o *InvokedOperation) IsRunning() bool {
return o.Status == sdk.StatusRunning.String()
}
func (o *InvokedOperation) IsPending() bool {
return o.Status == sdk.StatusPending.String()
}
func (o *InvokedOperation) IsCompleted() bool {
return o.Completed
}
func (o *InvokedOperation) IsScheduled() bool {
return o.Status == sdk.StatusScheduled.String()
}
func (o *InvokedOperation) CompletedAt() (*time.Time, error) {
if !o.IsCompleted() {
return nil, fmt.Errorf("operation is not completed")
}
completedAt := o.LatestResult().CompletedAt
return &completedAt, nil
}
// increment the number of attempts and set the status to running
// if it's in a state where it can be run.
// return: returns true if status changed to running
func (o *InvokedOperation) Pending() {
if o.IsPending() || o.AttemptsExceeded() { //already pending, so do nothing
return
}
o.setStatus(sdk.StatusPending.String())
}
// increment the number of attempts and set the status to running
// if it's in a state where it can be run.
// return: returns true if status changed to running
func (o *InvokedOperation) Running() {
if o.IsRunning() || o.AttemptsExceeded() { //already running, so do nothing
return
}
o.incrementAttempts()
o.setStatus(sdk.StatusRunning.String())
}
// whether the operation execution is being picked up where it last left off if interrupted
func (o *InvokedOperation) IsContinuation() bool {
if o.IsCompleted() {
return false
}
return o.IsRunning() && !o.AttemptsExceeded()
}
func (o *InvokedOperation) FirstResult() *InvokedOperationResult {
if len(o.Results) == 0 {
return o.appendResult()
}
if _, ok := o.Results[1]; !ok {
return o.appendResult()
}
return o.Results[1]
}
func (o *InvokedOperation) LatestResult() *InvokedOperationResult {
if len(o.Results) == 0 {
return o.appendResult()
}
if _, ok := o.Results[o.Attempts]; !ok {
return o.appendResult()
}
return o.Results[o.Attempts]
}
func (o *InvokedOperation) Error(err error) {
o.LatestResult().Error = err.Error()
o.setStatus(sdk.StatusError.String())
}
func (o *InvokedOperation) Value(v any) {
o.LatestResult().Value = v
}
func (o *InvokedOperation) Attribute(key AttributeKey, v any) {
if o.Attributes == nil {
o.Attributes = []InvokedOperationAttribute{}
}
for i, attr := range o.Attributes {
if attr.Key == string(key) {
o.Attributes[i].Value = v
return
}
}
o.Attributes = append(o.Attributes, NewAttribute(key, v))
}
// does the InvokedOperation have an attribute with the specified key?
func (o *InvokedOperation) HasAttribute(key AttributeKey) bool {
for _, attr := range o.Attributes {
if attr.Key == string(key) {
return true
}
}
return false
}
func (o *InvokedOperation) AttributeValue(key AttributeKey) (any, bool) {
for _, attr := range o.Attributes {
if attr.Key == string(key) {
return attr.Value, true
}
}
return nil, false
}
func (o *InvokedOperation) ParameterValue(key ParameterKey) (any, bool) {
for k, v := range o.Parameters {
if k == string(key) {
return v, true
}
}
return nil, false
}
func (o *InvokedOperation) AttemptsExceeded() bool {
return o.Attempts > o.Retries
}
func (o *InvokedOperation) IsRetrying() bool {
return o.Status == string(sdk.StatusScheduled) && (!o.AttemptsExceeded() && o.Attempts > 1)
}
// sets the status to failed for the operation and the latest attempt's result
func (o *InvokedOperation) Failed() error {
return o.setStatus(sdk.StatusFailed.String())
}
// sets the status to success for the operation and the latest attempt's result
func (o *InvokedOperation) Success() error {
return o.setStatus(sdk.StatusSuccess.String())
}
func (o *InvokedOperation) Complete() {
o.Completed = true
o.LatestResult().CompletedAt = time.Now().UTC()
}
func (o *InvokedOperation) Schedule() error {
if o.AttemptsExceeded() {
return fmt.Errorf("cannot schedule operation, %d of %d attemps reached", o.Attempts, o.Retries)
}
return o.setStatus(sdk.StatusScheduled.String())
}
func (o *InvokedOperation) setStatus(status string) error {
// if the operation is complete, the status cannot be set
if o.IsCompleted() {
return fmt.Errorf("cannot set status to %s, operation is already complete", status)
}
o.Status = status // track the latest status
//anything but schedule, update the results
if status != string(sdk.StatusScheduled) {
result := o.LatestResult()
result.Status = status
result.CompletedAt = time.Now().UTC()
}
return nil
}
func (o *InvokedOperation) incrementAttempts() {
if o.AttemptsExceeded() || o.IsCompleted() {
return
}
o.Attempts++
}
func (o *InvokedOperation) CorrelationId() (*uuid.UUID, error) {
value, ok := o.AttributeValue(AttributeKeyCorrelationId)
if !ok {
return nil, errors.New("no correlation id found for operation")
}
correlationIdString := fmt.Sprintf("%v", value)
correlationId, err := uuid.Parse(correlationIdString)
if err != nil {
return nil, err
}
return &correlationId, nil
}
func (o *InvokedOperation) appendResult() *InvokedOperationResult {
if o.Results == nil {
o.Results = make(map[uint]*InvokedOperationResult)
}
if _, exists := o.Results[o.Attempts]; exists {
return o.Results[o.Attempts]
}
result := &InvokedOperationResult{
Attempt: o.Attempts,
StartedAt: time.Now().UTC(),
Value: nil,
Status: sdk.StatusRunning.String(),
}
o.Results[o.Attempts] = result
return result
}