-
Notifications
You must be signed in to change notification settings - Fork 1
/
exor.go
109 lines (101 loc) · 1.75 KB
/
exor.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
package executor
import (
"errors"
"fmt"
"log"
)
type exor struct {
list []Command
initialized bool
async bool
resultChan chan *Command
closeChan chan bool
}
func (h *exor) loopCheckingResult() {
var n = 0
for {
select {
case <-h.resultChan:
n++
if n >= len(h.list) {
h.closeChan <- true
break
}
}
}
}
func (h *exor) Run() error {
var (
err error
count int = len(h.list)
)
if !h.initialized {
if err = h.Init(); err != nil {
goto ERR
}
}
logStart(count)
go h.loopCheckingResult()
for i := range h.list {
command := &h.list[i]
if command.Async {
go func() {
command.Exec()
h.resultChan <- command
}()
} else {
if err = command.Exec(); !command.AllowError && err != nil {
err = errors.New(fmt.Sprintf("[STOP](%d): %s, err: %s", i, command.Name, err.Error()))
goto ERR
} else {
h.resultChan <- command
}
}
}
<-h.closeChan
logDone()
return nil
ERR:
log.Println(err.Error())
h.closeChan <- true
return err
}
// init exor
func (h exor) Init() error {
var (
err error
payload = make(map[string]interface{})
)
for i := range h.list {
c := &h.list[i]
if c.Name == "" {
c.Name = fmt.Sprintf("Command%d", i)
}
// Abandon Empty Task
if err = c.Inspect(); err != nil {
goto ERR
}
if h.async {
c.Async = true
}
if c.Async {
c.Logging = false
}
c.payload = &payload
c.index = i
}
h.initialized = true
return nil
ERR:
return errors.New("Init error: " + err.Error())
}
func logStart(count int) {
if count > 1 {
log.Printf("Start. with %d commands in all.\n", count)
} else {
log.Printf("Start. with %d command in all.\n", count)
}
}
func logDone() {
log.Println("All works are done.\n --------------------------------\n ")
}