upstream_availability.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package cron
  2. import (
  3. "time"
  4. apiUpstream "github.com/0xJacky/Nginx-UI/api/upstream"
  5. "github.com/0xJacky/Nginx-UI/internal/upstream"
  6. "github.com/go-co-op/gocron/v2"
  7. "github.com/uozi-tech/cosy/logger"
  8. )
  9. // upstreamAvailabilityJob holds the job instance
  10. var upstreamAvailabilityJob gocron.Job
  11. // setupUpstreamAvailabilityJob initializes the upstream availability testing job
  12. func setupUpstreamAvailabilityJob(scheduler gocron.Scheduler) (gocron.Job, error) {
  13. job, err := scheduler.NewJob(
  14. gocron.DurationJob(30*time.Second),
  15. gocron.NewTask(executeUpstreamAvailabilityTest),
  16. gocron.WithSingletonMode(gocron.LimitModeWait),
  17. gocron.WithName("upstream_availability_test"),
  18. gocron.JobOption(gocron.WithStartImmediately()),
  19. )
  20. if err != nil {
  21. logger.Errorf("UpstreamAvailability Job: Err: %v\n", err)
  22. return nil, err
  23. }
  24. upstreamAvailabilityJob = job
  25. logger.Info("Upstream availability testing job started with 30s interval")
  26. return job, nil
  27. }
  28. // executeUpstreamAvailabilityTest performs the upstream availability test
  29. func executeUpstreamAvailabilityTest() {
  30. service := upstream.GetUpstreamService()
  31. targetCount := service.GetTargetCount()
  32. if targetCount == 0 {
  33. // logger.Debug("No upstream targets to test")
  34. return
  35. }
  36. // Check if we should skip this test due to active WebSocket connections
  37. // (WebSocket connections trigger more frequent checks)
  38. if hasActiveWebSocketConnections() {
  39. // logger.Debug("Skipping scheduled test due to active WebSocket connections")
  40. return
  41. }
  42. // start := time.Now()
  43. // logger.Debug("Starting scheduled upstream availability test for", targetCount, "targets")
  44. // service.PerformAvailabilityTest()
  45. // duration := time.Since(start)
  46. // logger.Debug("Upstream availability test completed in", duration)
  47. }
  48. // hasActiveWebSocketConnections checks if there are active WebSocket connections
  49. func hasActiveWebSocketConnections() bool {
  50. return apiUpstream.HasActiveWebSocketConnections()
  51. }
  52. // RestartUpstreamAvailabilityJob restarts the upstream availability job
  53. func RestartUpstreamAvailabilityJob() error {
  54. logger.Info("Restarting upstream availability job...")
  55. // Remove existing job if it exists
  56. if upstreamAvailabilityJob != nil {
  57. err := s.RemoveJob(upstreamAvailabilityJob.ID())
  58. if err != nil {
  59. logger.Error("Failed to remove existing upstream availability job:", err)
  60. }
  61. upstreamAvailabilityJob = nil
  62. }
  63. // Create new job
  64. job, err := setupUpstreamAvailabilityJob(s)
  65. if err != nil {
  66. return err
  67. }
  68. upstreamAvailabilityJob = job
  69. logger.Info("Upstream availability job restarted successfully")
  70. return nil
  71. }