Skip to content

Commit 6ee18ba

Browse files
committed
Add --exit-with-highest-exit-code which exits hivemind with the highest exit code of all processes
This allows for process failures to bubble up to the shell. Example: ```shellsession $ hivemind --exit-with-highest-exit-code - <<PROCFILE job1: echo "job1 is running"; sleep 0.5; echo "Failing"; exit 13 job2: echo "job2 is running"; sleep 1; echo "Will never complete"; exit 11 PROCFILE job2 | Running... job1 | Running... job2 | job2 is running job1 | job1 is running job1 | Failing job1 | exit status 13 job2 | Interrupting... job2 | signal: interrupt At least one process failed $ echo "Hivemind exited with: $?" Hivemind exited with: 13 ```
1 parent d8b34a0 commit 6ee18ba

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

hivemind.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type hivemindConfig struct {
2121
Timeout int
2222
NoPrefix bool
2323
PrintTimestamps bool
24+
ExitWithHighest bool
2425
}
2526

2627
type hivemind struct {
@@ -97,7 +98,7 @@ func (h *hivemind) waitForExit() {
9798
}
9899
}
99100

100-
func (h *hivemind) Run() {
101+
func (h *hivemind) Run() int {
101102
fmt.Printf("\033]0;%s | hivemind\007", h.title)
102103

103104
h.done = make(chan bool, len(h.procs))
@@ -112,4 +113,15 @@ func (h *hivemind) Run() {
112113
go h.waitForExit()
113114

114115
h.procWg.Wait()
116+
117+
exitCode := 0
118+
119+
for _, proc := range h.procs {
120+
code := proc.ProcessState.ExitCode()
121+
if code > exitCode {
122+
exitCode = code
123+
}
124+
}
125+
126+
return exitCode
115127
}

main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ func main() {
3030
app.HideHelp = true
3131

3232
app.Flags = []cli.Flag{
33-
cli.StringFlag{Name: "title, w", EnvVar: "HIVEMIND_TITLE", Usage: "Specify a title of the application", Destination: &conf.Title},
34-
cli.StringFlag{Name: "processes, l", EnvVar: "HIVEMIND_PROCESSES", Usage: "Specify process names to launch. Divide names with comma", Destination: &conf.ProcNames},
33+
cli.StringFlag{Name: "title, w", EnvVar: "HIVEMIND_TITLE", Usage: "specify a title of the application", Destination: &conf.Title},
34+
cli.StringFlag{Name: "processes, l", EnvVar: "HIVEMIND_PROCESSES", Usage: "specify process names to launch. Divide names with comma", Destination: &conf.ProcNames},
3535
cli.IntFlag{Name: "port, p", EnvVar: "HIVEMIND_PORT,PORT", Usage: "specify a port to use as the base", Value: 5000, Destination: &conf.PortBase},
3636
cli.IntFlag{Name: "port-step, P", EnvVar: "HIVEMIND_PORT_STEP", Usage: "specify a step to increase port number", Value: 100, Destination: &conf.PortStep},
3737
cli.StringFlag{Name: "root, d", EnvVar: "HIVEMIND_ROOT", Usage: "specify a working directory of application. Default: directory containing the Procfile", Destination: &conf.Root},
3838
cli.IntFlag{Name: "timeout, t", EnvVar: "HIVEMIND_TIMEOUT", Usage: "specify the amount of time (in seconds) processes have to shut down gracefully before being brutally killed", Value: 5, Destination: &conf.Timeout},
3939
cli.BoolFlag{Name: "no-prefix", EnvVar: "HIVEMIND_NO_PREFIX", Usage: "process names will not be printed if the flag is specified", Destination: &conf.NoPrefix},
4040
cli.BoolFlag{Name: "print-timestamps, T", EnvVar: "HIVEMIND_PRINT_TIMESTAMPS", Usage: "timestamps will be printed if the flag is specified", Destination: &conf.PrintTimestamps},
41+
cli.BoolFlag{Name: "exit-with-highest-exit-code, e", EnvVar: "HIVEMIND_EXIT_WITH_HIGHEST_EXIT_CODE", Usage: "exit hivemind with highest exit code from all processes", Destination: &conf.ExitWithHighest},
4142
}
4243

4344
app.Action = func(c *cli.Context) error {
@@ -65,7 +66,10 @@ func main() {
6566
conf.Root, err = filepath.Abs(conf.Root)
6667
fatalOnErr(err)
6768

68-
newHivemind(conf).Run()
69+
exitCode := newHivemind(conf).Run()
70+
if exitCode > 0 && conf.ExitWithHighest {
71+
return cli.NewExitError("At least one process failed", exitCode)
72+
}
6973

7074
return nil
7175
}

0 commit comments

Comments
 (0)