From 9a91d360a914c8c34baa321406691b3929a986b7 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Thu, 8 Aug 2019 09:43:12 -0400 Subject: [PATCH] Merge pull request #19037 from yrudman/che-for-existted-sheduler-when-seeding Fix: do not raise error if schedule for widget exists but not linked (cherry picked from commit a248ba302ef069a25d4967eee97f363d41d35745) https://bugzilla.redhat.com/show_bug.cgi?id=1729441 --- app/models/miq_widget.rb | 22 ++++++++++++++++++++-- spec/models/miq_widget_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/app/models/miq_widget.rb b/app/models/miq_widget.rb index 0d30d0d5be2..3fc35c57009 100644 --- a/app/models/miq_widget.rb +++ b/app/models/miq_widget.rb @@ -492,6 +492,10 @@ def self.sync_from_hash(attrs) widget end + def filter_for_schedule + {"=" => {"field" => "MiqWidget-id", "value" => id}} + end + def sync_schedule(schedule_info) return if schedule_info.nil? @@ -511,11 +515,12 @@ def sync_schedule(schedule_info) raise _("Unsupported interval '%{interval}'") % {:interval => interval} end - sched = MiqSchedule.create!( + sched = existing_schedule + sched ||= MiqSchedule.create!( :name => description, :description => description, :sched_action => {:method => "generate_widget"}, - :filter => MiqExpression.new("=" => {"field" => "MiqWidget-id", "value" => id}), + :filter => MiqExpression.new(filter_for_schedule), :resource_type => self.class.name, :run_at => { :interval => {:value => value, :unit => unit}, @@ -532,6 +537,19 @@ def sync_schedule(schedule_info) sched end + def existing_schedule + return nil if (sched = MiqSchedule.find_by(:name => description)).nil? + + # return existing sheduler if filter referr to the same widget + return sched if sched.filter.exp == filter_for_schedule + + # change name of existed schedule in case it is in use + suffix = Time.new.utc.to_s + _log.warn("Schedule #{sched.name} already exists, renaming it to `#{sched.name} #{suffix}`") + sched.update(:name => "#{sched.name} #{suffix}", :description => "#{sched.description} #{suffix}") + nil + end + def self.seed sync_from_dir end diff --git a/spec/models/miq_widget_spec.rb b/spec/models/miq_widget_spec.rb index 24295f639f9..c3e1f182da6 100644 --- a/spec/models/miq_widget_spec.rb +++ b/spec/models/miq_widget_spec.rb @@ -56,6 +56,40 @@ ')) end + describe "#filter_for_schedule" do + it "returns Hash object representing valid MiqExpression" do + exp = MiqExpression.new(@widget_chart_vendor_and_guest_os.filter_for_schedule) + expect(exp.valid?).to be_truthy + end + end + + describe "#sync_schedule" do + let(:schedule) do + filter = @widget_chart_vendor_and_guest_os.filter_for_schedule + FactoryBot.create(:miq_schedule, :filter => MiqExpression.new(filter), :resource_type => "MiqWidget", + :name => @widget_chart_vendor_and_guest_os.name) + end + + it "uses existing schedule if link between widget and schedule broken" do + expect(@widget_chart_vendor_and_guest_os.miq_schedule).to be_nil + @widget_chart_vendor_and_guest_os.sync_schedule(:run_at => schedule.run_at) + + expect(MiqSchedule.count).to eq(1) + expect(@widget_chart_vendor_and_guest_os.miq_schedule.id).to eq(schedule.id) + end + + it "rename existing scheduler by adding timestamp to name if existing scheduler use different filter" do + schedule.update(:filter => MiqExpression.new("=" => {"field" => "MiqWidget-id", "value" => 9999})) + + time_now = Time.now.utc + Timecop.freeze(time_now) { @widget_chart_vendor_and_guest_os.sync_schedule(:run_at => schedule.run_at) } + schedule.reload + + expect(MiqSchedule.count).to eq(2) + expect(schedule.name.end_with?(time_now.to_s)).to be_truthy + end + end + context "#queue_generate_content_for_users_or_group" do before do @widget = @widget_report_vendor_and_guest_os