forked from monzo/orchestra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
111 lines (99 loc) · 2.81 KB
/
start.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
package commands
import (
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/urfave/cli/v2"
"github.com/wsxiaoys/terminal"
"github.com/tifo/orchestra/services"
)
var StartCommand = &cli.Command{
Name: "start",
Usage: "Starts all the services",
Action: BeforeAfterWrapper(StartAction),
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",
},
},
}
// StartAction starts all the services (or the specified ones)
func StartAction(c *cli.Context) error {
worker := func(service *services.Service) func() {
return func() { start(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 start(c *cli.Context, service *services.Service) {
spacing := strings.Repeat(" ", services.MaxServiceNameLength+2-len(service.Name))
if service.Process == nil {
rebuilt, err := buildAndStart(c, service)
if err != nil {
appendError(err)
terminal.Stdout.Colorf("%s%s| @{r} error: @{|}%v\n", service.Name, spacing, err)
} else {
var rebuiltStatus string
if rebuilt {
rebuiltStatus = "(re)built and "
}
terminal.Stdout.Colorf("%s%s| @{g} %sstarted\n", service.Name, spacing, rebuiltStatus)
}
} else {
terminal.Stdout.Colorf("%s%s| @{c} already running\n", service.Name, spacing)
}
}
// startService takes a Service struct as input, creates a new log file in .orchestra,
// redirects the command stdout and stderr to the log file, configures the environment
// variables for the command and starts it. If cmd.Start() doesn't return any
// error, it will write a service.pid file in .orchestra
func buildAndStart(c *cli.Context, service *services.Service) (bool, error) {
cmd := exec.Command(service.BinPath)
rebuilt, err := installService(service)
if err != nil {
return rebuilt, err
}
outputFile, err := os.Create(service.LogFilePath)
if err != nil && os.IsNotExist(err) {
return rebuilt, err
}
defer outputFile.Close()
pidFile, err := os.Create(service.PidFilePath)
if err != nil && os.IsNotExist(err) {
return rebuilt, err
}
defer pidFile.Close()
cmd.Stdout = outputFile
cmd.Stderr = outputFile
cmd.Env = GetEnvForService(c, service)
if !c.Bool("attach") {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
if err := cmd.Start(); err != nil {
return rebuilt, err
}
_, _ = pidFile.WriteString(strconv.Itoa(cmd.Process.Pid))
time.Sleep(200 * time.Millisecond)
if !service.IsRunning() {
return rebuilt, fmt.Errorf("Service %s exited after %s", service.Name, cmd.ProcessState.UserTime().String())
}
return rebuilt, nil
}