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 Dec 11, 2017
1 parent 7a5fd37 commit c51285e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
5 changes: 3 additions & 2 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)
def self._queue_task(task, ids, deliver_on = nil)
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
18 changes: 18 additions & 0 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,22 @@ def refresh_ems(opts = {})
end
managers.flat_map { |manager| EmsRefresh.queue_refresh(manager, nil, opts) }
end

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

def destroy_queue
if managers.empty?
return destroy
end

if managers.collect(&:enabled).any?
_log.info("Queuing destroy of managers of provider: #{self.class.name} with id: #{id}")
managers.flat_map{ |manager| manager.destroy_queue }
end

_log.info("Queuing destroy of provider: #{self.class.name} with id: #{id}")
self.class._queue_task(:destroy_queue, id.to_miq_a, 15.seconds.from_now)
end
end
34 changes: 34 additions & 0 deletions spec/models/provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,38 @@
expect(tenant.providers).to include(provider)
end
end

context "#orchestrate_destroy" do
let(:manager) { FactoryGirl.create(:ext_management_system) }

it "destroy when has no managers" do
expect(provider).to receive(:destroy)
provider.destroy_queue
end

it "call orchestrate_destroy its managers first" do
expect(manager).to receive(:enabled) { true }
provider.managers = [manager]
expect(manager).to receive(:destroy_queue)
expect(provider).not_to receive(:destroy)
provider.destroy_queue
end

it "doesn't orchestrate_destroy its managers when they are disabled" do
expect(manager).to receive(:enabled) { false }
provider.managers = [manager]
expect(manager).not_to receive(:destroy_queue)
expect(provider).not_to receive(:destroy)
provider.destroy_queue
end

it "queues itself for orchestrate_destroy when managers exists" do
allow(Time).to receive(:now).and_return(Time.now)
provider.managers = [manager]
expect(manager).to receive(:destroy_queue)
expect(provider).not_to receive(:destroy)
expect(described_class).to receive(:_queue_task).with(:destroy_queue, provider.id.to_miq_a, 15.seconds.from_now)
provider.destroy_queue
end
end
end

0 comments on commit c51285e

Please sign in to comment.