diff --git a/lib/fbe/pmp.rb b/lib/fbe/pmp.rb index 709ea1bf..29b5c879 100644 --- a/lib/fbe/pmp.rb +++ b/lib/fbe/pmp.rb @@ -82,7 +82,7 @@ def initialize(value, default, type, memo) raise(ArgumentError, "#{value} is not a whole number") unless (f % 1).zero? Integer(f) end - query = ->(area) { fb.query("(and (eq what 'pmp') (eq area '#{area}'))") } + query = ->(area) { fb.query("(and (eq what 'pmp') (eq area '#{area.gsub("'", "\\\\'")}'))") } owner = ->(area, param) { query.call(area).each.find { |f| !f[param].nil? } } Class.new do define_method(:areas) do @@ -92,7 +92,7 @@ def initialize(value, default, type, memo) end others do |*args1| # rubocop:disable Metrics/BlockLength area = args1.first.to_s - node = xml.at_xpath("/pmp/area[@name='#{area}']") + node = xml.at_xpath('/pmp/area[@name=$name]', nil, 'name' => area) if node.nil? Class.new do define_method(:properties) do diff --git a/lib/fbe/regularly.rb b/lib/fbe/regularly.rb index 894796b1..088037ed 100644 --- a/lib/fbe/regularly.rb +++ b/lib/fbe/regularly.rb @@ -6,6 +6,7 @@ require 'tago' require_relative '../fbe' require_relative 'fb' +require_relative 'pmp' # Run the block provided every X days based on PMP configuration. # @@ -14,11 +15,13 @@ # Creates a fact recording when the judge was last run. # # @param [String] area The name of the PMP area -# @param [String] p_every_days PMP property name for interval (defaults to 7 days if not in PMP) -# @param [String] p_since_days PMP property name for since period (defaults to 28 days if not in PMP) +# @param [String] p_every_days PMP property name for interval (defaults to the pmp.xml value, or 7 days) +# @param [String] p_since_days PMP property name for since period (defaults to the pmp.xml value, or 28 days) # @param [Factbase] fb The factbase (defaults to Fbe.fb) # @param [String] judge The name of the judge (uses $judge global) # @param [Loog] loog The logging facility (uses $loog global) +# @param [Hash] global Hash of global options (uses $global), needed to read pmp.xml defaults +# @param [Judges::Options] options The options (uses $options), needed to read pmp.xml defaults # @yield [Factbase::Fact] Fact to populate with judge execution details # @return [nil] Nothing # @raise [Fbe::Error] If required parameters or globals are nil @@ -29,14 +32,23 @@ # f.total_cleaned = cleanup_old_records # # PMP might have: days_between_cleanups=3, cleanup_history_days=30 # end -def Fbe.regularly(area, p_every_days, p_since_days = nil, fb: Fbe.fb, judge: $judge, loog: $loog, &) +def Fbe.regularly( # rubocop:disable Metrics/AbcSize + area, p_every_days, p_since_days = nil, + fb: Fbe.fb, judge: $judge, loog: $loog, global: $global, options: $options, & +) { 'area' => area, 'p_every_days' => p_every_days, 'fb' => fb }.each do |name, value| raise(Fbe::Error, "The #{name} is nil") if value.nil? end raise(Fbe::Error, 'The $judge is not set') if judge.nil? raise(Fbe::Error, 'The $loog is not set') if loog.nil? pmp = fb.query("(and (eq what 'pmp') (eq area '#{area.gsub("'", "\\\\'")}'))").each.to_a - interval = pmp.filter_map { |f| f[p_every_days]&.first }.first || 7 + default = + lambda do |prop, value| + Fbe.pmp(fb:, global:, options:, loog:).public_send(area).public_send(prop) + rescue Fbe::Error + value + end + interval = pmp.filter_map { |f| f[p_every_days]&.first }.first || default.call(p_every_days, 7) recent = fb.query( "(and (eq what 'regularly') @@ -57,7 +69,7 @@ def Fbe.regularly(area, p_every_days, p_since_days = nil, fb: Fbe.fb, judge: $ju f.judge = judge f.when = Time.now unless p_since_days.nil? - days = pmp.filter_map { |f| f[p_since_days]&.first }.first || 28 + days = pmp.filter_map { |f| f[p_since_days]&.first }.first || default.call(p_since_days, 28) since = Time.now - (days * 24 * 60 * 60) f.since = since end diff --git a/test/fbe/test_regularly.rb b/test/fbe/test_regularly.rb index c8d28002..2aac1fd3 100644 --- a/test/fbe/test_regularly.rb +++ b/test/fbe/test_regularly.rb @@ -4,6 +4,7 @@ # SPDX-License-Identifier: MIT require 'factbase' +require 'judges/options' require 'loog' require_relative '../../lib/fbe/regularly' require_relative '../test__helper' @@ -116,4 +117,20 @@ def test_judge_with_single_quote end assert_equal(2, fb.size) end + + def test_uses_the_pmp_xml_default_when_no_area_fact_exists + $global = {} + $options = Judges::Options.new + fb = Factbase.new + Fbe.regularly('integration', 'eva_interval', fb:, loog: Loog::NULL, judge: 'test') do |f| + f.foo = 42 + end + ran = false + Time.stub(:now, Time.now + (8 * 24 * 60 * 60)) do + Fbe.regularly('integration', 'eva_interval', fb:, loog: Loog::NULL, judge: 'test') do |_f| + ran = true + end + end + refute(ran, 'the judge ran again 8 days later, while pmp.xml says eva_interval is 10') + end end