Commit b9d3f9a9 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Fixed key deletion 🐛

parent ae843e55
...@@ -57,7 +57,7 @@ class Key < ActiveRecord::Base ...@@ -57,7 +57,7 @@ class Key < ActiveRecord::Base
end end
def add_to_shell def add_to_shell
Gitlab::Geo.notify_ssh_key_change(id, :create) if Gitlab::Geo.primary? Gitlab::Geo.notify_key_change(id, key, :create) if Gitlab::Geo.primary?
GitlabShellWorker.perform_async( GitlabShellWorker.perform_async(
:add_key, :add_key,
shell_id, shell_id,
...@@ -74,7 +74,7 @@ class Key < ActiveRecord::Base ...@@ -74,7 +74,7 @@ class Key < ActiveRecord::Base
end end
def remove_from_shell def remove_from_shell
Gitlab::Geo.notify_ssh_key_change(id, :delete) if Gitlab::Geo.primary? Gitlab::Geo.notify_key_change(id, key, :delete) if Gitlab::Geo.primary?
GitlabShellWorker.perform_async( GitlabShellWorker.perform_async(
:remove_key, :remove_key,
shell_id, shell_id,
......
module Geo module Geo
class NotifyKeyChangeService < BaseNotify class NotifyKeyChangeService < BaseNotify
def initialize(key_id, change) def initialize(key_id, key, action)
@id = key_id @id = key_id
@action = change @key = key
@action = action
end end
def execute def execute
key_change = { 'id' => @id, 'action' => @action } key_change = { 'id' => @id, 'key' => @key, 'action' => @action }
content = { key_change: key_change }.to_json content = { key_change: key_change }.to_json
::Gitlab::Geo.secondary_nodes.each do |node| ::Gitlab::Geo.secondary_nodes.each do |node|
......
module Geo module Geo
class ScheduleKeyChangeService class ScheduleKeyChangeService
attr_reader :id, :action attr_reader :id, :key, :action
def initialize(key_change) def initialize(key_change)
@id = key_change['id'] @id = key_change['id']
@key = key_change['key']
@action = key_change['action'] @action = key_change['action']
end end
def execute def execute
GeoKeyRefreshWorker.perform_async(@id, @action) GeoKeyRefreshWorker.perform_async(@id, @key, @action)
end end
end end
end end
...@@ -7,8 +7,8 @@ class GeoKeyChangeNotifyWorker ...@@ -7,8 +7,8 @@ class GeoKeyChangeNotifyWorker
count <= 30 ? linear_backoff_strategy(count) : geometric_backoff_strategy(count) count <= 30 ? linear_backoff_strategy(count) : geometric_backoff_strategy(count)
end end
def perform(key_id, change) def perform(key_id, key, action)
Geo::NotifyKeyChangeService.new(key_id, change).execute Geo::NotifyKeyChangeService.new(key_id, key, action).execute
end end
private private
......
...@@ -7,7 +7,7 @@ class GeoKeyRefreshWorker ...@@ -7,7 +7,7 @@ class GeoKeyRefreshWorker
count <= 30 ? linear_backoff_strategy(count) : geometric_backoff_strategy(count) count <= 30 ? linear_backoff_strategy(count) : geometric_backoff_strategy(count)
end end
def perform(key_id, action) def perform(key_id, key, action)
action = action.to_sym action = action.to_sym
case action case action
...@@ -18,7 +18,7 @@ class GeoKeyRefreshWorker ...@@ -18,7 +18,7 @@ class GeoKeyRefreshWorker
when :delete when :delete
# we are physically removing the key after model is removed # we are physically removing the key after model is removed
# so we must reconstruct ids to schedule removal # so we must reconstruct ids to schedule removal
key = Key.new(id: key_id) key = Key.new(id: key_id, key: key)
key.remove_from_shell key.remove_from_shell
else else
fail "Invalid action: #{action}" fail "Invalid action: #{action}"
......
...@@ -42,8 +42,8 @@ module Gitlab ...@@ -42,8 +42,8 @@ module Gitlab
::Geo::EnqueueWikiUpdateService.new(project).execute ::Geo::EnqueueWikiUpdateService.new(project).execute
end end
def self.notify_ssh_key_change(key_id, change) def self.notify_key_change(key_id, key, action)
GeoKeyChangeNotifyWorker.perform_async(key_id, change) GeoKeyChangeNotifyWorker.perform_async(key_id, key, action)
end end
def self.bulk_notify_job def self.bulk_notify_job
......
...@@ -79,4 +79,15 @@ describe Gitlab::Geo, lib: true do ...@@ -79,4 +79,15 @@ describe Gitlab::Geo, lib: true do
described_class.notify_project_update(project) described_class.notify_project_update(project)
end end
end end
describe 'notify_ssh_key_change' do
let(:key) { FactoryGirl.build(:key) }
it 'schedule async notification' do
expect(GeoKeyChangeNotifyWorker).to receive(:perform_async).and_call_original
expect_any_instance_of(GeoKeyChangeNotifyWorker).to receive(:perform)
described_class.notify_key_change(key.id, key, 'create')
end
end
end end
require 'spec_helper' require 'spec_helper'
describe GeoKeyRefreshWorker do describe GeoKeyRefreshWorker do
subject(:key_create) { described_class.new.perform(key.id, 'create') } subject(:key_create) { described_class.new.perform(key.id, key.key, 'create') }
subject(:key_delete) { described_class.new.perform(key.id, 'delete') } subject(:key_delete) { described_class.new.perform(key.id, key.key, 'delete') }
let(:key) { FactoryGirl.create(:key) } let(:key) { FactoryGirl.create(:key) }
context 'key creation' do context 'key creation' do
...@@ -15,7 +15,7 @@ describe GeoKeyRefreshWorker do ...@@ -15,7 +15,7 @@ describe GeoKeyRefreshWorker do
context 'key removal' do context 'key removal' do
it 'removes key from the shell' do it 'removes key from the shell' do
expect(Key).to receive(:new).with(id: key.id) { key } expect(Key).to receive(:new).with(id: key.id, key: key.key) { key }
expect(key).to receive(:remove_from_shell) expect(key).to receive(:remove_from_shell)
expect { key_delete }.not_to raise_error expect { key_delete }.not_to raise_error
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