diff options
author | Alex Auvolat <alex@adnab.me> | 2020-03-02 21:51:13 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-03-02 21:51:13 +0100 |
commit | df1f2d38b3e5d31827bf1d07fa63d5eaed6bc682 (patch) | |
tree | dc03856fd2294724ad8c298744a27a1679b30fd7 /connector/external | |
parent | 7e0dbc5824a634875655be86098f95e8e3122d47 (diff) | |
download | easybridge-df1f2d38b3e5d31827bf1d07fa63d5eaed6bc682.tar.gz easybridge-df1f2d38b3e5d31827bf1d07fa63d5eaed6bc682.zip |
Handle shutdown better
Diffstat (limited to 'connector/external')
-rw-r--r-- | connector/external/external.go | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/connector/external/external.go b/connector/external/external.go index 6117880..b66ab6c 100644 --- a/connector/external/external.go +++ b/connector/external/external.go @@ -79,8 +79,8 @@ type External struct { config Configuration - recv io.Reader - send io.Writer + recvPipe io.ReadCloser + sendPipe io.WriteCloser sendJson *json.Encoder generation int @@ -114,7 +114,7 @@ func (ext *External) Configure(c Configuration) error { ext.handlerChan = make(chan *extMessageWithData, 1000) go ext.handlerLoop(ext.generation) - err = ext.setupProc() + err = ext.setupProc(ext.generation) if err != nil { return err } @@ -133,26 +133,28 @@ func (ext *External) Configure(c Configuration) error { // ---- Process management and communication logic -func (ext *External) setupProc() error { +func (ext *External) setupProc(generation int) error { var err error ext.proc = exec.Command(ext.command) - ext.recv, err = ext.proc.StdoutPipe() + ext.recvPipe, err = ext.proc.StdoutPipe() if err != nil { return err } - ext.send, err = ext.proc.StdinPipe() + ext.sendPipe, err = ext.proc.StdinPipe() if err != nil { return err } + send := io.Writer(ext.sendPipe) + recv := io.Reader(ext.recvPipe) if ext.debug { - ext.recv = io.TeeReader(ext.recv, os.Stderr) - ext.send = io.MultiWriter(ext.send, os.Stderr) + recv = io.TeeReader(recv, os.Stderr) + send = io.MultiWriter(send, os.Stderr) } - ext.sendJson = json.NewEncoder(ext.send) + ext.sendJson = json.NewEncoder(send) ext.proc.Stderr = os.Stderr @@ -161,7 +163,7 @@ func (ext *External) setupProc() error { return err } - go ext.recvLoop() + go ext.recvLoop(recv, generation) return nil } @@ -175,7 +177,7 @@ func (ext *External) restartLoop(generation int) { break } log.Printf("Process %s stopped, restarting.", ext.command) - err := ext.setupProc() + err := ext.setupProc(generation) if err != nil { ext.proc = nil log.Warnf("Unable to restart %s: %s", ext.command, err) @@ -232,8 +234,8 @@ func (m *extMessageWithData) UnmarshalJSON(jj []byte) error { } -func (ext *External) recvLoop() { - scanner := bufio.NewScanner(ext.recv) +func (ext *External) recvLoop(from io.Reader, generation int) { + scanner := bufio.NewScanner(from) for scanner.Scan() { var msg extMessageWithData err := json.Unmarshal(scanner.Bytes(), &msg) @@ -260,6 +262,10 @@ func (ext *External) recvLoop() { } else { ext.handlerChan <- &msg } + + if ext.generation != generation { + break + } } } @@ -315,20 +321,25 @@ func (ext *External) cmd(msg extMessage, data interface{}) (*extMessageWithData, } func (ext *External) Close() { + ext.generation += 1 + ext.sendJson.Encode(&extMessage{ MsgType: CLOSE, }) - ext.generation += 1 proc := ext.proc + proc.Process.Signal(os.Interrupt) + ext.recvPipe.Close() + ext.sendPipe.Close() + ext.proc = nil - ext.recv = nil - ext.send = nil + ext.recvPipe = nil + ext.sendPipe = nil ext.sendJson = nil ext.handlerChan = nil go func() { - time.Sleep(10 * time.Second) + time.Sleep(1 * time.Second) proc.Process.Kill() }() } |