Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a task when destroying an ems #16669

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions app/models/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -444,33 +444,52 @@ def enable!

# override destroy_queue from AsyncDeleteMixin
def self.destroy_queue(ids)
find(Array.wrap(ids)).each(&:destroy_queue)
find(Array.wrap(ids)).map(&:destroy_queue)
end

def destroy_queue
_log.info("Queuing destroy of #{self.class.name} with id: #{id}")
msg = "Queuing destroy of #{self.class.name} with id: #{id}"

_log.info(msg)
task = MiqTask.create(
:name => "Destroying #{self.class.name} with id: #{id}",
:state => MiqTask::STATE_QUEUED,
:status => MiqTask::STATUS_OK,
:message => msg,
)

child_managers.each(&:destroy_queue)
self.class.schedule_destroy_queue(id)
self.class.schedule_destroy_queue(id, task.id)

task.id
end

def self.schedule_destroy_queue(id, deliver_on = nil)
def self.schedule_destroy_queue(id, task_id, deliver_on = nil)
MiqQueue.put(
:class_name => name,
:instance_id => id,
:method_name => "orchestrate_destroy",
:deliver_on => deliver_on,
:args => [task_id],
)
end

# Wait until all associated workers are dead to destroy this ems
def orchestrate_destroy
def orchestrate_destroy(task_id)
disable! if enabled?

if self.destroy == false
_log.info("Cannot destroy #{self.class.name} with id: #{id}, workers still in progress. Requeuing destroy...")
self.class.schedule_destroy_queue(id, 15.seconds.from_now)
msg = "Cannot destroy #{self.class.name} with id: #{id}, workers still in progress. Requeuing destroy..."
MiqTask.update_status(task_id, MiqTask::STATE_ACTIVE, MiqTask::STATUS_OK, msg)

_log.info(msg)

self.class.schedule_destroy_queue(id, task_id, 15.seconds.from_now)
else
_log.info("#{self.class.name} with id: #{id} destroyed")
msg = "#{self.class.name} with id: #{id} destroyed"
MiqTask.update_status(task_id, MiqTask::STATE_FINISHED, MiqTask::STATUS_OK, msg)

_log.info(msg)
end
end

Expand Down
69 changes: 63 additions & 6 deletions spec/models/ext_management_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,70 @@
ems.destroy
expect(ExtManagementSystem.count).to eq(1)
end
end

context "#queue_destroy" do
let(:server) { EvmSpecHelper.local_miq_server }
let(:zone) { server.zone }

context "with no child managers" do
let(:ems) do
FactoryGirl.create(:ext_management_system, :zone => zone)
end

it "returns a task" do
task_id = ems.destroy_queue

deliver_queue_message

task = MiqTask.find(task_id)
expect(task.state).to eq("Finished")
expect(task.status).to eq("Ok")
end

it "re-schedules with active workers" do
FactoryGirl.create(:miq_ems_refresh_worker, :queue_name => ems.queue_name, :status => "started", :miq_server => server)
ems.destroy_queue

expect(MiqQueue.count).to eq(1)

deliver_queue_message

expect(MiqQueue.count).to eq(1)
expect(MiqQueue.last.deliver_on).to_not be_nil
end

it "destroys the ems when the active worker shuts down" do
refresh_worker = FactoryGirl.create(:miq_ems_refresh_worker, :queue_name => ems.queue_name, :status => "started", :miq_server => server)
ems.destroy_queue

deliver_queue_message

expect(ExtManagementSystem.count).to eq(1)

refresh_worker.destroy

deliver_queue_message

expect(ExtManagementSystem.count).to eq(0)
end
end

context "with child managers" do
let(:child_manager) { FactoryGirl.create(:ext_management_system) }
let(:ems) { FactoryGirl.create(:ext_management_system, :zone => zone, :child_managers => [child_manager]) }

it "queues up destroy for child_managers" do
described_class.destroy_queue(ems.id)

expect(MiqQueue.count).to eq(2)
expect(MiqQueue.pluck(:instance_id)).to include(ems.id, child_manager.id)
end
end

it "queues up destroy for child_managers" do
child_manager = FactoryGirl.create(:ext_management_system)
ems = FactoryGirl.create(:ext_management_system, :child_managers => [child_manager])
expect(described_class).to receive(:schedule_destroy_queue).with(ems.id)
expect(described_class).to receive(:schedule_destroy_queue).with(child_manager.id)
described_class.destroy_queue(ems.id)
def deliver_queue_message(queue_message = MiqQueue.order(:id).first)
status, message, result = queue_message.deliver
queue_message.delivered(status, message, result)
end
end

Expand Down