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

MiqSchedule call method directly if available #17588

Merged
merged 4 commits into from
Jun 15, 2018
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
38 changes: 25 additions & 13 deletions app/models/miq_schedule.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class MiqSchedule < ApplicationRecord
include ReservedMixin
reserve_attribute :resource_id, :big_integer

validates :name, :uniqueness => {:scope => [:userid, :towhat]}
validates :name, :description, :towhat, :run_at, :presence => true
validate :validate_run_at, :validate_file_depot
Expand Down Expand Up @@ -39,6 +42,12 @@ class MiqSchedule < ApplicationRecord
default_value_for :enabled, true
default_value_for(:zone_id) { MiqServer.my_server.zone_id }

def resource
# HACK: this should be a real relation, but for now it's using a reserve_attribute for backport reasons
return unless resource_id
towhat.safe_constantize.find_by(:id => resource_id)
end

def set_start_time_and_prod_default
run_at # Internally this will correct :start_time to UTC
self.prod_default = "system" if SYSTEM_SCHEDULE_CLASSES.include?(towhat.to_s)
Expand Down Expand Up @@ -72,21 +81,24 @@ def self.queue_scheduled_work(id, _rufus_job_id, at, _params)
_log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.towhat}] [#{method}]")

action = "action_" + method
unless sched.respond_to?(action)

if sched.respond_to?(action)
msg = MiqQueue.submit_job(
:class_name => name,
:instance_id => sched.id,
:method_name => "invoke_actions",
:args => [action, at],
:msg_timeout => 1200
)

_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")
return
end

msg = MiqQueue.submit_job(
:class_name => name,
:instance_id => sched.id,
:method_name => "invoke_actions",
:args => [action, at],
:msg_timeout => 1200
)

_log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.towhat}] [#{method}]...complete")
msg
end

def invoke_actions(action, at)
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