Reduce outage time on Xray errors

This commit is contained in:
somebodywashere 2023-12-18 20:48:56 +03:00
parent 78d3680ced
commit 4939474e89
3 changed files with 19 additions and 12 deletions

View file

@ -1,6 +1,9 @@
package job package job
import "x-ui/web/service" import (
"x-ui/logger"
"x-ui/web/service"
)
type CheckXrayRunningJob struct { type CheckXrayRunningJob struct {
xrayService service.XrayService xrayService service.XrayService
@ -15,11 +18,15 @@ func NewCheckXrayRunningJob() *CheckXrayRunningJob {
func (j *CheckXrayRunningJob) Run() { func (j *CheckXrayRunningJob) Run() {
if j.xrayService.IsXrayRunning() { if j.xrayService.IsXrayRunning() {
j.checkTime = 0 j.checkTime = 0
return } 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)
}
}
} }
j.checkTime++
if j.checkTime < 2 {
return
}
j.xrayService.SetToNeedRestart()
} }

View file

@ -185,7 +185,7 @@ func (s *XrayService) RestartXray(isForce bool) error {
return err return err
} }
if p != nil && p.IsRunning() { if s.IsXrayRunning() {
if !isForce && p.GetConfig().Equals(xrayConfig) { if !isForce && p.GetConfig().Equals(xrayConfig) {
logger.Debug("It does not need to restart xray") logger.Debug("It does not need to restart xray")
return nil return nil

View file

@ -240,11 +240,11 @@ func (s *Server) startTask() {
if err != nil { if err != nil {
logger.Warning("start xray failed:", err) logger.Warning("start xray failed:", err)
} }
// Check whether xray is running every 30 seconds // Check whether xray is running every second
s.cron.AddJob("@every 30s", job.NewCheckXrayRunningJob()) s.cron.AddJob("@every 1s", job.NewCheckXrayRunningJob())
// Check if xray needs to be restarted // Check if xray needs to be restarted every 30 seconds
s.cron.AddFunc("@every 10s", func() { s.cron.AddFunc("@every 30s", func() {
if s.xrayService.IsNeedRestartAndSetFalse() { if s.xrayService.IsNeedRestartAndSetFalse() {
err := s.xrayService.RestartXray(false) err := s.xrayService.RestartXray(false)
if err != nil { if err != nil {