Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions hivemind.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ func newHivemind(conf hivemindConfig) (h *hivemind) {
return
}

func (h *hivemind) runProcess(proc *process) {
func (h *hivemind) runProcess(proc *process, exitCode chan int) {
h.procWg.Add(1)

go func() {
defer h.procWg.Done()
defer func() { h.done <- true }()

proc.Run()
exitCode <- proc.Run()
}()
}

Expand Down Expand Up @@ -97,19 +97,22 @@ func (h *hivemind) waitForExit() {
}
}

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

h.done = make(chan bool, len(h.procs))

h.interrupted = make(chan os.Signal)
signal.Notify(h.interrupted, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)

exitCodes := make(chan int, len(h.procs))
for _, proc := range h.procs {
h.runProcess(proc)
h.runProcess(proc, exitCodes)
}

go h.waitForExit()

h.procWg.Wait()

// note: return first exited process exit code
return <-exitCodes
}
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ func main() {
conf.Root, err = filepath.Abs(conf.Root)
fatalOnErr(err)

newHivemind(conf).Run()
exitCode := newHivemind(conf).Run()
if exitCode > 0 {
os.Exit(exitCode)
}

return nil
}
Expand Down
3 changes: 2 additions & 1 deletion process.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (p *process) Running() bool {
return p.Process != nil && p.ProcessState == nil
}

func (p *process) Run() {
func (p *process) Run() int {
p.output.PipeOutput(p)
defer p.output.ClosePipe(p)

Expand All @@ -69,6 +69,7 @@ func (p *process) Run() {
} else {
p.writeLine([]byte("\033[1mProcess exited\033[0m"))
}
return p.Cmd.ProcessState.ExitCode()
}

func (p *process) Interrupt() {
Expand Down