Я бы предложил сделать это на рабочем месте, и вы можете использовать, возможно, переменную модели Config или Redis cahce, чтобы настроить вашу модель на готовность использовать новый индекс после его продвижения.
class UpdateProductIndexWorker
include Sidekiq::Worker
sidekiq_options retry: 0
def peform
#patch your product model for this job
class Product
searchkick text_middle: [:name, :item_volume]
end
# track status of reindex
Searchkick.redis = Redis.new
new_index = Product.reindex(async: true)
index_name = new_index['index_name']
loop do
break if Searchkick.reindex_status(index_name)[:completed]
sleep 60 # or however long you want to interval check
end
# You'll need to tell your app somehow to use new index
# You could use perhaps a Config model which could be something
# you could store in the database for exmaple:
# Config.create(name: 'use_new_products_index', state: true)
# or use redis assuming it's your Rails cache
Rails.cache.write('use_new_product_index', true, expires_in: 1.year)
# now you can just use the new index
Product.searchkick_index.promote(index_name)
end
end
Обновите вашу модель, чтобы использовать новый индекс, когда она будет готова.
class Product < ApplicationRecord
def self.use_new_index?
# Config.find_by(name: 'use_new_products_index').try(:state)
# or use Rails/redis cachea
Rails.cache.read('use_new_product_index')
end
# use the config data to make the model backward compatable until
# the new index is ready. Later you'll remove this in a cleanup deploy.
if Product.use_new_index?
searchkick text_middle: [:name, :item_volume]
else
searchkick
end
end