Commit 4d6a7d37 authored by Manoj M J's avatar Manoj M J

Pass proper `caller_id` to `UserRefreshFromReplicaWorker`

Pass proper `caller_id` to `UserRefreshFromReplicaWorker` which can be
further used in
`UserRefreshWithLowUrgencyWorker `
parent 10700efe
......@@ -20,8 +20,13 @@ class UserProjectAccessChangedService
if priority == HIGH_PRIORITY
AuthorizedProjectsWorker.bulk_perform_async(bulk_args) # rubocop:disable Scalability/BulkPerformWithContext
else
AuthorizedProjectUpdate::UserRefreshFromReplicaWorker.bulk_perform_in( # rubocop:disable Scalability/BulkPerformWithContext
DELAY, bulk_args, batch_size: 100, batch_delay: 30.seconds)
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
DELAY, bulk_args, batch_size: 100, batch_delay: 30.seconds)
end
end
end
......@@ -29,4 +34,11 @@ class UserProjectAccessChangedService
result
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
......@@ -41,15 +41,9 @@ module AuthorizedProjectUpdate
end
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)
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
......@@ -30,6 +30,17 @@ RSpec.describe UserProjectAccessChangedService do
described_class.new([1, 2]).execute(blocking: false,
priority: described_class::LOW_PRIORITY)
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
context 'with load balancing enabled' do
......
......@@ -34,30 +34,6 @@ RSpec.describe AuthorizedProjectUpdate::UserRefreshFromReplicaWorker do
execute_worker
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
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