github.com/gogf/gf@v1.16.9/os/gproc/gproc_manager.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gproc
     8  
     9  import (
    10  	"os"
    11  
    12  	"github.com/gogf/gf/container/gmap"
    13  )
    14  
    15  // Manager is a process manager maintaining multiple processes.
    16  type Manager struct {
    17  	processes *gmap.IntAnyMap // Process id to Process object mapping.
    18  }
    19  
    20  // NewManager creates and returns a new process manager.
    21  func NewManager() *Manager {
    22  	return &Manager{
    23  		processes: gmap.NewIntAnyMap(true),
    24  	}
    25  }
    26  
    27  // NewProcess creates and returns a Process object.
    28  func (m *Manager) NewProcess(path string, args []string, environment []string) *Process {
    29  	p := NewProcess(path, args, environment)
    30  	p.Manager = m
    31  	return p
    32  }
    33  
    34  // GetProcess retrieves and returns a Process object.
    35  // It returns nil if it does not find the process with given `pid`.
    36  func (m *Manager) GetProcess(pid int) *Process {
    37  	if v := m.processes.Get(pid); v != nil {
    38  		return v.(*Process)
    39  	}
    40  	return nil
    41  }
    42  
    43  // AddProcess adds a process to current manager.
    44  // It does nothing if the process with given `pid` does not exist.
    45  func (m *Manager) AddProcess(pid int) {
    46  	if m.processes.Get(pid) == nil {
    47  		if process, err := os.FindProcess(pid); err == nil {
    48  			p := m.NewProcess("", nil, nil)
    49  			p.Process = process
    50  			m.processes.Set(pid, p)
    51  		}
    52  	}
    53  }
    54  
    55  // RemoveProcess removes a process from current manager.
    56  func (m *Manager) RemoveProcess(pid int) {
    57  	m.processes.Remove(pid)
    58  }
    59  
    60  // Processes retrieves and returns all processes in current manager.
    61  func (m *Manager) Processes() []*Process {
    62  	processes := make([]*Process, 0)
    63  	m.processes.RLockFunc(func(m map[int]interface{}) {
    64  		for _, v := range m {
    65  			processes = append(processes, v.(*Process))
    66  		}
    67  	})
    68  	return processes
    69  }
    70  
    71  // Pids retrieves and returns all process id array in current manager.
    72  func (m *Manager) Pids() []int {
    73  	return m.processes.Keys()
    74  }
    75  
    76  // WaitAll waits until all process exit.
    77  func (m *Manager) WaitAll() {
    78  	processes := m.Processes()
    79  	if len(processes) > 0 {
    80  		for _, p := range processes {
    81  			p.Wait()
    82  		}
    83  	}
    84  }
    85  
    86  // KillAll kills all processes in current manager.
    87  func (m *Manager) KillAll() error {
    88  	for _, p := range m.Processes() {
    89  		if err := p.Kill(); err != nil {
    90  			return err
    91  		}
    92  	}
    93  	return nil
    94  }
    95  
    96  // SignalAll sends a signal `sig` to all processes in current manager.
    97  func (m *Manager) SignalAll(sig os.Signal) error {
    98  	for _, p := range m.Processes() {
    99  		if err := p.Signal(sig); err != nil {
   100  			return err
   101  		}
   102  	}
   103  	return nil
   104  }
   105  
   106  // Send sends data bytes to all processes in current manager.
   107  func (m *Manager) Send(data []byte) {
   108  	for _, p := range m.Processes() {
   109  		p.Send(data)
   110  	}
   111  }
   112  
   113  // SendTo sneds data bytes to specified processe in current manager.
   114  func (m *Manager) SendTo(pid int, data []byte) error {
   115  	return Send(pid, data)
   116  }
   117  
   118  // Clear removes all processes in current manager.
   119  func (m *Manager) Clear() {
   120  	m.processes.Clear()
   121  }
   122  
   123  // Size returns the size of processes in current manager.
   124  func (m *Manager) Size() int {
   125  	return m.processes.Size()
   126  }