search_index_job.rb 734 B

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