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 }