-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathshutdown.go
93 lines (84 loc) · 1.82 KB
/
shutdown.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
package main
import (
"log"
"os"
"os/exec"
"strings"
"sync"
"time"
)
type commandList struct {
sync.Mutex
commands []*exec.Cmd
}
func (cl *commandList) Add(cmd *exec.Cmd) {
cl.Lock()
cl.commands = append(cl.commands, cmd)
cl.Unlock()
}
func (cl *commandList) KillAll() {
for _, c := range cl.commands {
if c.Process != nil {
_ = c.Process.Kill()
}
}
cl.commands = nil
}
func parallelExecute(cmd *exec.Cmd, wg *sync.WaitGroup) {
err := cmd.Start()
if err != nil {
log.Printf("Error starting parallel command %s: %s", cmd.Path, err)
}
wg.Add(1)
err = cmd.Wait()
wg.Done()
if err != nil {
log.Printf("Error while running parallel command %s: %s", cmd.Path, err)
}
}
func shutdownSequence(conf *config) {
done := make(chan struct{})
cl := new(commandList)
wg := new(sync.WaitGroup)
t := time.NewTimer(time.Duration(conf.ShutdownTimeout) * time.Millisecond)
if conf.Commands != nil {
go func() {
select {
case <-done:
case <-t.C:
log.Println("timed out waiting for commands to finish")
cl.KillAll()
}
if conf.Shutdown {
log.Println("shutting down now")
if err := shutdownNow(); err != nil {
log.Fatal("error shutting down:", err)
}
} else {
log.Println("commands have finished")
os.Exit(0)
}
}()
for _, command := range conf.Commands {
cmdParts := strings.Split(command, " ")
if string(cmdParts[0][0]) == "!" {
osCommand := exec.Command(cmdParts[0][1:], cmdParts[1:]...)
cl.Add(osCommand)
go parallelExecute(osCommand, wg)
} else {
osCommand := exec.Command(cmdParts[0], cmdParts[1:]...)
cl.Add(osCommand)
err := osCommand.Run()
if err != nil {
log.Printf("Error while running command %s: %s", osCommand.Path, err)
}
}
}
go func() {
wg.Wait()
done <- struct{}{}
}()
} else {
shutdownNow()
}
}