|
@@ -109,6 +109,7 @@ func (w *procLogWriter) LastLine() string {
|
|
|
|
|
|
|
|
// Process wraps a single mtg process invocation for one mtproto inbound.
|
|
// Process wraps a single mtg process invocation for one mtproto inbound.
|
|
|
type Process struct {
|
|
type Process struct {
|
|
|
|
|
+ mu sync.RWMutex
|
|
|
cmd *exec.Cmd
|
|
cmd *exec.Cmd
|
|
|
done chan struct{}
|
|
done chan struct{}
|
|
|
configPath string
|
|
configPath string
|
|
@@ -126,20 +127,20 @@ func newProcess(configPath, label string) *Process {
|
|
|
|
|
|
|
|
// IsRunning reports whether the mtg process is currently running.
|
|
// IsRunning reports whether the mtg process is currently running.
|
|
|
func (p *Process) IsRunning() bool {
|
|
func (p *Process) IsRunning() bool {
|
|
|
- if p.cmd == nil || p.cmd.Process == nil {
|
|
|
|
|
|
|
+ p.mu.RLock()
|
|
|
|
|
+ cmd, done := p.cmd, p.done
|
|
|
|
|
+ p.mu.RUnlock()
|
|
|
|
|
+ if cmd == nil || cmd.Process == nil {
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
|
- if p.done != nil {
|
|
|
|
|
|
|
+ if done != nil {
|
|
|
select {
|
|
select {
|
|
|
- case <-p.done:
|
|
|
|
|
|
|
+ case <-done:
|
|
|
return false
|
|
return false
|
|
|
default:
|
|
default:
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- if p.cmd.ProcessState == nil {
|
|
|
|
|
- return true
|
|
|
|
|
- }
|
|
|
|
|
- return false
|
|
|
|
|
|
|
+ return true
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// GetResult returns the last log line or the exit error from the mtg process.
|
|
// GetResult returns the last log line or the exit error from the mtg process.
|
|
@@ -147,8 +148,11 @@ func (p *Process) GetResult() string {
|
|
|
if line := p.logWriter.LastLine(); line != "" {
|
|
if line := p.logWriter.LastLine(); line != "" {
|
|
|
return line
|
|
return line
|
|
|
}
|
|
}
|
|
|
- if p.exitErr != nil {
|
|
|
|
|
- return p.exitErr.Error()
|
|
|
|
|
|
|
+ p.mu.RLock()
|
|
|
|
|
+ exitErr := p.exitErr
|
|
|
|
|
+ p.mu.RUnlock()
|
|
|
|
|
+ if exitErr != nil {
|
|
|
|
|
+ return exitErr.Error()
|
|
|
}
|
|
}
|
|
|
return ""
|
|
return ""
|
|
|
}
|
|
}
|
|
@@ -161,22 +165,27 @@ func (p *Process) Start() error {
|
|
|
cmd := exec.CommandContext(context.Background(), GetBinaryPath(), "run", p.configPath)
|
|
cmd := exec.CommandContext(context.Background(), GetBinaryPath(), "run", p.configPath)
|
|
|
cmd.Stdout = p.logWriter
|
|
cmd.Stdout = p.logWriter
|
|
|
cmd.Stderr = p.logWriter
|
|
cmd.Stderr = p.logWriter
|
|
|
|
|
+ done := make(chan struct{})
|
|
|
|
|
+ p.mu.Lock()
|
|
|
p.cmd = cmd
|
|
p.cmd = cmd
|
|
|
- p.done = make(chan struct{})
|
|
|
|
|
|
|
+ p.done = done
|
|
|
p.exitErr = nil
|
|
p.exitErr = nil
|
|
|
|
|
+ p.mu.Unlock()
|
|
|
p.intentionalStop.Store(false)
|
|
p.intentionalStop.Store(false)
|
|
|
if err := cmd.Start(); err != nil {
|
|
if err := cmd.Start(); err != nil {
|
|
|
- close(p.done)
|
|
|
|
|
|
|
+ close(done)
|
|
|
|
|
+ p.mu.Lock()
|
|
|
p.cmd = nil
|
|
p.cmd = nil
|
|
|
|
|
+ p.mu.Unlock()
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
attachChildLifetime(cmd)
|
|
attachChildLifetime(cmd)
|
|
|
- go p.wait(cmd)
|
|
|
|
|
|
|
+ go p.wait(cmd, done)
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (p *Process) wait(cmd *exec.Cmd) {
|
|
|
|
|
- defer close(p.done)
|
|
|
|
|
|
|
+func (p *Process) wait(cmd *exec.Cmd, done chan struct{}) {
|
|
|
|
|
+ defer close(done)
|
|
|
err := cmd.Wait()
|
|
err := cmd.Wait()
|
|
|
p.logWriter.Flush()
|
|
p.logWriter.Flush()
|
|
|
if err == nil || p.intentionalStop.Load() {
|
|
if err == nil || p.intentionalStop.Load() {
|
|
@@ -184,12 +193,18 @@ func (p *Process) wait(cmd *exec.Cmd) {
|
|
|
}
|
|
}
|
|
|
if runtime.GOOS == "windows" {
|
|
if runtime.GOOS == "windows" {
|
|
|
if strings.Contains(strings.ToLower(err.Error()), "exit status 1") {
|
|
if strings.Contains(strings.ToLower(err.Error()), "exit status 1") {
|
|
|
- p.exitErr = err
|
|
|
|
|
|
|
+ p.setExitErr(err)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
logger.Errorf("mtproto: mtg process exited: %v", err)
|
|
logger.Errorf("mtproto: mtg process exited: %v", err)
|
|
|
|
|
+ p.setExitErr(err)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (p *Process) setExitErr(err error) {
|
|
|
|
|
+ p.mu.Lock()
|
|
|
p.exitErr = err
|
|
p.exitErr = err
|
|
|
|
|
+ p.mu.Unlock()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Stop terminates the running mtg process gracefully, falling back to a kill.
|
|
// Stop terminates the running mtg process gracefully, falling back to a kill.
|
|
@@ -198,40 +213,46 @@ func (p *Process) Stop() error {
|
|
|
return errors.New("mtg is not running")
|
|
return errors.New("mtg is not running")
|
|
|
}
|
|
}
|
|
|
p.intentionalStop.Store(true)
|
|
p.intentionalStop.Store(true)
|
|
|
|
|
+ p.mu.RLock()
|
|
|
|
|
+ cmd, done := p.cmd, p.done
|
|
|
|
|
+ p.mu.RUnlock()
|
|
|
|
|
+ if cmd == nil || cmd.Process == nil {
|
|
|
|
|
+ return errors.New("mtg is not running")
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
if runtime.GOOS == "windows" {
|
|
|
- if err := p.cmd.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
|
|
|
|
|
|
|
+ if err := cmd.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
- return p.waitForExit(forceStopTimeout)
|
|
|
|
|
|
|
+ return waitForExit(done, forceStopTimeout)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if err := p.cmd.Process.Signal(syscall.SIGTERM); err != nil {
|
|
|
|
|
|
|
+ if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
|
|
|
if errors.Is(err, os.ErrProcessDone) {
|
|
if errors.Is(err, os.ErrProcessDone) {
|
|
|
- return p.waitForExit(forceStopTimeout)
|
|
|
|
|
|
|
+ return waitForExit(done, forceStopTimeout)
|
|
|
}
|
|
}
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if err := p.waitForExit(gracefulStopTimeout); err == nil {
|
|
|
|
|
|
|
+ if err := waitForExit(done, gracefulStopTimeout); err == nil {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
logger.Warning("mtproto: mtg did not stop after SIGTERM, killing process")
|
|
logger.Warning("mtproto: mtg did not stop after SIGTERM, killing process")
|
|
|
- if err := p.cmd.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
|
|
|
|
|
|
|
+ if err := cmd.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
- return p.waitForExit(forceStopTimeout)
|
|
|
|
|
|
|
+ return waitForExit(done, forceStopTimeout)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (p *Process) waitForExit(timeout time.Duration) error {
|
|
|
|
|
- if p.done == nil {
|
|
|
|
|
|
|
+func waitForExit(done <-chan struct{}, timeout time.Duration) error {
|
|
|
|
|
+ if done == nil {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
timer := time.NewTimer(timeout)
|
|
timer := time.NewTimer(timeout)
|
|
|
defer timer.Stop()
|
|
defer timer.Stop()
|
|
|
select {
|
|
select {
|
|
|
- case <-p.done:
|
|
|
|
|
|
|
+ case <-done:
|
|
|
return nil
|
|
return nil
|
|
|
case <-timer.C:
|
|
case <-timer.C:
|
|
|
return fmt.Errorf("timed out waiting for mtg process to stop after %s", timeout)
|
|
return fmt.Errorf("timed out waiting for mtg process to stop after %s", timeout)
|