check_xray_running_job.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Package job provides background job implementations for the 3x-ui web panel,
  2. // including traffic monitoring, system checks, and periodic maintenance tasks.
  3. package job
  4. import (
  5. "github.com/mhsanaei/3x-ui/v2/logger"
  6. "github.com/mhsanaei/3x-ui/v2/web/service"
  7. )
  8. // CheckXrayRunningJob monitors Xray process health and restarts it if it crashes.
  9. type CheckXrayRunningJob struct {
  10. xrayService service.XrayService
  11. checkTime int
  12. }
  13. // NewCheckXrayRunningJob creates a new Xray health check job instance.
  14. func NewCheckXrayRunningJob() *CheckXrayRunningJob {
  15. return new(CheckXrayRunningJob)
  16. }
  17. // Run checks if Xray has crashed and restarts it after confirming it's down for 2 consecutive checks.
  18. func (j *CheckXrayRunningJob) Run() {
  19. if !j.xrayService.DidXrayCrash() {
  20. j.checkTime = 0
  21. } else {
  22. j.checkTime++
  23. // only restart if it's down 2 times in a row
  24. if j.checkTime > 1 {
  25. err := j.xrayService.RestartXray(false)
  26. j.checkTime = 0
  27. if err != nil {
  28. logger.Error("Restart xray failed:", err)
  29. }
  30. }
  31. }
  32. }