Commit 236d14b6 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'mmj-use-related-class' into 'master'

Pass `caller_id` to `AuthorizedProjectUpdate::UserRefreshFromReplicaWorker`

See merge request gitlab-org/gitlab!65313
parents d347c606 4d6a7d37
...@@ -20,13 +20,25 @@ class UserProjectAccessChangedService ...@@ -20,13 +20,25 @@ class UserProjectAccessChangedService
if priority == HIGH_PRIORITY if priority == HIGH_PRIORITY
AuthorizedProjectsWorker.bulk_perform_async(bulk_args) # rubocop:disable Scalability/BulkPerformWithContext AuthorizedProjectsWorker.bulk_perform_async(bulk_args) # rubocop:disable Scalability/BulkPerformWithContext
else else
with_related_class_context do
# We wrap the execution in `with_related_class_context`so as to obtain
# the location of the original caller
# in jobs enqueued from within `AuthorizedProjectUpdate::UserRefreshFromReplicaWorker`
AuthorizedProjectUpdate::UserRefreshFromReplicaWorker.bulk_perform_in( # rubocop:disable Scalability/BulkPerformWithContext AuthorizedProjectUpdate::UserRefreshFromReplicaWorker.bulk_perform_in( # rubocop:disable Scalability/BulkPerformWithContext
DELAY, bulk_args, batch_size: 100, batch_delay: 30.seconds) DELAY, bulk_args, batch_size: 100, batch_delay: 30.seconds)
end end
end end
end
::Gitlab::Database::LoadBalancing::Sticking.bulk_stick(:user, @user_ids) ::Gitlab::Database::LoadBalancing::Sticking.bulk_stick(:user, @user_ids)
result result
end end
private
def with_related_class_context(&block)
current_caller_id = Gitlab::ApplicationContext.current_context_attribute('meta.caller_id').presence
Gitlab::ApplicationContext.with_context(related_class: current_caller_id, &block)
end
end end
...@@ -41,15 +41,9 @@ module AuthorizedProjectUpdate ...@@ -41,15 +41,9 @@ module AuthorizedProjectUpdate
end end
def enqueue_project_authorizations_refresh(user) def enqueue_project_authorizations_refresh(user)
with_context(user: user, related_class: current_caller_id) do with_context(user: user) do
AuthorizedProjectUpdate::UserRefreshWithLowUrgencyWorker.perform_async(user.id) AuthorizedProjectUpdate::UserRefreshWithLowUrgencyWorker.perform_async(user.id)
end end
end end
# We use this so that we can obtain the details of the original caller
# in the enqueued `AuthorizedProjectUpdate::UserRefreshWithLowUrgencyWorker` job.
def current_caller_id
Gitlab::ApplicationContext.current_context_attribute('meta.caller_id').presence
end
end end
end end
...@@ -30,6 +30,17 @@ RSpec.describe UserProjectAccessChangedService do ...@@ -30,6 +30,17 @@ RSpec.describe UserProjectAccessChangedService do
described_class.new([1, 2]).execute(blocking: false, described_class.new([1, 2]).execute(blocking: false,
priority: described_class::LOW_PRIORITY) priority: described_class::LOW_PRIORITY)
end end
it 'sets the current caller_id as related_class in the context of all the enqueued jobs' do
Gitlab::ApplicationContext.with_context(caller_id: 'Foo') do
described_class.new([1, 2]).execute(blocking: false,
priority: described_class::LOW_PRIORITY)
end
expect(AuthorizedProjectUpdate::UserRefreshFromReplicaWorker.jobs).to all(
include(Labkit::Context.log_key(:related_class) => 'Foo')
)
end
end end
context 'with load balancing enabled' do context 'with load balancing enabled' do
......
...@@ -34,30 +34,6 @@ RSpec.describe AuthorizedProjectUpdate::UserRefreshFromReplicaWorker do ...@@ -34,30 +34,6 @@ RSpec.describe AuthorizedProjectUpdate::UserRefreshFromReplicaWorker do
execute_worker execute_worker
end end
context 'setting `meta.caller_id` as `meta.related_class` in the context of the newly enqueued `UserRefreshWithLowUrgencyWorker` job' do
context 'when the `UserRefreshFromReplicaWorker` job has a `caller_id` set' do
it 'sets the same `caller_id` as `related_class`' do
expect(AuthorizedProjectUpdate::UserRefreshWithLowUrgencyWorker).to receive(:perform_async).with(user.id) do
expect(Gitlab::ApplicationContext.current).to include('meta.related_class' => 'Foo')
end
Gitlab::ApplicationContext.with_context(caller_id: 'Foo') do
execute_worker
end
end
end
context 'when the `UserRefreshFromReplicaWorker` job does not have a `caller_id` set' do
it 'does not set the value of `related_class`' do
expect(AuthorizedProjectUpdate::UserRefreshWithLowUrgencyWorker).to receive(:perform_async).with(user.id) do
expect(Gitlab::ApplicationContext.current).not_to include('meta.related_class')
end
execute_worker
end
end
end
end end
context 'when there are no additions or removals to be made to project authorizations for a specific user' do context 'when there are no additions or removals to be made to project authorizations for a specific user' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment