Commit 1d40aa40 authored by David Fernandez's avatar David Fernandez

Fix the `requiring_cleanup` scope

We need to exclude container repositories which cleanups have already
been executed for the current expiration policy execution.

Changelog: fixed
parent 54e802e8
...@@ -42,16 +42,15 @@ class ContainerRepository < ApplicationRecord ...@@ -42,16 +42,15 @@ class ContainerRepository < ApplicationRecord
end end
def self.with_enabled_policy def self.with_enabled_policy
joins("INNER JOIN container_expiration_policies ON container_repositories.project_id = container_expiration_policies.project_id") joins('INNER JOIN container_expiration_policies ON container_repositories.project_id = container_expiration_policies.project_id')
.where(container_expiration_policies: { enabled: true }) .where(container_expiration_policies: { enabled: true })
end end
def self.requiring_cleanup def self.requiring_cleanup
where( with_enabled_policy
container_repositories: { expiration_policy_cleanup_status: REQUIRING_CLEANUP_STATUSES }, .where(container_repositories: { expiration_policy_cleanup_status: REQUIRING_CLEANUP_STATUSES })
project_id: ::ContainerExpirationPolicy.runnable_schedules .where('container_repositories.expiration_policy_started_at IS NULL OR container_repositories.expiration_policy_started_at < container_expiration_policies.next_run_at')
.select(:project_id) .where('container_expiration_policies.next_run_at < ?', Time.zone.now)
)
end end
def self.with_unfinished_cleanup def self.with_unfinished_cleanup
......
...@@ -423,6 +423,14 @@ RSpec.describe ContainerRepository do ...@@ -423,6 +423,14 @@ RSpec.describe ContainerRepository do
it { is_expected.to eq([repository]) } it { is_expected.to eq([repository]) }
end end
context 'with repository cleanup started at after policy next run at' do
before do
repository.update!(expiration_policy_started_at: policy.next_run_at + 5.minutes)
end
it { is_expected.to eq([]) }
end
end end
describe '.with_unfinished_cleanup' do describe '.with_unfinished_cleanup' 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