Browse Source

update: sub process exit

v1.x.x
dnomd343 2 years ago
parent
commit
0ecd1d0b12
  1. 29
      src/main.go
  2. 25
      src/process.go

29
src/main.go

@ -1,11 +1,15 @@
package main package main
import ( import (
"fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"syscall"
"time"
) )
var xray *Process var xray *Process
var sleep *Process var sleep *Process
var empty *Process
var logLevel = "warning" var logLevel = "warning"
@ -22,6 +26,21 @@ var addOnInbounds []interface{}
var assetFile = "/etc/xproxy/assets.tar.xz" var assetFile = "/etc/xproxy/assets.tar.xz"
func exit() {
log.Warningf("Start exit process")
xray.disableProcess()
xray.sendSignal(syscall.SIGTERM)
//log.Infof("Send kill signal to process %s", xray.caption)
sleep.disableProcess()
sleep.sendSignal(syscall.SIGTERM)
empty.disableProcess()
empty.sendSignal(syscall.SIGTERM)
log.Info("Wait sub process exit")
for !(xray.done && sleep.done) {
}
log.Infof("Exit complete")
}
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.Warning("XProxy start") log.Warning("XProxy start")
@ -32,12 +51,18 @@ func main() {
sleep = newProcess("sleep", "1000") sleep = newProcess("sleep", "1000")
sleep.startProcess(true, true) sleep.startProcess(true, true)
done := make(chan bool, 1) //done := make(chan bool, 1)
daemon(xray) daemon(xray)
daemon(sleep) daemon(sleep)
daemon(empty)
fmt.Println("start sleep...")
time.Sleep(10 * time.Second)
fmt.Println("wake up")
exit()
<-done //<-done
//content, err := os.ReadFile("test.yml") //content, err := os.ReadFile("test.yml")
//if err != nil { //if err != nil {

25
src/process.go

@ -8,6 +8,7 @@ import (
) )
type Process struct { type Process struct {
done bool
enable bool enable bool
caption string caption string
command []string command []string
@ -43,9 +44,11 @@ func (p *Process) isProcessAlive() bool {
} }
func (p *Process) sendSignal(signal syscall.Signal) { func (p *Process) sendSignal(signal syscall.Signal) {
err := p.process.Process.Signal(signal) if p != nil {
if err != nil { err := p.process.Process.Signal(signal)
log.Errorf("Send signal %v to process %s error -> %v", signal, p.caption, err) if err != nil {
log.Errorf("Send signal %v to process %s error -> %v", signal, p.caption, err)
}
} }
} }
@ -56,6 +59,12 @@ func (p *Process) waitProcess() {
} }
} }
func (p *Process) disableProcess() {
if p != nil {
p.enable = false
}
}
func daemonSub(sub *Process) { func daemonSub(sub *Process) {
for sub.isProcessAlive() { for sub.isProcessAlive() {
sub.waitProcess() sub.waitProcess()
@ -69,7 +78,11 @@ func daemonSub(sub *Process) {
} }
func daemon(sub *Process) { func daemon(sub *Process) {
go func() { if sub != nil && sub.enable {
daemonSub(sub) log.Infof("Start daemon of process %s", sub.caption)
}() go func() {
daemonSub(sub)
sub.done = true
}()
}
} }

Loading…
Cancel
Save