forked from cloudfoundry-attic/app-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
76 lines (62 loc) · 1.15 KB
/
process.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
package ifrit
import (
"fmt"
"os"
"sync"
)
type Process interface {
Wait() <-chan error
Signal(os.Signal)
}
func Envoke(r Runner) Process {
return envokeProcess(r)
}
func envokeProcess(r Runner) Process {
p := &process{
runner: r,
sig: make(chan os.Signal),
exitStatusChan: make(chan error, 1),
ready: make(chan struct{}),
}
go p.run()
select {
case <-p.ready:
case <-p.Wait():
}
return p
}
type process struct {
runner Runner
sig chan os.Signal
exitStatus error
exitStatusChan chan error
ready chan struct{}
exitOnce sync.Once
}
func (p *process) run() {
defer func() {
msg := recover()
if msg != nil {
p.exitStatusChan <- fmt.Errorf("%s", msg)
}
}()
p.exitStatusChan <- p.runner.Run(p.sig, p.ready)
}
func (p *process) getExitStatus() error {
p.exitOnce.Do(func() {
p.exitStatus = <-p.exitStatusChan
})
return p.exitStatus
}
func (p *process) Wait() <-chan error {
exitChan := make(chan error, 1)
go func() {
exitChan <- p.getExitStatus()
}()
return exitChan
}
func (p *process) Signal(signal os.Signal) {
go func() {
p.sig <- signal
}()
}