Skip to content

Commit a613070

Browse files
committed
Add --as-job-runner: With this flag hivemind will let processes exit gracefully, and wait for all to complete
Example: ```shellsession hivemind --as-job-runner --exit-with-highest-exit-code - <<PROCFILE job1: echo "job1 is running"; sleep 0.5; echo "Done" job2: echo "job2 is running"; sleep 1; echo "Done" PROCFILE hivemind --as-job-runner --exit-with-highest-exit-code - <<PROCFILE job1: echo "job1 is running"; sleep 0.5; echo "Fail"; exit 13 job2: echo "job2 is running"; sleep 1; echo "Done" PROCFILE ```
1 parent 6ee18ba commit a613070

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

hivemind.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type hivemindConfig struct {
2222
NoPrefix bool
2323
PrintTimestamps bool
2424
ExitWithHighest bool
25+
AsJobRunner bool
2526
}
2627

2728
type hivemind struct {
@@ -32,6 +33,7 @@ type hivemind struct {
3233
done chan bool
3334
interrupted chan os.Signal
3435
timeout time.Duration
36+
jobRunner bool
3537
}
3638

3739
func newHivemind(conf hivemindConfig) (h *hivemind) {
@@ -44,6 +46,7 @@ func newHivemind(conf hivemindConfig) (h *hivemind) {
4446
}
4547

4648
h.output = &multiOutput{printProcName: !conf.NoPrefix, printTimestamp: conf.PrintTimestamps}
49+
h.jobRunner = conf.AsJobRunner
4750

4851
entries := parseProcfile(conf.Procfile, conf.PortBase, conf.PortStep)
4952
h.procs = make([]*process, 0)
@@ -77,6 +80,12 @@ func (h *hivemind) waitForDoneOrInterrupt() {
7780
}
7881
}
7982

83+
func (h *hivemind) waitForInterrupt() {
84+
select {
85+
case <-h.interrupted:
86+
}
87+
}
88+
8089
func (h *hivemind) waitForTimeoutOrInterrupt() {
8190
select {
8291
case <-time.After(h.timeout):
@@ -85,7 +94,11 @@ func (h *hivemind) waitForTimeoutOrInterrupt() {
8594
}
8695

8796
func (h *hivemind) waitForExit() {
88-
h.waitForDoneOrInterrupt()
97+
if h.jobRunner {
98+
h.waitForInterrupt()
99+
} else {
100+
h.waitForDoneOrInterrupt()
101+
}
89102

90103
for _, proc := range h.procs {
91104
go proc.Interrupt()

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func main() {
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},
4141
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},
42+
cli.BoolFlag{Name: "as-job-runner", EnvVar: "HIVEMIND_AS_JOB_RUNNER", Usage: "be a job runner instead: Will wait for all to finish with exit code 0, or fail.", Destination: &conf.AsJobRunner},
4243
}
4344

4445
app.Action = func(c *cli.Context) error {

0 commit comments

Comments
 (0)