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

fix: killing descendants #2

Merged
merged 8 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 3 additions & 4 deletions examples/hotreload/api/.pw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ watchers:
timeout: 3s
cmd:
shell: /bin/sh -c
exec: dlv debug --headless -l :2345 --api-version=2 --accept-multiclient --log --continue ./cmd/api
env:
- GO111MODULE=off

exec: go mod tidy; dlv debug --headless -l :2345 --api-version=2 --accept-multiclient --log --continue ./cmd/api
pouyanh marked this conversation as resolved.
Show resolved Hide resolved
env:
- LOG_LEVEL=DEBUG
10 changes: 4 additions & 6 deletions examples/hotreload/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ services:
context: context/go
image: registry.ronaksoft.com/library/golang:dev
entrypoint:
- go
command:
- run
- /go/src/github.com/pouyanh/polywatch/cmd/polywatch/main.go
- /bin/sh
command: >
pouyanh marked this conversation as resolved.
Show resolved Hide resolved
-c "(cd /go/src/github.com/pouyanh/polywatch/cmd/polywatch; go mod tidy; go build -o /bin/polywatch); /bin/polywatch"
healthcheck:
test:
- "CMD"
Expand All @@ -31,12 +30,11 @@ services:
working_dir: /go/src/github.com/pouyanh/polywatch/examples/hotreload/api
volumes:
- gosrc:/go/src
# - gomod:/go/pkg/mod
- gomod:/go/pkg/mod
- /etc/localtime:/etc/localtime:ro
environment:
HOSTNAME: api.hotreload.plw

GO111MODULE: off
GOPRIVATE: github.com/pouyanh*,github.com/janstoon*

volumes:
Expand Down
29 changes: 24 additions & 5 deletions polywatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"log"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"

"github.com/radovskyb/watcher"
"github.com/zmwangx/debounce"
Expand Down Expand Up @@ -42,6 +44,15 @@ func Start() error {
}()
}

cchan := make(chan os.Signal)
pouyanh marked this conversation as resolved.
Show resolved Hide resolved
signal.Notify(cchan, syscall.SIGTERM, syscall.SIGINT)

go func() {
<-cchan

stop()
}()

wg.Wait()

return nil
Expand Down Expand Up @@ -112,6 +123,7 @@ func (pw *polyWatcher) renewCommand() {

cmd := exec.Command(rawcmd[0], rawcmd[1:]...)
cmd.Env = pw.cfg.Command.Env
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Expand Down Expand Up @@ -229,13 +241,20 @@ func (pw *polyWatcher) kill(ctx context.Context) error {
pw.lg.Printf("killing previous command pid(%d) with sig(%s)", pw.cmd.Process.Pid, pw.cfg.Kill.Signal)

// todo: apply kill timeout

err := pw.cmd.Process.Signal(pw.cfg.Kill.Signal)
if err != nil {
pw.lg.Printf("unable to send signal to previous command: %s", err)
group, err := os.FindProcess(-1 * pw.cmd.Process.Pid)
if err == nil {
err = group.Signal(pw.cfg.Kill.Signal)
if err != nil {
pw.lg.Printf("unable to send signal to previous command: %s", err)
}
}

defer pw.renewCommand()

return pw.cmd.Wait()
err = pw.cmd.Wait()
pouyanh marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

return nil
}
4 changes: 2 additions & 2 deletions pw.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ watchers:
timeout: 3s
cmd:
shell: /bin/sh -c
exec: dlv debug --headless -l :2345 --api-version=2 --accept-multiclient --log --continue ./cmd/api
exec: go mod tidy; dlv debug --headless -l :2345 --api-version=2 --accept-multiclient --log --continue ./cmd/api
pouyanh marked this conversation as resolved.
Show resolved Hide resolved
env:
- GO111MODULE=off
- LOG_LEVEL=DEBUG