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-10 19:16:41 +08:00
|
|
|
"fmt"
|
2018-05-13 00:17:12 +08:00
|
|
|
"os/exec"
|
2018-05-20 13:49:02 +08:00
|
|
|
"errors"
|
2018-05-08 18:41:29 +08:00
|
|
|
)
|
|
|
|
|
2018-05-09 18:29:46 +08:00
|
|
|
// 子进程
|
|
|
|
type Process struct {
|
2018-05-13 00:17:12 +08:00
|
|
|
exec.Cmd
|
|
|
|
Manager *Manager // 所属进程管理器
|
|
|
|
PPid int // 自定义关联的父进程ID
|
2018-05-09 18:29:46 +08:00
|
|
|
}
|
|
|
|
|
2018-05-13 00:17:12 +08:00
|
|
|
// 开始执行(非阻塞)
|
|
|
|
func (p *Process) Start() (int, error) {
|
|
|
|
if p.Process != nil {
|
2018-05-08 23:38:09 +08:00
|
|
|
return p.Pid(), nil
|
|
|
|
}
|
2018-05-13 00:17:12 +08:00
|
|
|
if p.PPid > 0 {
|
|
|
|
p.Env = append(p.Env, fmt.Sprintf("%s=%d", gPROC_ENV_KEY_PPID_KEY, p.PPid))
|
2018-05-12 10:37:42 +08:00
|
|
|
}
|
2018-05-13 00:17:12 +08:00
|
|
|
if err := p.Cmd.Start(); err == nil {
|
|
|
|
if p.Manager != nil {
|
|
|
|
p.Manager.processes.Set(p.Process.Pid, p)
|
2018-05-10 19:16:41 +08:00
|
|
|
}
|
2018-05-13 00:17:12 +08:00
|
|
|
return p.Process.Pid, nil
|
2018-05-08 18:41:29 +08:00
|
|
|
} else {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 00:17:12 +08:00
|
|
|
// 运行进程(阻塞等待执行完毕)
|
|
|
|
func (p *Process) Run() error {
|
|
|
|
if _, err := p.Start(); err == nil {
|
|
|
|
return p.Wait()
|
|
|
|
} else {
|
|
|
|
return err
|
2018-05-08 18:41:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PID
|
|
|
|
func (p *Process) Pid() int {
|
2018-05-13 00:17:12 +08:00
|
|
|
if p.Process != nil {
|
|
|
|
return p.Process.Pid
|
2018-05-08 18:41:29 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-05-10 16:07:14 +08:00
|
|
|
// 向进程发送消息
|
2018-05-18 15:45:08 +08:00
|
|
|
func (p *Process) Send(data []byte) error {
|
2018-05-13 00:17:12 +08:00
|
|
|
if p.Process != nil {
|
|
|
|
return Send(p.Process.Pid, data)
|
2018-05-10 16:07:14 +08:00
|
|
|
}
|
2018-05-20 13:49:02 +08:00
|
|
|
return errors.New("invalid process")
|
2018-05-10 16:07:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-08 18:41:29 +08:00
|
|
|
// Release releases any resources associated with the Process p,
|
|
|
|
// rendering it unusable in the future.
|
|
|
|
// Release only needs to be called if Wait is not.
|
|
|
|
func (p *Process) Release() error {
|
2018-05-13 00:17:12 +08:00
|
|
|
return p.Process.Release()
|
2018-05-08 18:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Kill causes the Process to exit immediately.
|
|
|
|
func (p *Process) Kill() error {
|
2018-05-13 00:17:12 +08:00
|
|
|
if err := p.Process.Kill(); err == nil {
|
|
|
|
if p.Manager != nil {
|
|
|
|
p.Manager.processes.Remove(p.Pid())
|
2018-05-10 19:16:41 +08:00
|
|
|
}
|
2018-05-08 18:41:29 +08:00
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal sends a signal to the Process.
|
|
|
|
// Sending Interrupt on Windows is not implemented.
|
|
|
|
func (p *Process) Signal(sig os.Signal) error {
|
2018-05-13 00:17:12 +08:00
|
|
|
return p.Process.Signal(sig)
|
2018-05-08 18:41:29 +08:00
|
|
|
}
|