From 5565e645000e3b3dff16d1a65ed5f2f2a8d6d8e0 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Thu, 14 Jun 2018 15:45:44 -0400 Subject: [PATCH] If the MiqSchedule#resource responds to the method, call it This avoids having to create an action_ method in the MiqSchedule namespace --- app/models/miq_schedule.rb | 3 ++ spec/models/miq_schedule_spec.rb | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/app/models/miq_schedule.rb b/app/models/miq_schedule.rb index 7d2b1720427..52a679c1f8e 100644 --- a/app/models/miq_schedule.rb +++ b/app/models/miq_schedule.rb @@ -93,6 +93,9 @@ def self.queue_scheduled_work(id, _rufus_job_id, at, _params) _log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.towhat}] [#{method}]...complete") msg + elsif sched.resource.respond_to?(method) + sched.resource.send(method, *sched.sched_action[:args]) + sched.update_attributes(:last_run_on => Time.now.utc) else _log.warn("[#{sched.name}] no such action: [#{method}], aborting schedule") end diff --git a/spec/models/miq_schedule_spec.rb b/spec/models/miq_schedule_spec.rb index 0df7da3f9eb..1db8e558673 100644 --- a/spec/models/miq_schedule_spec.rb +++ b/spec/models/miq_schedule_spec.rb @@ -695,4 +695,62 @@ expect(MiqSchedule.updated_since(1.month.ago)).to eq([s]) end end + + context ".queue_scheduled_work" do + it "When action exists" do + schedule = FactoryGirl.create(:miq_schedule, :sched_action => {:method => "scan"}) + MiqSchedule.queue_scheduled_work(schedule.id, nil, "abc", nil) + + expect(MiqQueue.first).to have_attributes( + :class_name => "MiqSchedule", + :instance_id => schedule.id, + :method_name => "invoke_actions", + :args => ["action_scan", "abc"], + :msg_timeout => 1200 + ) + end + + context "no action method" do + it "no resource" do + schedule = FactoryGirl.create(:miq_schedule, :sched_action => {:method => "test_method"}) + + expect($log).to receive(:warn) do |message| + expect(message).to include("no such action: [test_method], aborting schedule") + end + + MiqSchedule.queue_scheduled_work(schedule.id, nil, "abc", nil) + end + + context "resource exists" do + it "and does not respond to the method" do + resource = FactoryGirl.create(:host) + schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource, :sched_action => {:method => "test_method"}) + + expect($log).to receive(:warn) do |message| + expect(message).to include("no such action: [test_method], aborting schedule") + end + + MiqSchedule.queue_scheduled_work(schedule.id, nil, "abc", nil) + end + + it "and responds to the method" do + resource = FactoryGirl.create(:host) + schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource, :sched_action => {:method => "test_method"}) + + expect_any_instance_of(Host).to receive("test_method").once + + MiqSchedule.queue_scheduled_work(schedule.id, nil, "abc", nil) + end + + it "and responds to the method with arguments" do + resource = FactoryGirl.create(:host) + schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource, :sched_action => {:method => "test_method", :args => ["abc", 123, :a => 1]}) + + expect_any_instance_of(Host).to receive("test_method").once.with("abc", 123, :a => 1) + + MiqSchedule.queue_scheduled_work(schedule.id, nil, "abc", nil) + end + end + end + end end