2019-01-15 12:32:14 +00:00
|
|
|
class SearchIndexJob < ApplicationJob
|
2019-11-13 07:03:47 +00:00
|
|
|
include HasActiveJobLock
|
2019-01-15 12:32:14 +00:00
|
|
|
|
2020-03-02 09:33:37 +00:00
|
|
|
low_priority
|
|
|
|
|
2019-05-23 12:03:42 +00:00
|
|
|
retry_on StandardError, attempts: 20, wait: lambda { |executions|
|
|
|
|
executions * 10.seconds
|
|
|
|
}
|
2019-01-15 12:32:14 +00:00
|
|
|
|
2019-11-13 07:03:47 +00:00
|
|
|
def lock_key
|
2020-03-02 09:33:37 +00:00
|
|
|
# "SearchIndexJob/User/42"
|
|
|
|
"#{self.class.name}/#{arguments[0]}/#{arguments[1]}"
|
2019-11-13 07:03:47 +00:00
|
|
|
end
|
|
|
|
|
2020-03-02 09:33:37 +00:00
|
|
|
def perform(object, o_id)
|
2017-01-31 17:13:45 +00:00
|
|
|
@object = object
|
|
|
|
@o_id = o_id
|
|
|
|
|
2021-01-19 11:07:42 +00:00
|
|
|
record = @object.constantize.find_by(id: @o_id)
|
2017-01-31 17:13:45 +00:00
|
|
|
return if !exists?(record)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2020-03-02 09:33:37 +00:00
|
|
|
update_search_index(record)
|
|
|
|
end
|
2020-02-18 14:49:52 +00:00
|
|
|
|
2020-03-02 09:33:37 +00:00
|
|
|
def update_search_index(record)
|
|
|
|
record.search_index_update_backend
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def exists?(record)
|
|
|
|
return true if record
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-01-19 11:07:42 +00:00
|
|
|
Rails.logger.info "Can't index #{@object}.find_by(id: #{@o_id}), no such record found"
|
2017-01-31 17:13:45 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|