wide/output/processes.go

93 lines
2.0 KiB
Go
Raw Normal View History

2014-09-16 13:32:55 +04:00
package output
import (
"os"
2014-09-19 15:21:13 +04:00
"sync"
2014-09-16 13:32:55 +04:00
2014-09-19 15:21:13 +04:00
"github.com/b3log/wide/session"
2014-09-16 13:32:55 +04:00
"github.com/golang/glog"
)
2014-10-29 13:15:18 +03:00
// Type of process set.
2014-10-10 07:17:51 +04:00
type procs map[string][]*os.Process
2014-10-29 13:15:18 +03:00
// Processse of all users.
2014-09-25 09:37:59 +04:00
//
2014-09-16 13:32:55 +04:00
// <sid, []*os.Process>
var processes = procs{}
2014-10-29 13:15:18 +03:00
// Exclusive lock.
2014-09-19 15:21:13 +04:00
var mutex sync.Mutex
2014-10-29 13:15:18 +03:00
// add adds the specified process to the user process set.
2014-09-19 15:21:13 +04:00
func (procs *procs) add(wSession *session.WideSession, proc *os.Process) {
mutex.Lock()
defer mutex.Unlock()
sid := wSession.Id
2014-09-16 13:32:55 +04:00
userProcesses := (*procs)[sid]
userProcesses = append(userProcesses, proc)
(*procs)[sid] = userProcesses
2014-10-29 13:15:18 +03:00
// bind process with wide session
2014-09-19 15:21:13 +04:00
wSession.SetProcesses(userProcesses)
2014-09-16 13:32:55 +04:00
glog.V(3).Infof("Session [%s] has [%d] processes", sid, len((*procs)[sid]))
}
2014-10-29 13:15:18 +03:00
// remove removes the specified process from the user process set.
2014-09-19 15:21:13 +04:00
func (procs *procs) remove(wSession *session.WideSession, proc *os.Process) {
mutex.Lock()
defer mutex.Unlock()
sid := wSession.Id
2014-09-16 13:32:55 +04:00
userProcesses := (*procs)[sid]
var newProcesses []*os.Process
for i, p := range userProcesses {
if p.Pid == proc.Pid {
2014-10-29 13:15:18 +03:00
newProcesses = append(userProcesses[:i], userProcesses[i+1:]...) // remove it
2014-09-16 13:32:55 +04:00
(*procs)[sid] = newProcesses
2014-10-29 13:15:18 +03:00
// bind process with wide session
2014-09-19 15:21:13 +04:00
wSession.SetProcesses(newProcesses)
2014-09-16 13:32:55 +04:00
glog.V(3).Infof("Session [%s] has [%d] processes", sid, len((*procs)[sid]))
return
}
}
}
2014-10-29 13:15:18 +03:00
// kill kills a process specified by the given pid.
2014-09-19 15:21:13 +04:00
func (procs *procs) kill(wSession *session.WideSession, pid int) {
mutex.Lock()
defer mutex.Unlock()
sid := wSession.Id
2014-09-16 13:32:55 +04:00
2014-09-19 15:21:13 +04:00
userProcesses := (*procs)[sid]
for i, p := range userProcesses {
2014-09-16 13:32:55 +04:00
if p.Pid == pid {
if err := p.Kill(); nil != err {
glog.Error("Kill a process [pid=%d] of session [%s] failed [error=%v]", pid, sid, err)
} else {
2014-09-19 15:21:13 +04:00
var newProcesses []*os.Process
newProcesses = append(userProcesses[:i], userProcesses[i+1:]...)
(*procs)[sid] = newProcesses
2014-10-29 13:15:18 +03:00
// bind process with wide session
2014-09-19 15:21:13 +04:00
wSession.SetProcesses(newProcesses)
2014-09-16 13:32:55 +04:00
glog.V(3).Infof("Killed a process [pid=%d] of session [%s]", pid, sid)
}
2014-09-22 13:44:07 +04:00
return
2014-09-16 13:32:55 +04:00
}
}
}