2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-05-08 18:41:29 +08:00
|
|
|
//
|
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-05-08 18:41:29 +08:00
|
|
|
|
2018-05-09 18:29:46 +08:00
|
|
|
package gproc
|
2018-05-08 18:41:29 +08:00
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"os"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/gmap"
|
2021-12-23 00:09:00 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2018-05-08 18:41:29 +08:00
|
|
|
)
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Manager is a process manager maintaining multiple processes.
|
2018-05-08 18:41:29 +08:00
|
|
|
type Manager struct {
|
2021-05-17 21:26:39 +08:00
|
|
|
processes *gmap.IntAnyMap // Process id to Process object mapping.
|
2018-05-08 18:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// NewManager creates and returns a new process manager.
|
2018-05-10 19:16:41 +08:00
|
|
|
func NewManager() *Manager {
|
2019-06-19 09:06:52 +08:00
|
|
|
return &Manager{
|
2019-07-23 23:20:27 +08:00
|
|
|
processes: gmap.NewIntAnyMap(true),
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-05-08 18:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// NewProcess creates and returns a Process object.
|
2018-05-10 19:16:41 +08:00
|
|
|
func (m *Manager) NewProcess(path string, args []string, environment []string) *Process {
|
2019-06-19 09:06:52 +08:00
|
|
|
p := NewProcess(path, args, environment)
|
|
|
|
|
p.Manager = m
|
|
|
|
|
return p
|
2018-05-10 19:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// GetProcess retrieves and returns a Process object.
|
|
|
|
|
// It returns nil if it does not find the process with given `pid`.
|
2018-05-08 18:41:29 +08:00
|
|
|
func (m *Manager) GetProcess(pid int) *Process {
|
2019-06-19 09:06:52 +08:00
|
|
|
if v := m.processes.Get(pid); v != nil {
|
|
|
|
|
return v.(*Process)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-05-08 18:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// AddProcess adds a process to current manager.
|
|
|
|
|
// It does nothing if the process with given `pid` does not exist.
|
2018-05-11 12:57:05 +08:00
|
|
|
func (m *Manager) AddProcess(pid int) {
|
2019-06-19 09:06:52 +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
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// RemoveProcess removes a process from current manager.
|
2018-05-11 12:57:05 +08:00
|
|
|
func (m *Manager) RemoveProcess(pid int) {
|
2019-06-19 09:06:52 +08:00
|
|
|
m.processes.Remove(pid)
|
2018-05-11 12:57:05 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Processes retrieves and returns all processes in current manager.
|
2018-05-08 18:41:29 +08:00
|
|
|
func (m *Manager) Processes() []*Process {
|
2019-06-19 09:06:52 +08:00
|
|
|
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-08 18:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Pids retrieves and returns all process id array in current manager.
|
2018-05-11 23:17:25 +08:00
|
|
|
func (m *Manager) Pids() []int {
|
2019-06-19 09:06:52 +08:00
|
|
|
return m.processes.Keys()
|
2018-05-11 23:17:25 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// WaitAll waits until all process exit.
|
2018-05-08 18:41:29 +08:00
|
|
|
func (m *Manager) WaitAll() {
|
2019-06-19 09:06:52 +08:00
|
|
|
processes := m.Processes()
|
|
|
|
|
if len(processes) > 0 {
|
|
|
|
|
for _, p := range processes {
|
|
|
|
|
p.Wait()
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-08 18:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// KillAll kills all processes in current manager.
|
2018-05-08 18:41:29 +08:00
|
|
|
func (m *Manager) KillAll() error {
|
2019-06-19 09:06:52 +08:00
|
|
|
for _, p := range m.Processes() {
|
|
|
|
|
if err := p.Kill(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-05-08 18:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// SignalAll sends a signal `sig` to all processes in current manager.
|
2018-05-08 18:41:29 +08:00
|
|
|
func (m *Manager) SignalAll(sig os.Signal) error {
|
2019-06-19 09:06:52 +08:00
|
|
|
for _, p := range m.Processes() {
|
|
|
|
|
if err := p.Signal(sig); err != nil {
|
2021-12-23 00:09:00 +08:00
|
|
|
err = gerror.Wrapf(err, `send signal to process failed for pid "%d" with signal "%s"`, p.Process.Pid, sig)
|
2019-06-19 09:06:52 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-05-08 18:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Send sends data bytes to all processes in current manager.
|
2018-05-21 10:11:24 +08:00
|
|
|
func (m *Manager) Send(data []byte) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for _, p := range m.Processes() {
|
2021-12-23 00:09:00 +08:00
|
|
|
_ = p.Send(data)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-05-10 19:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// SendTo sneds data bytes to specified processe in current manager.
|
2018-05-18 15:45:08 +08:00
|
|
|
func (m *Manager) SendTo(pid int, data []byte) error {
|
2019-06-19 09:06:52 +08:00
|
|
|
return Send(pid, data)
|
2018-05-10 16:07:14 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Clear removes all processes in current manager.
|
2018-05-11 18:34:17 +08:00
|
|
|
func (m *Manager) Clear() {
|
2019-06-19 09:06:52 +08:00
|
|
|
m.processes.Clear()
|
2018-05-11 18:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Size returns the size of processes in current manager.
|
2018-05-08 18:41:29 +08:00
|
|
|
func (m *Manager) Size() int {
|
2019-06-19 09:06:52 +08:00
|
|
|
return m.processes.Size()
|
|
|
|
|
}
|