Skip to content

Commit

Permalink
Merge pull request #17588 from bdunne/miq_schedule_call_method_directly
Browse files Browse the repository at this point in the history
MiqSchedule call method directly if available
  • Loading branch information
gtanzillo authored Jun 15, 2018
2 parents ec113b6 + 5565e64 commit 7202cbe
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/models/miq_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions spec/models/miq_schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7202cbe

Please sign in to comment.