import_job.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module MonitoringHelper
  3. class HealthChecker
  4. class ImportJob < Backend
  5. TIMEOUT = 5.minutes
  6. def run_health_check
  7. failed_imports
  8. stuck_imports
  9. end
  10. private
  11. def import_backends
  12. ::ImportJob.backends
  13. end
  14. def failed_imports
  15. import_backends.each { |backend| single_failed_import(backend) }
  16. end
  17. def failed_import_job(backend)
  18. ::ImportJob
  19. .where(name: backend, dry_run: false, finished_at: TIMEOUT.ago..)
  20. .first
  21. end
  22. def single_failed_import(backend)
  23. job = failed_import_job(backend)
  24. return if job.blank?
  25. return if !job.result.is_a?(Hash)
  26. error_message = job.result[:error]
  27. return if error_message.blank?
  28. response.issues.push "Failed to run import backend '#{backend}'. Cause: #{error_message}"
  29. end
  30. def stuck_imports
  31. import_backends.each { |backend| single_stuck_import(backend) }
  32. end
  33. def stuck_import_job(backend)
  34. ::ImportJob
  35. .where(name: backend, dry_run: false, finished_at: nil, updated_at: ..TIMEOUT.ago)
  36. .first
  37. end
  38. def single_stuck_import(backend)
  39. job = stuck_import_job(backend)
  40. return if job.blank?
  41. response.issues.push "Stuck import backend '#{backend}' detected. Last update: #{job.updated_at}"
  42. end
  43. end
  44. end
  45. end