Commit c83308d7 authored by Etienne Baqué's avatar Etienne Baqué

Merge branch '330325-improve-logs' into 'master'

Cleanup policies worker, improve logs

See merge request gitlab-org/gitlab!66182
parents e02439db 0ee8d9f9
...@@ -63,16 +63,23 @@ module ContainerExpirationPolicies ...@@ -63,16 +63,23 @@ module ContainerExpirationPolicies
def container_repository def container_repository
strong_memoize(:container_repository) do strong_memoize(:container_repository) do
ContainerRepository.transaction do ContainerRepository.transaction do
# We need a lock to prevent two workers from picking up the same row repository = next_container_repository
container_repository = next_container_repository
container_repository&.tap(&:cleanup_ongoing!) repository&.tap do |repo|
log_info(
project_id: repo.project_id,
container_repository_id: repo.id
)
repo.cleanup_ongoing!
end
end end
end end
end end
def next_container_repository def next_container_repository
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
# We need a lock to prevent two workers from picking up the same row
next_one_requiring = ContainerRepository.requiring_cleanup next_one_requiring = ContainerRepository.requiring_cleanup
.order(:expiration_policy_cleanup_status, :expiration_policy_started_at) .order(:expiration_policy_cleanup_status, :expiration_policy_started_at)
.limit(1) .limit(1)
......
...@@ -25,6 +25,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do ...@@ -25,6 +25,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do
expect(ContainerExpirationPolicies::CleanupService) expect(ContainerExpirationPolicies::CleanupService)
.to receive(:new).with(repository).and_return(double(execute: service_response)) .to receive(:new).with(repository).and_return(double(execute: service_response))
expect_log_extra_metadata(service_response: service_response) expect_log_extra_metadata(service_response: service_response)
expect_log_info(project_id: project.id, container_repository_id: repository.id)
subject subject
end end
...@@ -35,6 +36,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do ...@@ -35,6 +36,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do
expect(ContainerExpirationPolicies::CleanupService) expect(ContainerExpirationPolicies::CleanupService)
.to receive(:new).with(repository).and_return(double(execute: service_response)) .to receive(:new).with(repository).and_return(double(execute: service_response))
expect_log_extra_metadata(service_response: service_response, cleanup_status: :unfinished) expect_log_extra_metadata(service_response: service_response, cleanup_status: :unfinished)
expect_log_info(project_id: project.id, container_repository_id: repository.id)
subject subject
end end
...@@ -45,6 +47,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do ...@@ -45,6 +47,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do
expect(ContainerExpirationPolicies::CleanupService) expect(ContainerExpirationPolicies::CleanupService)
.to receive(:new).with(repository).and_return(double(execute: service_response)) .to receive(:new).with(repository).and_return(double(execute: service_response))
expect_log_extra_metadata(service_response: service_response, cleanup_status: :unfinished, truncated: true) expect_log_extra_metadata(service_response: service_response, cleanup_status: :unfinished, truncated: true)
expect_log_info(project_id: project.id, container_repository_id: repository.id)
subject subject
end end
...@@ -65,6 +68,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do ...@@ -65,6 +68,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do
expect(ContainerExpirationPolicies::CleanupService) expect(ContainerExpirationPolicies::CleanupService)
.to receive(:new).with(repository).and_return(double(execute: service_response)) .to receive(:new).with(repository).and_return(double(execute: service_response))
expect_log_extra_metadata(service_response: service_response, cleanup_status: :unfinished, truncated: truncated) expect_log_extra_metadata(service_response: service_response, cleanup_status: :unfinished, truncated: truncated)
expect_log_info(project_id: project.id, container_repository_id: repository.id)
subject subject
end end
...@@ -78,6 +82,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do ...@@ -78,6 +82,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do
expect(ContainerExpirationPolicies::CleanupService) expect(ContainerExpirationPolicies::CleanupService)
.to receive(:new).with(repository).and_return(double(execute: service_response)) .to receive(:new).with(repository).and_return(double(execute: service_response))
expect_log_extra_metadata(service_response: service_response, cleanup_status: :error) expect_log_extra_metadata(service_response: service_response, cleanup_status: :error)
expect_log_info(project_id: project.id, container_repository_id: repository.id)
subject subject
end end
...@@ -361,6 +366,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do ...@@ -361,6 +366,7 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do
expect(ContainerExpirationPolicies::CleanupService) expect(ContainerExpirationPolicies::CleanupService)
.to receive(:new).with(repository).and_return(double(execute: service_response)) .to receive(:new).with(repository).and_return(double(execute: service_response))
expect_log_extra_metadata(service_response: service_response) expect_log_extra_metadata(service_response: service_response)
expect_log_info(project_id: project.id, container_repository_id: repository.id)
subject subject
end end
...@@ -396,6 +402,11 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do ...@@ -396,6 +402,11 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do
expect(worker).to receive(:log_extra_metadata_on_done).with(:cleanup_error_message, service_response.message) expect(worker).to receive(:log_extra_metadata_on_done).with(:cleanup_error_message, service_response.message)
end end
end end
def expect_log_info(structure)
expect(worker.logger)
.to receive(:info).with(worker.structured_payload(structure))
end
end end
describe '#remaining_work_count' do describe '#remaining_work_count' do
...@@ -446,6 +457,12 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do ...@@ -446,6 +457,12 @@ RSpec.describe ContainerExpirationPolicies::CleanupContainerRepositoryWorker do
end end
it { is_expected.to eq(0) } it { is_expected.to eq(0) }
it 'does not log a selected container' do
expect(worker).not_to receive(:log_info)
subject
end
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