-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobkiller.go
77 lines (66 loc) · 2.55 KB
/
jobkiller.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
/*
Copyright (C) 2003-2011 Institute for Systems Biology
Seattle, Washington, USA.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import (
"fmt"
"syscall"
)
// links a SubId and JobId with a pid that can be used to kill it
type Killable struct {
Pid int
SubId string
JobId int
}
// kills via the stored process id
func (k *Killable) Kill() {
logger.Printf("kill process id: %v", k.Pid)
errno := syscall.Kill(k.Pid, syscall.SIGCHLD)
logger.Printf("kill results: %v: %v", k.Pid, errno)
}
//A job killer is created to monitor and kill jobs
type JobKiller struct {
Killchan chan string //used to send in the SubId of jobs to kill
Donechan chan *Killable //used to indicate that a job is done and should no longer be killable
Registerchan chan *Killable //used to register a job as a killable
killables map[string]*Killable //internal structure to keep track of killables by subid+jobId (as strings)
}
//creates a Job Killer and starts its routine KillJobs
func NewJobKiller() (jk *JobKiller) {
jk = &JobKiller{Killchan: make(chan string, 3), Donechan: make(chan *Killable, 3), Registerchan: make(chan *Killable, 3), killables: map[string]*Killable{}}
go jk.KillJobs()
return
}
// should be run as a go routine, monitors job killers channel. maintains the internal map of killables and locates jobs that need to be killed.
func (jk *JobKiller) KillJobs() {
for {
select {
case SubId := <-jk.Killchan:
logger.Debug("killing: %v", SubId)
for _, kb := range jk.killables {
if kb.SubId == SubId {
kb.Kill()
}
}
logger.Debug("done killing: %v", SubId)
case kb := <-jk.Registerchan:
logger.Debug("registering: %v", kb)
jk.killables[fmt.Sprintf("%v%v", kb.SubId, kb.JobId)] = kb
case kb := <-jk.Donechan:
logger.Debug("removing: %v", kb)
delete(jk.killables, fmt.Sprintf("%v%v", kb.SubId, kb.JobId))
}
}
}