3x-ui/web/job/check_xray_running_job.go

37 lines
1 KiB
Go
Raw Normal View History

2025-09-20 07:35:50 +00:00
// Package job provides background job implementations for the 3x-ui web panel,
// including traffic monitoring, system checks, and periodic maintenance tasks.
2023-02-09 19:18:06 +00:00
package job
import (
2025-09-19 08:05:43 +00:00
"github.com/mhsanaei/3x-ui/v2/logger"
"github.com/mhsanaei/3x-ui/v2/web/service"
)
2023-02-09 19:18:06 +00:00
2025-09-20 07:35:50 +00:00
// CheckXrayRunningJob monitors Xray process health and restarts it if it crashes.
2023-02-09 19:18:06 +00:00
type CheckXrayRunningJob struct {
xrayService service.XrayService
2025-09-20 07:35:50 +00:00
checkTime int
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// NewCheckXrayRunningJob creates a new Xray health check job instance.
2023-02-09 19:18:06 +00:00
func NewCheckXrayRunningJob() *CheckXrayRunningJob {
return new(CheckXrayRunningJob)
}
2025-09-20 07:35:50 +00:00
// Run checks if Xray has crashed and restarts it after confirming it's down for 2 consecutive checks.
2023-02-09 19:18:06 +00:00
func (j *CheckXrayRunningJob) Run() {
if !j.xrayService.DidXrayCrash() {
2023-02-09 19:18:06 +00:00
j.checkTime = 0
} else {
j.checkTime++
// only restart if it's down 2 times in a row
if j.checkTime > 1 {
err := j.xrayService.RestartXray(false)
j.checkTime = 0
if err != nil {
logger.Error("Restart xray failed:", err)
}
}
2023-02-09 19:18:06 +00:00
}
}