-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow.go
63 lines (54 loc) · 1.35 KB
/
workflow.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
package gflow
import (
"fmt"
"strings"
"time"
)
const (
SERIAL = "serial"
CONCURRENT = "concurrent"
)
type Context interface{}
type Workflow struct {
Mode string
OnFailure FailureFunc
Context Context
Description string
FileNmae string
queue []Step
}
func New(mode string, retry int, desc string, filename string) *Workflow {
w := &Workflow{
Mode: strings.ToLower(mode),
queue: make([]Step, 0),
Description: desc,
FileNmae: filename,
}
w.OnFailure = RetryFailure(retry)
return w
}
func (w *Workflow) Run() error {
if w.Mode == SERIAL {
for i, step := range w.queue {
// output format
// yaml-index-uuid-label-start-end-status-error
prefix := fmt.Sprintf("%s-%d-%s-%s-%d", w.FileNmae, i, step.UUID(), step.Label(), time.Now().UnixNano()/1e6)
if err := step.Run(w.Context); err != nil {
if err := step.OnFailure(err, w.Context); err != nil {
if err := w.OnFailure(err, step, w.Context); err != nil {
fmt.Println(fmt.Sprintf("%s-%d-FAILED-%s", prefix, time.Now().UnixNano()/1e6, err))
return err
}
}
}
fmt.Println(fmt.Sprintf("%s-%d-COMPLETE-%s", prefix, time.Now().UnixNano()/1e6, "OK"))
}
}
return nil
}
func (w *Workflow) AddSteps(steps []Step) {
w.queue = append(w.queue, steps...)
}
func (w *Workflow) AddStep(step Step) {
w.queue = append(w.queue, step)
}