forked from monzo/orchestra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restart.go
73 lines (61 loc) · 1.57 KB
/
restart.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
package commands
import (
"runtime"
"strings"
"github.com/urfave/cli/v2"
"github.com/wsxiaoys/terminal"
"github.com/tifo/orchestra/services"
)
var RestartCommand = &cli.Command{
Name: "restart",
Usage: "Restarts all the services",
Action: BeforeAfterWrapper(RestartAction),
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "attach, a",
Usage: "Attach to services output after start",
},
&cli.BoolFlag{
Name: "logs, l",
Usage: "Start logging after start",
},
},
}
// RestartAction restarts all the services (or the specified ones)
func RestartAction(c *cli.Context) error {
worker := func(service *services.Service) func() {
return func() {
restart(c, service)
}
}
pool := make(workerPool, runtime.NumCPU())
svcs := services.Sort(FilterServices(c))
for _, service := range svcs {
pool.Do(worker(service))
}
pool.Drain()
if c.Bool("attach") || c.Bool("logs") {
_ = LogsAction(c)
}
return nil
}
func restart(c *cli.Context, service *services.Service) {
spacing := strings.Repeat(" ", services.MaxServiceNameLength+2-len(service.Name))
err := killService(service)
if err != nil {
appendError(err)
terminal.Stdout.Colorf("%s%s| @{r} error: @{|}%s\n", service.Name, spacing, err.Error())
return
}
rebuilt, err := buildAndStart(c, service)
if err != nil {
appendError(err)
terminal.Stdout.Colorf("%s%s| @{r} error: @{|}%s\n", service.Name, spacing, err.Error())
return
}
var rebuiltStatus string
if rebuilt {
rebuiltStatus = "rebuilt & "
}
terminal.Stdout.Colorf("%s%s| @{c} %srestarted\n", service.Name, spacing, rebuiltStatus)
}