Class: FiberJob::CronParser

Inherits:
Object
  • Object
show all
Defined in:
lib/fiber_job/cron_parser.rb

Overview

Simple cron parser - supports basic patterns Format: [second] minute hour day month weekday Examples: “*/30 * * * * *” (every 30 sec), “0 2 * * *” (daily 2am)

Class Method Summary collapse

Class Method Details

.next_run(cron_expression, from_time = Time.now) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fiber_job/cron_parser.rb', line 8

def self.next_run(cron_expression, from_time = Time.now)
  fields = cron_expression.split

  if fields.length == 6
    # 6-field format: second minute hour day month weekday
    second, minute, hour, day, month, weekday = fields
    current = from_time + 1 # Start from next second
    current = Time.new(current.year, current.month, current.day, current.hour, current.min, current.sec)
    increment = 1
    max_iterations = 86_400 # 24 hours in seconds
  elsif fields.length == 5
    # 5-field format: minute hour day month weekday
    minute, hour, day, month, weekday = fields
    second = nil
    current = from_time + 60 # Start from next minute
    current = Time.new(current.year, current.month, current.day, current.hour, current.min, 0)
    increment = 60
    max_iterations = 1440 # 24 hours in minutes
  else
    raise "Invalid cron expression: #{cron_expression}. Must have 5 or 6 fields."
  end

  # Simple implementation - find next matching time
  max_iterations.times do
    if matches?(current, second, minute, hour, day, month, weekday)
      return current
    end
    current += increment
  end

  raise "No matching time found for cron expression: #{cron_expression}"
end