Skip to content

Commit 1e895ef

Browse files
authored
Use close(e.binStopCh) to boardcast event which is stop running bins (#255)
* Use close(e.binStopCh) to boardcast event which is stop running bins
1 parent 673a832 commit 1e895ef

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

runner/engine.go

+13-19
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Engine struct {
2424
buildRunCh chan bool
2525
buildRunStopCh chan bool
2626
canExit chan bool
27-
binStopChMap map[int]chan bool
27+
binStopCh chan bool
2828
exitCh chan bool
2929

3030
mu sync.RWMutex
@@ -57,7 +57,7 @@ func NewEngine(cfgPath string, debugMode bool) (*Engine, error) {
5757
buildRunCh: make(chan bool, 1),
5858
buildRunStopCh: make(chan bool, 1),
5959
canExit: make(chan bool, 1),
60-
binStopChMap: make(map[int]chan bool),
60+
binStopCh: make(chan bool),
6161
exitCh: make(chan bool),
6262
watchers: 0,
6363
}
@@ -94,7 +94,7 @@ func (e *Engine) checkRunEnv() error {
9494
p := e.config.tmpPath()
9595
if _, err := os.Stat(p); os.IsNotExist(err) {
9696
e.runnerLog("mkdir %s", p)
97-
if err := os.Mkdir(p, 0755); err != nil {
97+
if err := os.Mkdir(p, 0o755); err != nil {
9898
e.runnerLog("failed to mkdir, error: %s", err.Error())
9999
return err
100100
}
@@ -340,10 +340,8 @@ func (e *Engine) start() {
340340

341341
// if current app is running, stop it
342342
e.withLock(func() {
343-
for _, v := range e.binStopChMap {
344-
v <- true
345-
}
346-
e.binStopChMap = make(map[int]chan bool)
343+
close(e.binStopCh)
344+
e.binStopCh = make(chan bool)
347345
})
348346
go e.buildRun()
349347
}
@@ -424,15 +422,16 @@ func (e *Engine) runBin() error {
424422
return err
425423
}
426424

427-
killFunc := func(cmd *exec.Cmd, stdin io.WriteCloser, stdout io.ReadCloser, stderr io.ReadCloser, binStopChan chan bool) {
425+
killFunc := func(cmd *exec.Cmd, stdin io.WriteCloser, stdout io.ReadCloser, stderr io.ReadCloser) {
428426
defer func() {
429427
select {
430428
case <-e.exitCh:
431429
close(e.canExit)
432430
default:
433431
}
434432
}()
435-
<-binStopChan
433+
// when invoke close() it will return
434+
<-e.binStopCh
436435
e.mainDebug("trying to kill pid %d, cmd %+v", cmd.Process.Pid, cmd.Args)
437436
defer func() {
438437
stdout.Close()
@@ -457,12 +456,9 @@ func (e *Engine) runBin() error {
457456
}
458457
}
459458
e.withLock(func() {
460-
for _, v := range e.binStopChMap {
461-
v <- true
462-
}
463-
e.binStopChMap = make(map[int]chan bool)
464-
e.binStopChMap[cmd.Process.Pid] = make(chan bool)
465-
go killFunc(cmd, stdin, stdout, stderr, e.binStopChMap[cmd.Process.Pid])
459+
close(e.binStopCh)
460+
e.binStopCh = make(chan bool)
461+
go killFunc(cmd, stdin, stdout, stderr)
466462
})
467463
e.mainDebug("running process pid %v", cmd.Process.Pid)
468464
return nil
@@ -473,10 +469,8 @@ func (e *Engine) cleanup() {
473469
defer e.mainLog("see you again~")
474470

475471
e.withLock(func() {
476-
for _, v := range e.binStopChMap {
477-
v <- true
478-
}
479-
e.binStopChMap = make(map[int]chan bool)
472+
close(e.binStopCh)
473+
e.binStopCh = make(chan bool)
480474
})
481475
e.mainDebug("wating for close watchers..")
482476

0 commit comments

Comments
 (0)