cleanup_action.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class BackgroundServices
  3. class Service
  4. class ProcessDelayedJobs
  5. class CleanupAction
  6. extend ::Mixin::StartFinishLogger
  7. attr_reader :job
  8. def self.cleanup_delayed_jobs(after)
  9. log_start_finish(:info, "Cleanup of left over locked delayed jobs #{after}") do
  10. scope(after).each do |job|
  11. log_start_finish(:info, "Checking left over delayed job #{job.inspect}") do
  12. CleanupAction.new(job).cleanup
  13. end
  14. end
  15. end
  16. end
  17. private_class_method def self.scope(after)
  18. ::Delayed::Job.where(updated_at: ...after).where.not(locked_at: nil)
  19. end
  20. def initialize(job)
  21. @job = job
  22. end
  23. def cleanup
  24. return if job.locked_at.blank?
  25. if reschedulable?
  26. job.unlock
  27. job.save
  28. else
  29. job.destroy
  30. end
  31. Rails.logger.warn "#{action_name} locked delayed job: #{job_name}"
  32. end
  33. private
  34. def action_name
  35. reschedulable? ? 'Rescheduling' : 'Destroyed'
  36. end
  37. def job_name
  38. job_name = job.name
  39. if (object_id = job.payload_object.try(:object).try(:id))
  40. job_name += " (id: #{object_id})"
  41. end
  42. if job.payload_object.respond_to?(:args)
  43. job_name += " - ARGS: #{payload_object.args.inspect}"
  44. end
  45. job_name
  46. end
  47. def reschedulable?
  48. @reschedulable ||= job.payload_object.try(:object).try(:reschedule?, job)
  49. end
  50. end
  51. end
  52. end
  53. end