Skip to content

Commit

Permalink
orchestrate_destroy for provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswnl committed Jan 5, 2018
1 parent 2cfe78b commit 3649541
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/models/mixins/async_delete_mixin.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module AsyncDeleteMixin
extend ActiveSupport::Concern
included do
def self._queue_task(task, ids)
ids.each do |id|
def self._queue_task(task, ids, deliver_on = nil)
Array.wrap(ids).each do |id|
MiqQueue.put(
:class_name => name,
:instance_id => id,
:msg_timeout => 3600,
:method_name => task.to_s
:method_name => task.to_s,
:deliver_on => deliver_on,
)
end
end
Expand Down
19 changes: 19 additions & 0 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,23 @@ def refresh_ems(opts = {})
end
managers.flat_map { |manager| EmsRefresh.queue_refresh(manager, nil, opts) }
end

def self.destroy_queue(ids)
find(ids).each(&:orchestrate_destroy)
end

def destroy_queue
_log.info("Queuing destroy of provider: #{self.class.name} with id: #{id}")
self.class._queue_task(:orchestrate_destroy, id)
end

def orchestrate_destroy
return destroy if managers.empty?

_log.info("Queuing destroy of managers of provider: #{self.class.name} with id: #{id}")
managers.each(&:destroy_queue)

_log.info("Queuing destroy of provider: #{self.class.name} with id: #{id}")
self.class._queue_task(:orchestrate_destroy, id, 15.seconds.from_now)
end
end
34 changes: 33 additions & 1 deletion spec/models/provider_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe Provider do
let(:provider) { described_class.new }
let(:provider) { FactoryGirl.create(:provider) }

describe "#verify_ssl" do
context "when non set" do
Expand Down Expand Up @@ -61,4 +61,36 @@
expect(tenant.providers).to include(provider)
end
end

context "#destroy_queue" do
it "queues itself for orchestrate_destroy" do
manager = FactoryGirl.create(:ext_management_system, :zone => EvmSpecHelper.local_miq_server.zone)
provider.managers = [manager]
provider.destroy_queue
expect(MiqQueue.find_by(:instance_id => provider.id)).to have_attributes(
'method_name' => 'orchestrate_destroy',
'class_name' => provider.class.name,
)
end
end

context "#orchestrate_destroy" do
it "to destroy itself when has no managers" do
provider.managers = []
expect(provider).to receive(:destroy)
provider.orchestrate_destroy
end

it "to destroy_queue its managers and itself" do
manager = FactoryGirl.create(:ext_management_system, :zone => EvmSpecHelper.local_miq_server.zone)
provider.managers = [manager]
expect(manager).to receive(:destroy_queue)
expect(provider).not_to receive(:destroy)
provider.orchestrate_destroy
expect(MiqQueue.find_by(:instance_id => provider.id)).to have_attributes(
'method_name' => 'orchestrate_destroy',
'class_name' => provider.class.name,
)
end
end
end

0 comments on commit 3649541

Please sign in to comment.