Commit b9d3f9a9 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Fixed key deletion 🐛

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