Commit bb4e47a8 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Clear context when scheduling mirroring jobs

The `UpdateAllMirrorsWorker` could get scheduled from a request (from
a webhook) for a certain project.

This means that it would get the pointing to the project the request
was performed for. But since the `UpdateAllMirrorsWorker` could
schedule updates for other projects, we need to clear that metadata so
it doesn't propagate to those jobs that are being scheduled.
parent af5e68c8
...@@ -14,13 +14,14 @@ class UpdateAllMirrorsWorker ...@@ -14,13 +14,14 @@ class UpdateAllMirrorsWorker
def perform def perform
return if Gitlab::Database.read_only? return if Gitlab::Database.read_only?
Gitlab::ApplicationContext.with_context({ user: nil, project: nil, namespace: nil }) do
scheduled = 0 scheduled = 0
with_lease do with_lease do
scheduled = schedule_mirrors! scheduled = schedule_mirrors!
end end
# If we didn't get the lease, or no updates were scheduled, exit early # If we didn't get the lease, or no updates were scheduled, exit early
return unless scheduled > 0 break unless scheduled > 0
# Wait to give some jobs a chance to complete # Wait to give some jobs a chance to complete
Kernel.sleep(RESCHEDULE_WAIT) Kernel.sleep(RESCHEDULE_WAIT)
...@@ -31,6 +32,7 @@ class UpdateAllMirrorsWorker ...@@ -31,6 +32,7 @@ class UpdateAllMirrorsWorker
# This is in addition to the regular (cron-like) scheduling of this job. # This is in addition to the regular (cron-like) scheduling of this job.
UpdateAllMirrorsWorker.perform_async if Gitlab::Mirror.reschedule_immediately? UpdateAllMirrorsWorker.perform_async if Gitlab::Mirror.reschedule_immediately?
end end
end
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def schedule_mirrors! def schedule_mirrors!
......
...@@ -27,6 +27,26 @@ describe UpdateAllMirrorsWorker do ...@@ -27,6 +27,26 @@ describe UpdateAllMirrorsWorker do
worker.perform worker.perform
end end
it 'removes metadata except correlation_id from the application context before scheduling mirrors' do
inner_context = nil
outer_context = nil
Gitlab::ApplicationContext.with_context(project: build_stubbed(:project)) do
outer_context = Labkit::Context.current.to_h
expect(worker).to receive(:schedule_mirrors!) do
inner_context = Labkit::Context.current.to_h
# `schedule_mirrors!` needs to return an integer.
0
end
end
worker.perform
expect(inner_context).to eq(outer_context.slice('correlation_id'))
end
it 'schedules mirrors' do it 'schedules mirrors' do
expect(worker).to receive(:schedule_mirrors!).and_call_original expect(worker).to receive(:schedule_mirrors!).and_call_original
......
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