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,22 +14,24 @@ class UpdateAllMirrorsWorker
def perform
return if Gitlab::Database.read_only?
scheduled = 0
with_lease do
scheduled = schedule_mirrors!
Gitlab::ApplicationContext.with_context({ user: nil, project: nil, namespace: nil }) do
scheduled = 0
with_lease do
scheduled = schedule_mirrors!
end
# If we didn't get the lease, or no updates were scheduled, exit early
break unless scheduled > 0
# Wait to give some jobs a chance to complete
Kernel.sleep(RESCHEDULE_WAIT)
# If there's capacity left now (some jobs completed),
# reschedule this job to enqueue more work.
#
# This is in addition to the regular (cron-like) scheduling of this job.
UpdateAllMirrorsWorker.perform_async if Gitlab::Mirror.reschedule_immediately?
end
# If we didn't get the lease, or no updates were scheduled, exit early
return unless scheduled > 0
# Wait to give some jobs a chance to complete
Kernel.sleep(RESCHEDULE_WAIT)
# If there's capacity left now (some jobs completed),
# reschedule this job to enqueue more work.
#
# This is in addition to the regular (cron-like) scheduling of this job.
UpdateAllMirrorsWorker.perform_async if Gitlab::Mirror.reschedule_immediately?
end
# rubocop: disable CodeReuse/ActiveRecord
......
......@@ -27,6 +27,26 @@ describe UpdateAllMirrorsWorker do
worker.perform
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
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