check_xray_running_job.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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/v3/internal/eventbus"
  6. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  7. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  8. )
  9. // EventBus is set from web layer to publish events.
  10. var EventBus *eventbus.Bus
  11. // CheckXrayRunningJob monitors Xray process health and restarts it if it crashes.
  12. type CheckXrayRunningJob struct {
  13. xrayService service.XrayService
  14. checkTime int
  15. }
  16. // NewCheckXrayRunningJob creates a new Xray health check job instance.
  17. func NewCheckXrayRunningJob() *CheckXrayRunningJob {
  18. return new(CheckXrayRunningJob)
  19. }
  20. // Run checks if Xray has crashed and restarts it after confirming it's down for 2 consecutive checks.
  21. func (j *CheckXrayRunningJob) Run() {
  22. if !j.xrayService.DidXrayCrash() {
  23. j.checkTime = 0
  24. } else {
  25. j.checkTime++
  26. // only restart if it's down 2 times in a row
  27. if j.checkTime > 1 {
  28. err := j.xrayService.RestartXray(false)
  29. j.checkTime = 0
  30. if err != nil {
  31. logger.Error("Restart xray failed:", err)
  32. }
  33. }
  34. }
  35. }