search_index_job.rb 813 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class SearchIndexJob < ApplicationJob
  3. include HasActiveJobLock
  4. low_priority
  5. retry_on StandardError, attempts: 20, wait: lambda { |executions|
  6. executions * 10.seconds
  7. }
  8. def lock_key
  9. # "SearchIndexJob/User/42"
  10. "#{self.class.name}/#{arguments[0]}/#{arguments[1]}"
  11. end
  12. def perform(object, o_id)
  13. @object = object
  14. @o_id = o_id
  15. record = @object.constantize.find_by(id: @o_id)
  16. return if !exists?(record)
  17. update_search_index(record)
  18. end
  19. def update_search_index(record)
  20. record.search_index_update_backend
  21. end
  22. private
  23. def exists?(record)
  24. return true if record
  25. Rails.logger.info "Can't index #{@object}.find_by(id: #{@o_id}), no such record found"
  26. false
  27. end
  28. end