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

Commit

Permalink
sync: rollback if events have happened
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mssola committed Feb 12, 2018
1 parent d6f9beb commit 02d3873
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/portus/background/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ def update_registry!(catalog)
portus = User.find_by(username: "portus")
Tag.where(repository_id: dangling_repos).find_each { |t| t.delete_and_update!(portus) }
Repository.where(id: dangling_repos).find_each { |r| r.delete_and_update!(portus) }

# Check that no events have happened while doing all this.
check_events!
end

# Raises an ActiveRecord::Rollback exception if there are registry events
# ready to be fetched.
def check_events!
return unless RegistryEvent.exists?(status: RegistryEvent.statuses[:fresh])
raise ActiveRecord::Rollback
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/api/grape_api/v1/vulnerabilities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

describe API::V1::Vulnerabilities, focus: true do
describe API::V1::Vulnerabilities do
let!(:admin) { create(:admin) }
let!(:token) { create(:application_token, user: admin) }
let!(:public_namespace) do
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/portus/background/sync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ def update_registry!(catalog)
expect(activities.last.key).to eq "repository.delete"
end
end

it "rolls back if an event happened while syncing" do
registry = create(:registry)
owner = create(:user)
repo = create(:repository, name: "repo", namespace: registry.global_namespace)
create(:tag, name: "latest", author: owner, repository: repo)

# Event!
RegistryEvent.create!(event_id: "id", data: "", status: RegistryEvent.statuses[:fresh])

allow_any_instance_of(::Portus::RegistryClient).to receive(:manifest).and_return(["", ""])
sync = SyncMock.new

expect do
sync.update_registry!([{ "name" => "repo", "tags" => ["0.1"] }])
end.to raise_error(ActiveRecord::Rollback)
end
end

describe "#enabled?" do
Expand Down

0 comments on commit 02d3873

Please sign in to comment.