Skip to content

Commit

Permalink
Add --as-job-runner: With this flag hivemind will let processes exi…
Browse files Browse the repository at this point in the history
…t 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
```
  • Loading branch information
danieroux committed Oct 18, 2024
1 parent 6ee18ba commit a613070
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 14 additions & 1 deletion hivemind.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type hivemindConfig struct {
NoPrefix bool
PrintTimestamps bool
ExitWithHighest bool
AsJobRunner bool
}

type hivemind struct {
Expand All @@ -32,6 +33,7 @@ type hivemind struct {
done chan bool
interrupted chan os.Signal
timeout time.Duration
jobRunner bool
}

func newHivemind(conf hivemindConfig) (h *hivemind) {
Expand All @@ -44,6 +46,7 @@ func newHivemind(conf hivemindConfig) (h *hivemind) {
}

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

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

func (h *hivemind) waitForInterrupt() {
select {
case <-h.interrupted:
}
}

func (h *hivemind) waitForTimeoutOrInterrupt() {
select {
case <-time.After(h.timeout):
Expand All @@ -85,7 +94,11 @@ func (h *hivemind) waitForTimeoutOrInterrupt() {
}

func (h *hivemind) waitForExit() {
h.waitForDoneOrInterrupt()
if h.jobRunner {
h.waitForInterrupt()
} else {
h.waitForDoneOrInterrupt()
}

for _, proc := range h.procs {
go proc.Interrupt()
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {
cli.BoolFlag{Name: "no-prefix", EnvVar: "HIVEMIND_NO_PREFIX", Usage: "process names will not be printed if the flag is specified", Destination: &conf.NoPrefix},
cli.BoolFlag{Name: "print-timestamps, T", EnvVar: "HIVEMIND_PRINT_TIMESTAMPS", Usage: "timestamps will be printed if the flag is specified", Destination: &conf.PrintTimestamps},
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},
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},
}

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

0 comments on commit a613070

Please sign in to comment.