Skip to content

Commit f539e78

Browse files
authored
fix consecutive file update bug (#7)
1 parent 732b348 commit f539e78

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

run.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package watcher
77
import (
88
"log"
99
"os/exec"
10-
"sync"
1110

1211
"github.com/fatih/color"
1312
)
@@ -18,16 +17,13 @@ type Runner struct {
1817
start chan string
1918
done chan struct{}
2019
cmd *exec.Cmd
21-
22-
mu *sync.Mutex
2320
}
2421

2522
// NewRunner creates a new Runner instance and returns its pointer
2623
func NewRunner() *Runner {
2724
return &Runner{
2825
start: make(chan string),
2926
done: make(chan struct{}),
30-
mu: &sync.Mutex{},
3127
}
3228
}
3329

@@ -40,20 +36,18 @@ func (r *Runner) Run(p *Params) {
4036
cmd, err := runCommand(fileName, p.Package...)
4137
if err != nil {
4238
log.Printf("Could not run the go binary: %s \n", err)
43-
r.kill()
39+
r.kill(cmd)
4440

4541
continue
4642
}
4743

48-
r.mu.Lock()
4944
r.cmd = cmd
5045
removeFile(fileName)
51-
r.mu.Unlock()
5246

5347
go func(cmd *exec.Cmd) {
5448
if err := cmd.Wait(); err != nil {
5549
log.Printf("process interrupted: %s \n", err)
56-
r.kill()
50+
r.kill(cmd)
5751
}
5852
}(r.cmd)
5953
}
@@ -62,25 +56,20 @@ func (r *Runner) Run(p *Params) {
6256
// Restart kills the process, removes the old binary and
6357
// restarts the new process
6458
func (r *Runner) restart(fileName string) {
65-
r.kill()
59+
r.kill(r.cmd)
6660

6761
r.start <- fileName
6862
}
6963

70-
func (r *Runner) kill() {
71-
r.mu.Lock()
72-
defer r.mu.Unlock()
73-
if r.cmd != nil {
74-
pid := r.cmd.Process.Pid
75-
log.Printf("Killing PID %d \n", pid)
76-
r.cmd.Process.Kill()
77-
r.cmd = nil
64+
func (r *Runner) kill(cmd *exec.Cmd) {
65+
if cmd != nil {
66+
cmd.Process.Kill()
7867
}
7968
}
8069

8170
func (r *Runner) Close() {
8271
close(r.start)
83-
r.kill()
72+
r.kill(r.cmd)
8473
close(r.done)
8574
}
8675

0 commit comments

Comments
 (0)