From 6f283cb2fd13492430b692fcd72cb4ed44791d15 Mon Sep 17 00:00:00 2001 From: Vincent Composieux Date: Mon, 11 Nov 2019 14:33:40 +0100 Subject: [PATCH] Fixed applications to be killed and relaunched correctly by file watcher --- cmd/main.go | 7 +++---- pkg/runner/runner.go | 6 ++++-- pkg/watcher/watcher.go | 8 ++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index a57ea7f7..7d2fac59 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -5,7 +5,6 @@ import ( "os" "os/signal" "strconv" - "syscall" "github.com/eko/monday/internal/runtime" "github.com/eko/monday/pkg/config" @@ -113,7 +112,7 @@ func run(conf *config.Config, choice string) { forwarderComponent = forwarder.NewForwarder(layout.GetForwardsView(), proxyComponent, project) watcherComponent = watcher.NewWatcher(runnerComponent, forwarderComponent, conf.Watcher, project) - watcherComponent.Watch() + go watcherComponent.Watch() if uiEnabled { defer layout.GetGui().Close() @@ -134,7 +133,7 @@ func run(conf *config.Config, choice string) { // Handle for an exit signal in order to quit application on a proper way (shutting down connections and servers). func handleExitSignal() { stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGTERM) + signal.Notify(stop, os.Interrupt, os.Kill) <-stop @@ -144,10 +143,10 @@ func handleExitSignal() { func stopAll() { fmt.Println("\nšŸ‘‹ Bye, closing your local applications and remote connections now") + watcherComponent.Stop() forwarderComponent.Stop() proxyComponent.Stop() runnerComponent.Stop() - watcherComponent.Stop() os.Exit(0) } diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index 96ce7fd0..7032013b 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -111,7 +111,8 @@ func (r *Runner) Restart(application *config.Application) { if cmd, ok := r.cmds[application.Name]; ok { pgid, err := syscall.Getpgid(cmd.Process.Pid) if err == nil { - syscall.Kill(-pgid, 15) + syscall.Kill(-pgid, syscall.SIGKILL) + cmd.Wait() } } @@ -125,7 +126,8 @@ func (r *Runner) Stop() error { if cmd, ok := r.cmds[application.Name]; ok { pgid, err := syscall.Getpgid(cmd.Process.Pid) if err == nil { - syscall.Kill(-pgid, 15) + syscall.Kill(-pgid, syscall.SIGKILL) + cmd.Wait() } } diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go index ffab9202..08f0b9b5 100644 --- a/pkg/watcher/watcher.go +++ b/pkg/watcher/watcher.go @@ -48,9 +48,9 @@ func NewWatcher(runner runner.RunnerInterface, forwarder forwarder.ForwarderInte // It also relaunch them in case of file changes. func (w *Watcher) Watch() { w.runner.SetupAll() - w.runner.RunAll() - w.forwarder.ForwardAll() + go w.runner.RunAll() + go w.forwarder.ForwardAll() for _, application := range w.project.Applications { if !application.Watch { @@ -100,10 +100,10 @@ func (w *Watcher) watchApplication(application *config.Application) error { for { select { case event := <-fileWatcher.Event: - fmt.Printf("šŸ‘“ Watcher has detected a file change: %v", event) + fmt.Printf("šŸ‘“ Watcher has detected a file change: %v\n", event) w.runner.Restart(application) case err := <-fileWatcher.Error: - fmt.Printf("āŒ An error has occured while file watching: %v", err) + fmt.Printf("āŒ An error has occured while file watching: %v\n", err) } } }()