|
|
@ -5,22 +5,24 @@ import ( |
|
|
|
"os" |
|
|
|
"os/exec" |
|
|
|
"syscall" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
var subProcess []*Process |
|
|
|
|
|
|
|
type Process struct { |
|
|
|
done bool |
|
|
|
enable bool |
|
|
|
caption string |
|
|
|
name string |
|
|
|
command []string |
|
|
|
process *exec.Cmd |
|
|
|
exit bool |
|
|
|
} |
|
|
|
|
|
|
|
func newProcess(command ...string) *Process { |
|
|
|
process := new(Process) |
|
|
|
process.enable = true |
|
|
|
process.name = command[0] |
|
|
|
process.command = command |
|
|
|
process.caption = command[0] |
|
|
|
log.Debugf("New process %s -> %v", process.caption, process.command) |
|
|
|
log.Debugf("New process %s -> %v", process.name, process.command) |
|
|
|
return process |
|
|
|
} |
|
|
|
|
|
|
@ -32,57 +34,78 @@ func (p *Process) startProcess(isStdout bool, isStderr bool) { |
|
|
|
if isStderr { |
|
|
|
p.process.Stderr = os.Stderr |
|
|
|
} |
|
|
|
p.enable = true |
|
|
|
err := p.process.Start() |
|
|
|
if err != nil { |
|
|
|
log.Errorf("Failed to start %s -> %v", p.caption, err) |
|
|
|
} |
|
|
|
log.Infof("Start process %s -> PID = %d", p.caption, p.process.Process.Pid) |
|
|
|
log.Errorf("Failed to start %s -> %v", p.name, err) |
|
|
|
} |
|
|
|
|
|
|
|
func (p *Process) isProcessAlive() bool { |
|
|
|
return p.process.ProcessState == nil |
|
|
|
log.Infof("Start process %s -> PID = %d", p.name, p.process.Process.Pid) |
|
|
|
} |
|
|
|
|
|
|
|
func (p *Process) sendSignal(signal syscall.Signal) { |
|
|
|
if p != nil { |
|
|
|
err := p.process.Process.Signal(signal) |
|
|
|
if err != nil { |
|
|
|
log.Errorf("Send signal %v to process %s error -> %v", signal, p.caption, err) |
|
|
|
} |
|
|
|
//defer func() {
|
|
|
|
// _ = recover()
|
|
|
|
//}()
|
|
|
|
if p.process != nil && p.process.ProcessState == nil { |
|
|
|
log.Debugf("Send signal %v to %s", signal, p.name) |
|
|
|
_ = p.process.Process.Signal(signal) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (p *Process) waitProcess() { |
|
|
|
if p.process != nil { |
|
|
|
err := p.process.Wait() |
|
|
|
if err != nil { |
|
|
|
log.Warningf("Wait process %s -> %v", p.caption, err) |
|
|
|
} |
|
|
|
log.Warningf("Wait process %s -> %v", p.name, err) |
|
|
|
} |
|
|
|
|
|
|
|
func (p *Process) disableProcess() { |
|
|
|
if p != nil { |
|
|
|
p.enable = false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func daemonSub(sub *Process) { |
|
|
|
for sub.isProcessAlive() { |
|
|
|
for sub.process.ProcessState == nil { |
|
|
|
sub.waitProcess() |
|
|
|
} |
|
|
|
log.Warningf("Catch process %s exit", sub.caption) |
|
|
|
if sub.enable { |
|
|
|
log.Warningf("Catch process %s exit", sub.name) |
|
|
|
if !sub.enable { |
|
|
|
log.Debugf("Process %s disabled -> stop daemon", sub.name) |
|
|
|
return |
|
|
|
} |
|
|
|
sub.startProcess(true, true) |
|
|
|
log.Infof("Process %s restart success", xray.caption) |
|
|
|
log.Infof("Process %s restart success", sub.name) |
|
|
|
daemonSub(sub) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func daemon(sub *Process) { |
|
|
|
if sub != nil && sub.enable { |
|
|
|
log.Infof("Start daemon of process %s", sub.caption) |
|
|
|
if !sub.enable { |
|
|
|
log.Infof("Process %s disabled -> skip daemon", sub.name) |
|
|
|
sub.exit = true |
|
|
|
return |
|
|
|
} |
|
|
|
log.Infof("Start daemon of process %s", sub.name) |
|
|
|
go func() { |
|
|
|
daemonSub(sub) |
|
|
|
sub.done = true |
|
|
|
log.Infof("Process %s daemon exit", sub.name) |
|
|
|
sub.exit = true |
|
|
|
}() |
|
|
|
} |
|
|
|
|
|
|
|
func exit() { |
|
|
|
log.Warningf("Start exit process") |
|
|
|
for _, sub := range subProcess { |
|
|
|
sub.enable = false |
|
|
|
if sub.process != nil { |
|
|
|
sub.sendSignal(syscall.SIGTERM) |
|
|
|
log.Infof("Send kill signal to process %s", sub.name) |
|
|
|
} |
|
|
|
} |
|
|
|
var allExit bool |
|
|
|
log.Info("Wait all sub process exit") |
|
|
|
for !allExit { |
|
|
|
time.Sleep(10 * time.Millisecond) // delay 10ms
|
|
|
|
allExit = true |
|
|
|
for _, sub := range subProcess { |
|
|
|
allExit = allExit && sub.exit //(sub.process.ProcessState != nil)
|
|
|
|
} |
|
|
|
} |
|
|
|
log.Infof("Exit complete") |
|
|
|
} |
|
|
|