-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
376 lines (343 loc) · 9.1 KB
/
worker.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package corerunner
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"log"
"os/exec"
"strconv"
"strings"
"sync"
"time"
)
const (
// Размер пакета данных (в байтах) для общения между Go и запускаемым
// процессом. Чем больше значение, тем меньше будет итераций при
// передачи большого количества данных, но есть возможость упереться в
// ограничения транспорта.
// https://unix.stackexchange.com/questions/11946/how-big-is-the-pipe-buffer
PipeChunkSize = 2048
)
var (
ErrWorkerTimedOut = errors.New("worker timed out")
)
type Pool struct {
pool []*Worker
queue chan WorkerJob
mu sync.Mutex
}
// Start запускает n воркеров, указанных в argv с переменными окружения env.
// Повторный запуск возможен только после выполнения Stop.
func (p *Pool) Start(argv []string, n int, env []string) error {
if len(p.pool) != 0 {
return errors.New("already started")
}
p.queue = make(chan WorkerJob, n*512)
var wg sync.WaitGroup
wg.Add(n)
var werr error
for i := 0; i < n; i++ {
go func() {
defer wg.Done()
wrk := NewWorker(p.queue)
start := time.Now()
err := wrk.Start(argv, env)
if err != nil {
werr = err
return
}
p.mu.Lock()
p.pool = append(p.pool, wrk)
p.mu.Unlock()
log.Printf(
"PID %d: worker started in %s",
wrk.cmd.Process.Pid,
time.Since(start),
)
}()
}
wg.Wait()
return werr
}
// Send добавляет данные data в очередь на обработку воркером. Возвращает
// канал, из которого можно получить ответ от воркера. Если ответ не получен
// через timeout, воркер перезапускается и возвращается ошибка.
func (p *Pool) Send(data []byte, timeout time.Duration) chan WorkerResult {
res := make(chan WorkerResult)
p.queue <- WorkerJob{data: data, res: res, timeout: timeout}
return res
}
// Stop останавливает все запущенные процессы и очищает пул.
func (p *Pool) Stop() {
close(p.queue)
for _, wrk := range p.pool {
wrk.Stop()
}
p.pool = []*Worker{}
}
type Worker struct {
cmd *exec.Cmd
read *bufio.Reader
write *bufio.Writer
mu sync.Mutex
argv []string
env []string
queue chan WorkerJob
}
// Задача на обработку для запущенного процесса.
type WorkerJob struct {
data []byte
timeout time.Duration
res chan WorkerResult
}
// Результат выполнения WorkerJob.
type WorkerResult struct {
Res []byte
Err error
}
func NewWorker(queue chan WorkerJob) *Worker {
return &Worker{queue: queue}
}
// Start запускает процесс с указанными аргументами argv. Этот метод не
// дожидается завершения процесса.
// XXX: переделать в блокирующий метод? Будет проще отслеживать завершение
// процесса (сейчас это реализовано отловом EOF в любом из pipe'ов).
// exec.Run(), судя по всему, не дает параллельно читать pipe'ы, как и
// последовательный запуск exec.Start() и exec.Wait().
func (wrk *Worker) Start(argv []string, env []string) error {
if wrk.cmd != nil {
return errors.New("already started")
}
wrk.argv = argv
wrk.env = env
var cmd *exec.Cmd
if len(argv) == 1 {
cmd = exec.Command(argv[0])
} else {
cmd = exec.Command(argv[0], argv[1:]...)
}
cmd.Env = env
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
wrk.read = bufio.NewReader(stdout)
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
wrk.write = bufio.NewWriter(stdin)
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
errReader := bufio.NewReader(stderr)
// Параллельно запускаем чтение и вывод ошибок приложения.
go func() {
for {
l, err := errReader.ReadString('\n')
if err != nil {
if err != io.EOF {
log.Println("logging error: ", err)
}
break
}
log.Print(l)
}
}()
if err = cmd.Start(); err != nil {
return err
}
ok, err := wrk.read.ReadString('\n')
if err != nil {
return err
}
if string(ok) != "ok\n" {
msg, _ := io.ReadAll(wrk.read)
return errors.New(string(msg))
}
wrk.cmd = cmd
// Запуск бесконечного цикла обработки сообщений.
go wrk.jobLoop()
return nil
}
func (wrk *Worker) jobLoop() {
for {
select {
case job, ok := <-wrk.queue:
if ok == false {
return
}
job.res <- *wrk.timedSend(job.data, job.timeout)
}
}
}
// Stop останавливает процесс и закрывает все соответствующие буферы
// чтения/записи.
func (wrk *Worker) Stop() error {
wrk.mu.Lock()
defer wrk.mu.Unlock()
if wrk.cmd == nil {
return errors.New("Worker is not running")
}
var err error
if _, err := wrk.write.Write([]byte("\n")); err != nil {
return err
}
if err = wrk.write.Flush(); err != nil {
return err
}
if err = wrk.cmd.Wait(); err != nil {
return err
}
wrk.reset()
return nil
}
// Wait ждет завершения процесса и закрывает все соответствующие буферы
// чтения/записи.
func (wrk *Worker) Wait() error {
wrk.mu.Lock()
defer wrk.mu.Unlock()
if wrk.cmd == nil {
return errors.New("Worker is not running")
}
if err := wrk.cmd.Wait(); err != nil {
return err
}
wrk.reset()
return nil
}
// != nil Kill посылает SIGKILL процессу и закрывает все соответствующие буферы
// чтения/записи.
func (wrk *Worker) Kill() error {
wrk.mu.Lock()
defer wrk.mu.Unlock()
if wrk.cmd == nil {
return errors.New("Worker is not running")
}
if err := wrk.cmd.Process.Kill(); err != nil {
return err
}
if _, err := wrk.cmd.Process.Wait(); err != nil {
return err
}
wrk.reset()
return nil
}
// send отправляет данные процессу для обработки, дожидается ответа и
// возвращает его.
func (wrk *Worker) send(data []byte) ([]byte, error) {
wrk.mu.Lock()
defer wrk.mu.Unlock()
err := wrk.writeMsg(data)
if err != nil {
log.Println("write error:", err)
return nil, err
}
res, err := wrk.readMsg()
if err != nil {
log.Println("read error:", err)
return nil, err
}
return res, nil
}
// Restart перезапускает воркер. Если kill = true, то процессу посылается
// SIGKILL, иначе ожидается его естественное завершение.
func (wrk *Worker) Restart(kill bool) error {
var err error
if wrk.cmd != nil && wrk.cmd.Process != nil {
if kill {
if err = wrk.cmd.Process.Kill(); err != nil {
return err
}
}
if err = wrk.cmd.Wait(); err != nil {
log.Println("restart wait error:", err)
}
}
argv := wrk.argv
env := wrk.env
wrk.reset()
return wrk.Start(argv, env)
}
func (wrk *Worker) readMsg() ([]byte, error) {
if wrk.read == nil {
return nil, errors.New("read pipe is not started")
}
// Считываем длину сообщения.
l, err := wrk.read.ReadString('\n')
if err != nil {
return nil, err
}
ln, err := strconv.Atoi(strings.TrimSuffix(l, "\n"))
if err != nil {
return nil, err
}
// Читаем ln байт сообщения частями размером PipeChunkSize.
buf := &bytes.Buffer{}
for ln > 0 {
n, err := io.CopyN(buf, wrk.read, int64(min(ln, PipeChunkSize)))
if err != nil {
return nil, err
}
ln -= int(n)
}
return buf.Bytes(), nil
}
func (wrk *Worker) writeMsg(data []byte) error {
if wrk.write == nil {
return errors.New("write pipe is not started")
}
// Записываем длину сообщения.
dlen := len(data)
_, err := wrk.write.WriteString(strconv.Itoa(dlen) + "\n")
if err != nil {
return err
}
// Записываем сообщение по частям размером PipeChunkSize.
ptr := 0
for ptr < dlen {
_, err = wrk.write.Write(data[ptr:min(ptr+PipeChunkSize, dlen)])
if err != nil {
return err
}
ptr += PipeChunkSize
}
return wrk.write.Flush()
}
func (wrk *Worker) reset() {
wrk.cmd = nil
wrk.write = nil
wrk.read = nil
}
func (wrk *Worker) timedSend(data []byte, timeout time.Duration) *WorkerResult {
ch := make(chan *WorkerResult)
timer := time.NewTimer(timeout)
go func() {
res, err := wrk.send(data)
ch <- &WorkerResult{Res: res, Err: err}
}()
select {
// Ответ пришел до таймаута.
case res := <-ch:
timer.Stop()
if res.Err != nil {
wrk.Restart(true)
}
return res
// Таймаут.
case <-timer.C:
wrk.Restart(true)
return &WorkerResult{
nil,
fmt.Errorf(
"%w: PID %d, after %s",
ErrWorkerTimedOut,
wrk.cmd.Process.Pid,
timeout,
),
}
}
}