wide/output/processes.go

106 lines
2.6 KiB
Go
Raw Normal View History

2019-05-17 06:28:50 +03:00
// Copyright (c) 2014-present, b3log.org
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
//
2018-03-12 07:28:33 +03:00
// https://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>
2015-02-13 04:59:51 +03:00
var Processes = procs{}
2014-09-16 13:32:55 +04:00
2014-10-29 13:15:18 +03:00
// Exclusive lock.
2014-09-19 15:21:13 +04:00
var mutex sync.Mutex
2015-09-26 10:40:05 +03:00
// Add adds the specified process to the user process set.
2015-02-13 04:59:51 +03:00
func (procs *procs) Add(wSession *session.WideSession, proc *os.Process) {
2014-09-19 15:21:13 +04:00
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
}
2015-09-26 10:40:05 +03:00
// Remove removes the specified process from the user process set.
2015-02-13 04:59:51 +03:00
func (procs *procs) Remove(wSession *session.WideSession, proc *os.Process) {
2014-09-19 15:21:13 +04:00
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
}
}
}
2015-09-26 10:40:05 +03:00
// Kill kills a process specified by the given pid.
2015-02-13 04:59:51 +03:00
func (procs *procs) Kill(wSession *session.WideSession, pid int) {
2014-09-19 15:21:13 +04:00
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 {
2019-05-16 18:17:25 +03:00
logger.Errorf("Kill a process [pid=%d] of user [%s, %s] failed [error=%v]", pid, wSession.UserId, 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)
2019-05-16 18:17:25 +03:00
logger.Debugf("Killed a process [pid=%d] of user [%s, %s]", pid, wSession.UserId, 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
}
}
}