Skip to content

Commit

Permalink
Fixed applications to be killed and relaunched correctly by file watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
eko committed Nov 11, 2019
1 parent 36a16e1 commit 6f283cb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
7 changes: 3 additions & 4 deletions cmd/main.go
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"os/signal"
"strconv"
"syscall"

"github.com/eko/monday/internal/runtime"
"github.com/eko/monday/pkg/config"
Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand All @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/runner/runner.go
Expand Up @@ -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()
}
}

Expand All @@ -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()
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/watcher/watcher.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}()
Expand Down

0 comments on commit 6f283cb

Please sign in to comment.