delayed_jobs_ensure_active_job_lock_removal.rb 1005 B

12345678910111213141516171819202122232425262728293031
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'delayed_job'
  3. module Delayed
  4. class Job < ::ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
  5. after_destroy :remove_active_job_lock
  6. def remove_active_job_lock
  7. # only ActiveJob Delayed::Jobs can have a lock
  8. return if !payload_object.is_a?(::ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper)
  9. # deserialize ActiveJob and load it's arguments to generate the lock_key
  10. active_job = ::ActiveJob::Base.deserialize(payload_object.job_data)
  11. # ActiveJob that is not an HasActiveJobLock has no lock
  12. return if !active_job.is_a?(HasActiveJobLock)
  13. begin
  14. active_job.arguments = ::ActiveJob::Arguments.deserialize(active_job.instance_variable_get(:@serialized_arguments))
  15. rescue => e
  16. active_job.arguments = nil
  17. Rails.logger.error e
  18. end
  19. # remove possible lock
  20. active_job.release_active_job_lock!
  21. end
  22. end
  23. end