-
Notifications
You must be signed in to change notification settings - Fork 115
/
parallel_script.go
114 lines (89 loc) · 2.85 KB
/
parallel_script.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
package script
import (
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type ParallelScript struct {
name string
allScripts []Script
logTag string
logger boshlog.Logger
}
type scriptResult struct {
Script Script
Error error
}
func NewParallelScript(name string, scripts []Script, logger boshlog.Logger) ParallelScript {
return ParallelScript{
name: name,
allScripts: scripts,
logTag: "ParallelScript",
logger: logger,
}
}
func (s ParallelScript) Tag() string { return "" }
func (s ParallelScript) Path() string { return "" }
func (s ParallelScript) Exists() bool { return true }
func (s ParallelScript) Run() error {
existingScripts := s.findExistingScripts(s.allScripts)
s.logger.Info(s.logTag, "Will run %d %s scripts in parallel", len(existingScripts), s.name)
resultsChan := make(chan scriptResult)
for _, script := range existingScripts {
script := script
go func() { resultsChan <- scriptResult{script, script.Run()} }()
}
var failedScripts, passedScripts []string
for i := 0; i < len(existingScripts); i++ {
select {
case r := <-resultsChan:
jobName := r.Script.Tag()
if r.Error == nil {
passedScripts = append(passedScripts, jobName)
s.logger.Info(s.logTag, "'%s' script has successfully executed", r.Script.Path())
} else {
failedScripts = append(failedScripts, jobName)
s.logger.Error(s.logTag, "'%s' script has failed with error: %s", r.Script.Path(), r.Error)
}
}
}
return s.summarizeErrs(passedScripts, failedScripts)
}
func (s ParallelScript) Cancel() error {
s.logger.Debug(s.logTag, "Canceling a parallel script")
existingScripts := s.findExistingScripts(s.allScripts)
for _, script := range existingScripts {
if script, ok := script.(CancellableScript); ok {
err := script.Cancel()
if err != nil {
bosherr.WrapErrorf(err, "'%s' script did not cancel", s.name)
}
} else {
return bosherr.Errorf("Script %s is not cancellable", s.name)
}
}
return nil
}
func (s ParallelScript) findExistingScripts(all []Script) []Script {
var existing []Script
for _, script := range all {
if script.Exists() {
s.logger.Debug(s.logTag, "Found '%s' script in job '%s'", s.name, script.Tag())
existing = append(existing, script)
} else {
s.logger.Debug(s.logTag, "Did not find '%s' script in job '%s'", s.name, script.Tag())
}
}
return existing
}
func (s ParallelScript) summarizeErrs(passedScripts, failedScripts []string) error {
if len(failedScripts) > 0 {
errMsg := "Failed Jobs: " + strings.Join(failedScripts, ", ")
if len(passedScripts) > 0 {
errMsg += ". Successful Jobs: " + strings.Join(passedScripts, ", ")
}
totalRan := len(passedScripts) + len(failedScripts)
return bosherr.Errorf("%d of %d %s scripts failed. %s.", len(failedScripts), totalRan, s.name, errMsg)
}
return nil
}