-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogram.go
286 lines (238 loc) · 7.12 KB
/
program.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package server
import (
"fmt"
"io"
"os"
"os/exec"
)
// program wraps the default *exec.Cmd structure and makes easier the
// access to redirect the standard output and check when it terminates.
// Another limitation is that graceful shutdown is not implemented yet
// due to Windows limitations, but will be. It's possible to wait for its
// termination on multiple goroutines by waiting for exitC closure. Both
// in and out can be nil
type program struct {
name string
dir string
execName string
args []string
exitC chan struct{}
exec *exec.Cmd
lastProcessState *os.ProcessState
running bool
in io.Reader
out io.Writer
}
// newProgram creates a new program with the diven parameters
func newProgram(name, dir string, in io.Reader, out io.Writer, execName string, args ...string) (*program, error) {
info, err := os.Stat(dir)
if err != nil {
return nil, fmt.Errorf("directory \"%s\" not found", dir)
}
if !info.IsDir() {
return nil, fmt.Errorf("\"%s\" is not a directory", dir)
}
return &program {
name: name,
dir: dir,
execName: execName,
args: args,
exitC: make(chan struct{}),
in: in,
out: out,
}, nil
}
// start starts the program and with a goroutine waits for its
// termination. It returns an error if there is a problem with
// the creation of the new process, but if something happens during
// the execution it will be reported to che channel returned
func (p *program) start() (<-chan error, error) {
if p.isRunning() {
return nil, fmt.Errorf("program \"%s\" is already running", p.name)
}
p.exec = exec.Command(p.execName, p.args...)
if p.dir != "" {
p.exec.Dir = p.dir
}
p.exec.Stdin = p.in
p.exec.Stdout = p.out
p.exec.Stderr = p.out
err := p.exec.Start()
if err != nil {
return nil, fmt.Errorf("program \"%s\" startup error: %w", p.name, err)
}
p.running = true
errChan := make(chan error, 1)
go p.afterStart(errChan)
return errChan, nil
}
// afterStart waits for the process with the already provided function by *os.Process,
// then closes the exitC channel to segnal its termination
func (p *program) afterStart(errChan chan error) {
err := p.exec.Wait()
if err != nil {
errChan <- fmt.Errorf("program \"%s\" waiting error: %w", p.name, err)
}
p.lastProcessState = p.exec.ProcessState
p.running = false
close(errChan)
close(p.exitC)
}
// wait waits for the process termination (if running) and returns the last process
// state known
func (p *program) wait() *os.ProcessState {
for range p.exitC {
}
return p.lastProcessState
}
// stop gracefully stops the process (not implemented, now just kills)
// and waits for the cleanup
func (p *program) stop() error {
return p.kill()
}
// kill forcibly kills the process and waits for the cleanup
func (p *program) kill() error {
if !p.isRunning() {
return fmt.Errorf("program \"%s\" is already stopped", p.name)
}
err := p.exec.Process.Kill()
if err != nil {
return fmt.Errorf("program \"%s\" stop error: %w", p.name, err)
}
for range p.exitC {
}
return nil
}
// isRunning reports whether the program is still running
func (p *program) isRunning() bool {
return p.running
}
func (p *program) String() string {
var state string
if p.isRunning() {
state = fmt.Sprintf("Running (%d)", p.exec.Process.Pid)
} else {
state = "Stopped"
}
return fmt.Sprintf("%s (%s)", p.name, state)
}
// NewProgram creates a new program with the given parameters.
// The program name must be a unique one and both in and out can
// be nil. Graceful shut down is not implemented yet due to Windows
// limitations, but will be (not it just calls the Kill method).
// It's possible to wait for its termination on multiple goroutines
// by calling the Wait method.
func (tm *TaskManager) NewProgram(name, dir string, in io.Reader, out io.Writer, execName string, args ...string) error {
if !tm.checkProgramName(name) {
return fmt.Errorf("program named \"%s\" already registered", name)
}
p, err := newProgram(name, dir, in, out, execName, args...)
if err != nil {
return fmt.Errorf("error creating program \"%s\": %w", name, err)
}
tm.programs[name] = p
return nil
}
// findProgram finds if a program with the given name is registered in the programs map
func (tm *TaskManager) findProgram(name string) (*program, error) {
p, ok := tm.programs[name]
if !ok {
return nil, fmt.Errorf("program \"%s\" not found", name)
}
return p, nil
}
// StartProgram starts an already registered program if it's not running.
// This method just waits for the successful start-up of the program, but
// It does not wait for the termination. For this, call the Wait method
func (tm *TaskManager) StartProgram(name string) error {
p, err := tm.findProgram(name)
if err != nil {
return err
}
errChan, err := p.start()
if err != nil {
return err
}
go func() {
for err := range errChan {
tm.Router.Log(LOG_LEVEL_ERROR, err.Error())
}
}()
return nil
}
// StopProgram gracefully stops the program with the given name
// (not implemented now, just kills the program)
func (tm *TaskManager) StopProgram(name string) error {
p, err := tm.findProgram(name)
if err != nil {
return err
}
return p.stop()
}
// KillProgram forcibly kills the program with the given name
func (tm *TaskManager) KillProgram(name string) error {
p, err := tm.findProgram(name)
if err != nil {
return err
}
return p.kill()
}
// RestartProgram first gracefully stops the program (not implemented,
// see StopProgram method) and then starts it again
func (tm *TaskManager) RestartProgram(name string) error {
_, err := tm.findProgram(name)
if err != nil {
return err
}
err = tm.StopProgram(name)
if err != nil {
return err
}
return tm.StartProgram(name)
}
// WaitProgram waits for the termination of the program and returns
// process information
func (tm *TaskManager) WaitProgram(name string) (*os.ProcessState, error) {
p, err := tm.findProgram(name)
if err != nil {
return nil, err
}
return p.wait(), nil
}
// ProgramIsRunning tells if the program is running or not
func (tm *TaskManager) ProgramIsRunning(name string) (bool, error) {
p, err := tm.findProgram(name)
if err != nil {
return false, err
}
return p.isRunning(), nil
}
// GetProgramsNames returns a slice containing all the names
// of the registered programs
func (tm *TaskManager) GetProgramsNames() []string {
names := make([]string, 0, len(tm.programs))
for name := range tm.programs {
names = append(names, name)
}
return names
}
// StopAllPrograms stops all the running programs registered in the
// TaskManager. In case of errors, they will be logged automatically
// with the Router
func (tm *TaskManager) StopAllPrograms() {
for _, p := range tm.programs {
if !p.isRunning() {
continue
}
if err := p.stop(); err != nil {
tm.Router.Log(LOG_LEVEL_ERROR, err.Error())
}
}
}
// checkProgramName checks if a new program can be created with the giver name. If there is an
// already registered program with the same name, it returns false, otherwise
// it returns true
func (tm *TaskManager) checkProgramName(name string) bool {
_, exists := tm.programs[name]
return !exists
}