forked from slene/margo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_kill.go
86 lines (70 loc) · 1.33 KB
/
m_kill.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
package main
import (
"os/exec"
"sync"
)
var (
cmdWatchlist = map[string]*exec.Cmd{}
cmdWatchLck = sync.Mutex{}
)
type mKill struct {
Cid string
}
func (m *mKill) Call() (res interface{}, err string) {
res = M{
m.Cid: killCmd(m.Cid),
}
return
}
func watchCmd(id string, c *exec.Cmd) bool {
if id == "" {
return false
}
cmdWatchLck.Lock()
defer cmdWatchLck.Unlock()
if _, ok := cmdWatchlist[id]; ok {
return false
}
cmdWatchlist[id] = c
return true
}
func unwatchCmd(id string) bool {
if id == "" {
return false
}
cmdWatchLck.Lock()
defer cmdWatchLck.Unlock()
if _, ok := cmdWatchlist[id]; ok {
delete(cmdWatchlist, id)
return true
}
return false
}
func killCmd(id string) bool {
if id == "" {
return false
}
cmdWatchLck.Lock()
defer cmdWatchLck.Unlock()
if c, ok := cmdWatchlist[id]; ok {
// the primary use-case for these functions are remote requests to cancel the proces
// so we won't remove it from the map
c.Process.Kill()
// neither wait nor release are called because the cmd owner should be waiting on it
return true
}
return false
}
func init() {
byeDefer(func() {
cmdWatchLck.Lock()
defer cmdWatchLck.Unlock()
for _, c := range cmdWatchlist {
c.Process.Kill()
c.Process.Release()
}
})
registry.Register("kill", func(b *Broker) Caller {
return &mKill{}
})
}