Skip to content

Run UserRefreshFromReplicaWorker on the replica database

This should be done after #333360 (closed) is introduced.

  • Read data from replica.
  • Check if the user actually needs a refresh.
  • If they do, enqueue a completely new UserRefreshWithLowUrgencyWorker job for this user that would read from primary.

This is similar to what we did with UserRefreshOverRangeWorker with #292443 (comment 592724801)

The code changes would look like

module AuthorizedProjectUpdate
  class UserRefreshFromReplicaWorker
    include ApplicationWorker

    feature_category :authentication_and_authorization
    urgency :low
    queue_namespace :authorized_project_update
    deduplicate :until_executing, including_scheduled: true
    data_consistency :delayed

    idempotent!

    def perform(user_id)
      user = User.find_by(id: user_id)
      return unless user

      enqueue_project_authorizations_refresh(user) if project_authorizations_needs_refresh?(user)
    end

    private

    def project_authorizations_needs_refresh?(user)
      AuthorizedProjectUpdate::FindRecordsDueForRefreshService.new(user).needs_refresh?
    end

    def enqueue_project_authorizations_refresh(user)
      with_context(user: user) do
        AuthorizedProjectUpdate::UserRefreshWithLowUrgencyWorker.perform_async(user.id)
      end
    end
  end
end

Also remember to make sure that from all the places that UserRefreshFromReplicaWorker is enqueued, it is enqueued with a delay (else reading from replica will fail due to sync issues)

Edited by Manoj M J [OOO]