~cytrogen/masto-fe

e263db276fdc05dab743400977606efe98f1e949 — Eugen Rochko 2 years ago 5694e24
Change indexing jobs to use database replica (#26692)

M app/workers/add_to_public_statuses_index_worker.rb => app/workers/add_to_public_statuses_index_worker.rb +8 -3
@@ 2,15 2,20 @@

class AddToPublicStatusesIndexWorker
  include Sidekiq::Worker
  include DatabaseHelper

  sidekiq_options queue: 'pull'

  def perform(account_id)
    account = Account.find(account_id)
    with_primary do
      @account = Account.find(account_id)
    end

    return unless account.indexable?
    return unless @account.indexable?

    account.add_to_public_statuses_index!
    with_read_replica do
      @account.add_to_public_statuses_index!
    end
  rescue ActiveRecord::RecordNotFound
    true
  end

M app/workers/scheduler/indexing_scheduler.rb => app/workers/scheduler/indexing_scheduler.rb +5 -1
@@ 3,6 3,7 @@
class Scheduler::IndexingScheduler
  include Sidekiq::Worker
  include Redisable
  include DatabaseHelper

  sidekiq_options retry: 0, lock: :until_executed, lock_ttl: 1.day.to_i



@@ 15,7 16,10 @@ class Scheduler::IndexingScheduler
    indexes.each do |type|
      with_redis do |redis|
        redis.sscan_each("chewy:queue:#{type.name}", count: SCAN_BATCH_SIZE).each_slice(IMPORT_BATCH_SIZE) do |ids|
          type.import!(ids)
          with_read_replica do
            type.import!(ids)
          end

          redis.srem("chewy:queue:#{type.name}", ids)
        end
      end