Commit 5454ece3 authored by Dmytro Zaporozhets's avatar Dmytro Zaporozhets

Merge branch 'bvl-add-context-to-containter-expiration-worker' into 'master'

Add context to the ContainerExpirationWorker

See merge request gitlab-org/gitlab!24701
parents 61fb75f7 28cc0fc9
......@@ -14,7 +14,7 @@ class ContainerExpirationPolicy < ApplicationRecord
validates :keep_n, inclusion: { in: ->(_) { self.keep_n_options.keys } }, allow_nil: true
scope :active, -> { where(enabled: true) }
scope :preloaded, -> { preload(:project) }
scope :preloaded, -> { preload(project: [:route]) }
def self.keep_n_options
{
......
......@@ -60,6 +60,6 @@ module WorkerContext
end
def with_context(context, &block)
Gitlab::ApplicationContext.new(context).use(&block)
Gitlab::ApplicationContext.new(context).use { yield(**context) }
end
end
......@@ -2,15 +2,17 @@
class ContainerExpirationPolicyWorker
include ApplicationWorker
include CronjobQueue # rubocop:disable Scalability/CronWorkerContext
include CronjobQueue
feature_category :container_registry
def perform
ContainerExpirationPolicy.runnable_schedules.preloaded.find_each do |container_expiration_policy|
ContainerExpirationPolicyService.new(
container_expiration_policy.project, container_expiration_policy.project.owner
).execute(container_expiration_policy)
with_context(project: container_expiration_policy.project,
user: container_expiration_policy.project.owner) do |project:, user:|
ContainerExpirationPolicyService.new(project, user)
.execute(container_expiration_policy)
end
end
end
end
......@@ -49,9 +49,9 @@ RSpec.describe ContainerExpirationPolicy, type: :model do
it 'preloads the associations' do
subject
query = ActiveRecord::QueryRecorder.new { subject.each(&:project) }
query = ActiveRecord::QueryRecorder.new { subject.map(&:project).map(&:full_path) }
expect(query.count).to eq(2)
expect(query.count).to eq(3)
end
end
......
......@@ -106,5 +106,15 @@ describe WorkerContext do
expect(Labkit::Context.current.to_h).to include('meta.user' => 'jane-doe')
end
end
it 'yields the arguments to the block' do
a_user = build_stubbed(:user)
a_project = build_stubbed(:project)
worker.new.with_context(user: a_user, project: a_project) do |user:, project:|
expect(user).to eq(a_user)
expect(project).to eq(a_project)
end
end
end
end
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