Files
mirror-3x-ui/internal/xray/lifecycle.go
T
Hamed 783e0c5438 feat(xray): report managed core lifecycle outcomes (#7264)
At the default log level an operator could see the Xray child PID change many times a day without learning which of those changes the panel caused, or why.

Write one xray_lifecycle line for every real start, stop, restart, hot apply, rejected config and unexpected child exit. It carries the old and new PID, the path that asked (manual, watchdog, client depletion, pending restart and its first trigger, ...), a fixed reason, the force flag, the duration, the hot-apply outcome and the previous child's exit code or signal. An unchanged config still emits nothing and restart policy is unchanged.

A hot apply the core refuses also logs the core's own error once, with the inbound tag or client it concerned, so a restart labelled hot_apply_failed can be traced to its cause.

Fixes #7260
2026-11-29 17:03:00 +03:30

65 lines
1.6 KiB
Go

package xray
import (
"fmt"
"os"
"strings"
"syscall"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/logger"
)
// LifecycleEvent contains only operational metadata from a managed core.
type LifecycleEvent struct {
Event string
Origin string
Reason string
Force bool
OldPID int
NewPID int
Duration time.Duration
HotApply string
HotApplyStep string
HotApplyCode string
Trigger string
StopResult string
Exit *os.ProcessState
}
func (e LifecycleEvent) Log(warn bool) {
line := fmt.Sprintf("xray_lifecycle event=%s origin=%s reason=%s force=%t old_pid=%d new_pid=%d duration_ms=%d hot_apply=%s",
e.Event, e.Origin, e.Reason, e.Force, e.OldPID, e.NewPID, e.Duration.Milliseconds(), e.HotApply)
if e.Trigger != "" {
line += " trigger=" + e.Trigger
}
if e.HotApplyStep != "" {
line += " hot_apply_step=" + e.HotApplyStep + " hot_apply_code=" + e.HotApplyCode
}
if e.StopResult != "" {
line += " stop_result=" + e.StopResult
}
if e.Exit != nil {
if status, ok := e.Exit.Sys().(syscall.WaitStatus); ok && status.Signaled() {
line += " signal=" + strings.ReplaceAll(status.Signal().String(), " ", "_")
} else if code := e.Exit.ExitCode(); code >= 0 {
line += fmt.Sprintf(" exit_code=%d", code)
}
}
if warn {
logger.Warning(line)
} else {
logger.Info(line)
}
}
// LifecycleState returns the last PID and any exit state recorded by Wait.
func (p *Process) LifecycleState() (int, *os.ProcessState) {
if p == nil {
return 0, nil
}
p.mu.RLock()
defer p.mu.RUnlock()
return p.pid, p.processState
}