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
|
|
|
|
|
2024-01-01 15:07:56 +00:00
|
|
|
import (
|
2025-09-19 08:05:43 +00:00
|
|
|
"github.com/mhsanaei/3x-ui/v2/logger"
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/service"
|
2024-01-01 15:07:56 +00:00
|
|
|
)
|
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() {
|
2025-08-07 18:35:11 +00:00
|
|
|
if !j.xrayService.DidXrayCrash() {
|
2023-02-09 19:18:06 +00:00
|
|
|
j.checkTime = 0
|
2024-01-01 15:07:56 +00:00
|
|
|
} else {
|
|
|
|
j.checkTime++
|
2024-03-10 21:31:24 +00:00
|
|
|
// only restart if it's down 2 times in a row
|
2024-01-01 15:07:56 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|