wide/output/processes.go

106 lines
2.6 KiB
Go
Raw Permalink Normal View History

2014-11-12 18:13:14 +03:00
// Copyright (c) 2014, B3log
2014-11-21 09:22:12 +03:00
//
2014-11-12 18:13:14 +03:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-11-21 09:22:12 +03:00
//
2014-11-12 18:13:14 +03:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-11-21 09:22:12 +03:00
//
2014-11-12 18:13:14 +03:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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
)
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()
2014-12-07 06:07:32 +03:00
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-12-23 04:35:29 +03:00
logger.Tracef("Session [%s] has [%d] processes", sid, len((*procs)[sid]))
2014-09-16 13:32:55 +04:00
}
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()
2014-12-07 06:07:32 +03:00
sid := wSession.ID
2014-09-19 15:21:13 +04:00
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-12-23 04:35:29 +03:00
logger.Tracef("Session [%s] has [%d] processes", sid, len((*procs)[sid]))
2014-09-16 13:32:55 +04:00
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()
2014-12-07 06:07:32 +03:00
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 {
2014-12-13 13:47:41 +03:00
logger.Error("Kill a process [pid=%d] of session [%s] failed [error=%v]", pid, sid, err)
2014-09-16 13:32:55 +04:00
} 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-12-13 13:47:41 +03:00
logger.Debugf("Killed a process [pid=%d] of session [%s]", pid, sid)
2014-09-16 13:32:55 +04:00
}
2014-09-22 13:44:07 +04:00
return
2014-09-16 13:32:55 +04:00
}
}
}