-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_endpoint.go
163 lines (141 loc) · 3.86 KB
/
process_endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2013 Joe Walnes and the websocketd team.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"io"
"log"
"os/exec"
"syscall"
)
type LaunchedProcess struct {
cmd *exec.Cmd
stdin io.WriteCloser
stdout io.ReadCloser
stderr io.ReadCloser
}
func launchCmd(commandName string, commandArgs []string, env []string) (*LaunchedProcess, error) {
cmd := exec.Command(commandName, commandArgs...)
cmd.Env = env
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
err = cmd.Start()
if err != nil {
return nil, err
}
return &LaunchedProcess{cmd, stdin, stdout, stderr}, err
}
func NewProcessEndpoint(command string) *ProcessEndpoint {
process, _ := launchCmd(command, []string{}, []string{})
return &ProcessEndpoint{
process: process,
bufferedIn: bufio.NewWriter(process.stdin),
output: make(chan string),
input: make(chan string),
err: make(chan bool),
}
}
type ProcessEndpoint struct {
process *LaunchedProcess
bufferedIn *bufio.Writer
output chan string
input chan string
err chan bool
//log *LogScope
}
func (pe *ProcessEndpoint) Output() chan string { return pe.output }
func (pe *ProcessEndpoint) Input() chan string { return pe.input }
func (pe *ProcessEndpoint) Err() chan bool { return pe.err }
func (pe *ProcessEndpoint) Terminate() {
pe.process.stdin.Close()
err := pe.process.cmd.Process.Signal(syscall.SIGINT)
if err != nil {
//pe.log.Debug("process", "Failed to Interrupt process %v: %s, attempting to kill", pe.process.cmd.Process.Pid, err)
err = pe.process.cmd.Process.Kill()
if err != nil {
log.Printf("Failed to Kill process %v: %s\n", pe.process.cmd.Process.Pid, err)
//pe.log.Debug("process", "Failed to Kill process %v: %s", pe.process.cmd.Process.Pid, err)
}
}
pe.process.cmd.Wait()
if err != nil {
log.Printf("Failed to reap process %v: %s\n", pe.process.cmd.Process.Pid, err)
//pe.log.Debug("process", "Failed to reap process %v: %s", pe.process.cmd.Process.Pid, err)
}
}
func (pe *ProcessEndpoint) Start() {
go pe.log_stderr()
go pe.process_stdout()
go pe.process_stdin()
}
func (pe *ProcessEndpoint) process_stdin() {
for msg := range pe.input {
pe.bufferedIn.WriteString(msg)
pe.bufferedIn.WriteString("\n")
pe.bufferedIn.Flush()
}
close(pe.input)
}
func (pe *ProcessEndpoint) process_stdout() {
bufin := bufio.NewReader(pe.process.stdout)
for {
str, err := bufin.ReadString('\n')
if err != nil {
if err != io.EOF {
log.Println("process: Unexpected error while reading STDOUT from process: %s", err)
panic(err)
} else {
log.Println("process: Process STDOUT closed")
pe.Terminate()
pe.err <- true
//panic(err)
}
break
}
pe.output <- trimEOL(str)
}
close(pe.output)
}
func (pe *ProcessEndpoint) log_stderr() {
bufstderr := bufio.NewReader(pe.process.stderr)
for {
//str, err := bufstderr.ReadString('\n')
str, err := bufstderr.ReadString('\n')
log.Println("stderr")
log.Println(str)
if err != nil {
if err != io.EOF {
panic(err)
//pe.log.Error("process", "Unexpected error while reading STDERR from process: %s", err)
} else {
pe.err <- true
//pe.log.Debug("process", "Process STDERR closed")
}
break
}
//pe.log.Error("stderr", "%s", trimEOL(str))
}
}
// trimEOL cuts unixy style \n and windowsy style \r\n suffix from the string
func trimEOL(s string) string {
lns := len(s)
if lns > 0 && s[lns-1] == '\n' {
lns--
if lns > 0 && s[lns-1] == '\r' {
lns--
}
}
return s[0:lns]
}