Skip to content
Open
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
21 changes: 18 additions & 3 deletions lib/fbe/repeatedly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative '../fbe'
require_relative 'fb'
require_relative 'overwrite'
require_relative 'pmp'

# Run the block provided every X hours based on PMP configuration.
#
Expand All @@ -18,10 +19,12 @@
# got in the previous run.
#
# @param [String] area The name of the PMP area
# @param [String] p_every_hours PMP property name for interval (defaults to 24 hours if not in PMP)
# @param [String] p_every_hours PMP property name for interval (defaults to the pmp.xml value, or 24 hours)
# @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] The judge fact to populate with execution details
# @return [nil] Nothing
# @raise [Fbe::Error] If required parameters or globals are nil
Expand All @@ -33,15 +36,27 @@
# f.issues_found = count_issues
# # PMP might have: hours_between_checks=6
# end
def Fbe.repeatedly(area, p_every_hours, fb: Fbe.fb, judge: $judge, loog: $loog, &)
def Fbe.repeatedly( # rubocop:disable Metrics/AbcSize
area, p_every_hours,
fb: Fbe.fb, judge: $judge, loog: $loog, global: $global, options: $options, &
)
raise(Fbe::Error, 'The area is nil') if area.nil?
raise(Fbe::Error, 'The p_every_hours is nil') if p_every_hours.nil?
raise(Fbe::Error, 'The fb is nil') if fb.nil?
raise(Fbe::Error, 'The $judge is not set') if judge.nil?
raise(Fbe::Error, 'The $loog is not set') if loog.nil?
raise(Fbe::Error, 'A block is required by repeatedly') unless block_given?
pmp = fb.query("(and (eq what 'pmp') (eq area '#{area.gsub("'", "\\\\'")}') (exists #{p_every_hours}))").each.first
hours = pmp.nil? ? 24 : pmp[p_every_hours].first
hours =
if pmp.nil?
begin
Fbe.pmp(fb:, global:, options:, loog:).public_send(area).public_send(p_every_hours)
rescue Fbe::Error
24
end
else
pmp[p_every_hours].first
end
marker = "(and (eq what 'repeatedly') (eq judge '#{judge.gsub("'", "\\\\'")}'))"
recent = fb.query(
"(and
Expand Down
16 changes: 16 additions & 0 deletions test/fbe/test_repeatedly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,20 @@ def test_prefers_the_configured_interval_over_the_daily_default
end
assert(ran, "the judge stayed idle #{hours + 1} hours later, while the interval is #{hours}, seed is #{seed}")
end

def test_uses_the_pmp_xml_default_when_no_area_fact_exists
$global = {}
$options = Judges::Options.new
fb = Fbe.fb(fb: Factbase.new, global: {}, options: $options, loog: Loog::NULL)
Fbe.repeatedly('integration', 'eva_learn_hours', fb:, judge: 'test', loog: Loog::NULL) do |f|
f.foo = 42
end
ran = false
Time.stub(:now, Time.now + (9 * 60 * 60)) do
Fbe.repeatedly('integration', 'eva_learn_hours', fb:, judge: 'test', loog: Loog::NULL) do |_f|
ran = true
end
end
assert(ran, 'the judge stayed idle 9 hours later, while pmp.xml says eva_learn_hours is 8')
end
end
Loading