Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple improvements to "plz watch" and "plz run". #600

Merged
merged 1 commit into from
Apr 20, 2019
Merged
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
28 changes: 20 additions & 8 deletions src/run/run_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package run

import (
"bytes"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -107,15 +108,26 @@ func run(ctx context.Context, state *core.BuildState, label core.BuildLabel, arg
// Note that we don't connect stdin. It doesn't make sense for multiple processes.
cmd := exec.CommandContext(ctx, splitCmd[0], args[1:]...) // args here don't include argv[0]
cmd.Env = env
if !quiet {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
must(cmd.Start(), args)
err := cmd.Wait()
return toExitError(err, cmd, nil)

var combinedOutput bytes.Buffer // Dump the command output here, for quiet mode.
if quiet {
cmd.Stdout, cmd.Stderr = &combinedOutput, &combinedOutput
} else {
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
}
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
must(cmd.Start(), args)

if pgid, err := syscall.Getpgid(cmd.Process.Pid); err == nil && pgid > 0 {
go func() {
for range ctx.Done() {
}
syscall.Kill(-pgid, syscall.SIGKILL)
}()
}
out, err := cmd.CombinedOutput()
return toExitError(err, cmd, out)

err := cmd.Wait()
return toExitError(err, cmd, combinedOutput.Bytes())
}

// environ returns an appropriate environment for a command.
Expand Down
19 changes: 17 additions & 2 deletions src/watch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package watch
import (
"context"
"fmt"
"os"
"os/signal"
"path"
"syscall"
"time"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -39,7 +42,19 @@ func Watch(state *core.BuildState, labels core.BuildLabels, callback CallbackFun
files := cmap.New()
go startWatching(watcher, state, labels, files)

ctx, cancel := context.WithCancel(context.Background())
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
parentCtx, cancelParent := context.WithCancel(context.Background())
go func() {
for range sigchan {
cancelParent()
signal.Stop(sigchan)
close(sigchan)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}
}()

ctx, cancel := context.WithCancel(parentCtx)

// The initial setup only builds targets, it doesn't test or run things.
// Do one of those now if requested.
Expand All @@ -57,7 +72,7 @@ func Watch(state *core.BuildState, labels core.BuildLabels, callback CallbackFun
}
// Kill any previous process.
cancel()
ctx, cancel = context.WithCancel(context.Background())
ctx, cancel = context.WithCancel(parentCtx)

// Quick debounce; poll and discard all events for the next brief period.
outer:
Expand Down