-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
107 lines (97 loc) · 1.91 KB
/
context.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
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"sync"
)
var (
mu sync.Mutex
stopped = false
procs = make(map[int]*os.Process)
)
type context struct {
workerID int
mappers int
reducers int
}
func (c context) err(msg string) error {
return fmt.Errorf("error in worker.%d: %s", c.workerID, msg)
}
func (c context) log(msg string) {
log.Printf(" [worker.%d] %s", c.workerID, msg)
}
func (c context) logf(format string, v ...interface{}) {
c.log(fmt.Sprintf(format, v...))
}
func (c context) exec(
command string,
stdinHandler func(context, io.WriteCloser) error,
stdoutHandler func(context, io.ReadCloser) error,
stderrHandler func(context, io.ReadCloser) error,
) error {
args := strings.Fields(command)
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = append(
os.Environ(),
fmt.Sprintf("WORKER_ID=%d", c.workerID),
fmt.Sprintf("MAPPERS=%d", c.mappers),
fmt.Sprintf("REDUCERS=%d", c.reducers),
)
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
errc := make(chan error)
go func() { errc <- stdinHandler(c, stdin) }()
go func() { errc <- stdoutHandler(c, stdout) }()
go func() { errc <- stderrHandler(c, stderr) }()
if err := start(cmd); err != nil {
return err
}
for i := 0; i < 3; i++ {
if err := <-errc; err != nil {
return err
}
}
return wait(cmd)
}
func start(c *exec.Cmd) error {
mu.Lock()
defer mu.Unlock()
if stopped {
return errors.New("no new processes may be started")
}
if err := c.Start(); err != nil {
return err
}
procs[c.Process.Pid] = c.Process
return nil
}
func wait(c *exec.Cmd) error {
err := c.Wait()
mu.Lock()
delete(procs, c.Process.Pid)
mu.Unlock()
return err
}
func killAll() {
mu.Lock()
defer mu.Unlock()
stopped = true
for _, p := range procs {
p.Kill()
}
}