2018-05-08 18:41:29 +08:00
|
|
|
|
// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
|
|
|
|
|
|
|
|
|
|
|
// 进程管理.
|
2018-05-09 18:29:46 +08:00
|
|
|
|
package gproc
|
2018-05-08 18:41:29 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"os"
|
2018-05-13 00:17:12 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/gmap"
|
2018-05-08 18:41:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 进程管理器
|
|
|
|
|
|
type Manager struct {
|
|
|
|
|
|
processes *gmap.IntInterfaceMap // 所管理的子进程map
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个进程管理器
|
2018-05-10 19:16:41 +08:00
|
|
|
|
func NewManager() *Manager {
|
2018-05-08 18:41:29 +08:00
|
|
|
|
return &Manager{
|
|
|
|
|
|
processes : gmap.NewIntInterfaceMap(),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-10 19:16:41 +08:00
|
|
|
|
// 创建一个进程(不执行)
|
|
|
|
|
|
func (m *Manager) NewProcess(path string, args []string, environment []string) *Process {
|
|
|
|
|
|
p := NewProcess(path, args, environment)
|
2018-05-13 00:17:12 +08:00
|
|
|
|
p.Manager = m
|
2018-05-10 19:16:41 +08:00
|
|
|
|
return p
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-08 23:38:09 +08:00
|
|
|
|
// 获取当前进程管理器中的一个进程
|
2018-05-08 18:41:29 +08:00
|
|
|
|
func (m *Manager) GetProcess(pid int) *Process {
|
|
|
|
|
|
if v := m.processes.Get(pid); v != nil {
|
|
|
|
|
|
return v.(*Process)
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-11 12:57:05 +08:00
|
|
|
|
// 添加一个已存在进程到进程管理器中
|
|
|
|
|
|
func (m *Manager) AddProcess(pid int) {
|
2018-05-20 13:49:02 +08:00
|
|
|
|
if m.processes.Get(pid) == nil {
|
|
|
|
|
|
if process, err := os.FindProcess(pid); err == nil {
|
|
|
|
|
|
p := m.NewProcess("", nil, nil)
|
|
|
|
|
|
p.Process = process
|
|
|
|
|
|
m.processes.Set(pid, p)
|
|
|
|
|
|
}
|
2018-05-11 12:57:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 移除进程管理器中的指定进程
|
|
|
|
|
|
func (m *Manager) RemoveProcess(pid int) {
|
|
|
|
|
|
m.processes.Remove(pid)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-08 18:41:29 +08:00
|
|
|
|
// 获取所有的进程对象,构成列表返回
|
|
|
|
|
|
func (m *Manager) Processes() []*Process {
|
|
|
|
|
|
processes := make([]*Process, 0)
|
|
|
|
|
|
m.processes.RLockFunc(func(m map[int]interface{}) {
|
|
|
|
|
|
for _, v := range m {
|
|
|
|
|
|
processes = append(processes, v.(*Process))
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
return processes
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-11 23:17:25 +08:00
|
|
|
|
// 获取所有的进程pid,构成列表返回
|
|
|
|
|
|
func (m *Manager) Pids() []int {
|
|
|
|
|
|
return m.processes.Keys()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-08 18:41:29 +08:00
|
|
|
|
// 等待所有子进程结束
|
|
|
|
|
|
func (m *Manager) WaitAll() {
|
|
|
|
|
|
processes := m.Processes()
|
|
|
|
|
|
if len(processes) > 0 {
|
|
|
|
|
|
for _, p := range processes {
|
|
|
|
|
|
p.Wait()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 关闭所有的进程
|
|
|
|
|
|
func (m *Manager) KillAll() error {
|
|
|
|
|
|
for _, p := range m.Processes() {
|
|
|
|
|
|
if err := p.Kill(); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 向所有进程发送信号量
|
|
|
|
|
|
func (m *Manager) SignalAll(sig os.Signal) error {
|
|
|
|
|
|
for _, p := range m.Processes() {
|
|
|
|
|
|
if err := p.Signal(sig); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-10 19:16:41 +08:00
|
|
|
|
// 向所有进程发送消息
|
2018-05-21 10:11:24 +08:00
|
|
|
|
func (m *Manager) Send(data []byte) {
|
2018-05-10 19:16:41 +08:00
|
|
|
|
for _, p := range m.Processes() {
|
2018-05-21 10:11:24 +08:00
|
|
|
|
p.Send(data)
|
2018-05-10 19:16:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 向指定进程发送消息
|
2018-05-18 15:45:08 +08:00
|
|
|
|
func (m *Manager) SendTo(pid int, data []byte) error {
|
2018-05-10 16:07:14 +08:00
|
|
|
|
return Send(pid, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-11 18:34:17 +08:00
|
|
|
|
// 清空管理器
|
|
|
|
|
|
func (m *Manager) Clear() {
|
|
|
|
|
|
m.processes.Clear()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-08 18:41:29 +08:00
|
|
|
|
// 当前进程总数
|
|
|
|
|
|
func (m *Manager) Size() int {
|
|
|
|
|
|
return m.processes.Size()
|
|
|
|
|
|
}
|