Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit 02d3873

Browse files
committed
sync: rollback if events have happened
If registry events have happened while performing a sync, then cancel this process altogether. This should fix situations like: 1. User pushes image:tag. 2. Background process executes sync (because the registry task has already been executed before 1 happened). 3. Sync process marks image:tag as pushed by portus. Signed-off-by: Miquel Sabaté Solà <[email protected]>
1 parent d6f9beb commit 02d3873

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

lib/portus/background/sync.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ def update_registry!(catalog)
7777
portus = User.find_by(username: "portus")
7878
Tag.where(repository_id: dangling_repos).find_each { |t| t.delete_and_update!(portus) }
7979
Repository.where(id: dangling_repos).find_each { |r| r.delete_and_update!(portus) }
80+
81+
# Check that no events have happened while doing all this.
82+
check_events!
83+
end
84+
85+
# Raises an ActiveRecord::Rollback exception if there are registry events
86+
# ready to be fetched.
87+
def check_events!
88+
return unless RegistryEvent.exists?(status: RegistryEvent.statuses[:fresh])
89+
raise ActiveRecord::Rollback
8090
end
8191
end
8292
end

spec/api/grape_api/v1/vulnerabilities_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "rails_helper"
44

5-
describe API::V1::Vulnerabilities, focus: true do
5+
describe API::V1::Vulnerabilities do
66
let!(:admin) { create(:admin) }
77
let!(:token) { create(:application_token, user: admin) }
88
let!(:public_namespace) do

spec/lib/portus/background/sync_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,23 @@ def update_registry!(catalog)
187187
expect(activities.last.key).to eq "repository.delete"
188188
end
189189
end
190+
191+
it "rolls back if an event happened while syncing" do
192+
registry = create(:registry)
193+
owner = create(:user)
194+
repo = create(:repository, name: "repo", namespace: registry.global_namespace)
195+
create(:tag, name: "latest", author: owner, repository: repo)
196+
197+
# Event!
198+
RegistryEvent.create!(event_id: "id", data: "", status: RegistryEvent.statuses[:fresh])
199+
200+
allow_any_instance_of(::Portus::RegistryClient).to receive(:manifest).and_return(["", ""])
201+
sync = SyncMock.new
202+
203+
expect do
204+
sync.update_registry!([{ "name" => "repo", "tags" => ["0.1"] }])
205+
end.to raise_error(ActiveRecord::Rollback)
206+
end
190207
end
191208

192209
describe "#enabled?" do

0 commit comments

Comments
 (0)