Class: Kaal::OccurrenceFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/kaal/core/occurrence_finder.rb

Overview

Finds all absolute fire times for a parsed cron expression within a window.

Instance Method Summary collapse

Constructor Details

#initialize(configuration:) ⇒ OccurrenceFinder

Returns a new instance of OccurrenceFinder.



10
11
12
# File 'lib/kaal/core/occurrence_finder.rb', line 10

def initialize(configuration:)
  @configuration = configuration
end

Instance Method Details

#call(cron:, start_time:, end_time:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kaal/core/occurrence_finder.rb', line 14

def call(cron:, start_time:, end_time:)
  occurrences = []
  current_time = start_time.getutc
  normalized_end_time = end_time.getutc
  normalized_end_time_unix = normalized_end_time.to_f

  while current_time <= normalized_end_time
    next_occurrence = cron.next_time(current_time)
    break unless next_occurrence

    next_occurrence_unix = next_occurrence.to_f
    break if next_occurrence_unix > normalized_end_time_unix

    fire_time = Time.at(next_occurrence_unix).utc
    occurrences << fire_time
    current_time = Time.at(next_occurrence_unix + 1).utc
  end

  occurrences
rescue StandardError => e
  @configuration.logger&.error("Failed to calculate occurrences: #{e.message}")
  []
end