upstream_availability.go 2.6 KB

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