This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
background: added registry event handling
From now on, registry notifications (image got pushed, image deleted) will be handled entirely in the background. Portus itself will only create the event on the DB, and it will expect for it to be handled automatically by the background process. Fixes #940 See #1353 and #1526 Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
- Loading branch information
Showing
13 changed files
with
316 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
db/migrate/20171213093923_add_status_and_data_to_registry_event.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddStatusAndDataToRegistryEvent < ActiveRecord::Migration | ||
def change | ||
add_column :registry_events, :status, :integer, default: RegistryEvent.statuses[:done] | ||
add_column :registry_events, :data, :text | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20171213094038_remove_repository_and_tag_from_registry_event.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class RemoveRepositoryAndTagFromRegistryEvent < ActiveRecord::Migration | ||
def change | ||
remove_column :registry_events, :repository, :string | ||
remove_column :registry_events, :tag, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Portus | ||
module Background | ||
# Registry represents a task that syncs pending registry events into Portus. | ||
class Registry | ||
# Returns how many seconds has to pass between each loop for this | ||
# background service. | ||
def sleep_value | ||
2 | ||
end | ||
|
||
# Returns always true because this does not depend on some configuration | ||
# options and because the `execute!` method will deal with valid rows | ||
# already. | ||
def work? | ||
true | ||
end | ||
|
||
def execute! | ||
RegistryEvent.where(status: RegistryEvent.statuses[:fresh]).find_each do |e| | ||
data = JSON.parse(e.data) | ||
RegistryEvent.handle!(data) | ||
end | ||
end | ||
|
||
def to_s | ||
"Registry events" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
require "portus/background/registry" | ||
|
||
describe ::Portus::Background::Registry do | ||
describe "#sleep_value" do | ||
it "returns always 2" do | ||
expect(subject.sleep_value).to eq 2 | ||
end | ||
end | ||
|
||
describe "#work?" do | ||
it "always return true" do | ||
expect(subject.work?).to be_truthy | ||
end | ||
end | ||
|
||
describe "#execute!" do | ||
let(:delete) { ::Portus::Fixtures::RegistryEvent::DELETE.dup } | ||
let(:version23) { ::Portus::Fixtures::RegistryEvent::VERSION23.dup } | ||
|
||
it "calls RegistryEvent.handle! with the given events" do | ||
data = '{ "key": "value" }'.to_json | ||
RegistryEvent.create!(event_id: 1, data: data, status: RegistryEvent.statuses[:fresh]) | ||
|
||
count = 0 | ||
parsed = JSON.parse(data) | ||
allow(RegistryEvent).to receive(:handle!).with(parsed) { count += 1 } | ||
subject.execute! | ||
expect(count).to eq 1 | ||
end | ||
|
||
it "does nothing if no events happened" do | ||
RegistryEvent.create!(event_id: 1, status: RegistryEvent.statuses[:done]) | ||
|
||
count = 0 | ||
allow(RegistryEvent).to receive(:handle!) { count += 1 } | ||
subject.execute! | ||
expect(count).to eq 0 | ||
end | ||
|
||
it "performs well in an overall example" do | ||
data = { "events" => [delete, version23] } | ||
|
||
# All notifications are properly registered. | ||
::Portus::RegistryNotification.process!(data) | ||
expect(RegistryEvent.count).to eq 2 | ||
expect(RegistryEvent.all.all? { |e| e.status == "fresh" }).to be_truthy | ||
|
||
# And finally it processes the notifications. | ||
expect(Webhook).to receive(:handle_delete_event).with(delete) | ||
expect(Repository).to receive(:handle_delete_event).with(delete) | ||
expect(Webhook).to receive(:handle_push_event).with(version23) | ||
expect(Repository).to receive(:handle_push_event).with(version23) | ||
subject.execute! | ||
|
||
expect(RegistryEvent.all.all? { |e| e.status == "done" }).to be_truthy | ||
end | ||
end | ||
|
||
describe "#to_s" do | ||
it "works" do | ||
expect(subject.to_s).to eq "Registry events" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.