diff --git a/app/models/mixins/async_delete_mixin.rb b/app/models/mixins/async_delete_mixin.rb index 5932aa0d33fa..d98a708c2e24 100644 --- a/app/models/mixins/async_delete_mixin.rb +++ b/app/models/mixins/async_delete_mixin.rb @@ -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 diff --git a/app/models/provider.rb b/app/models/provider.rb index 0715e4470181..aa77a99ac1ee 100644 --- a/app/models/provider.rb +++ b/app/models/provider.rb @@ -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 diff --git a/spec/models/provider_spec.rb b/spec/models/provider_spec.rb index c73c708d4416..826320f4b5da 100644 --- a/spec/models/provider_spec.rb +++ b/spec/models/provider_spec.rb @@ -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 @@ -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