Class: Protege::Loop::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/protege/loop/schedule.rb

Overview

Domain wrapper over a standard 5-field cron expression — the only knob a responsibility's schedule exposes. Backed by Fugit::Cron so raw cron parsing never scatters through the engine (the gateway/wrapper convention), and deliberately answers just one question: does a given minute match? The Loop scheduler ticks every minute, so there is no "next occurrence" maths to do.

The expression is interpreted in the application timezone (+Time.zone+) by pinning Fugit to that zone's IANA name, so "0 9 * * *" means 9am app-time regardless of the host's system clock.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression) ⇒ void

Parameters:

  • expression (String, nil)

    a standard 5-field cron expression (e.g. "0 9 * * 1-5")



30
31
32
33
# File 'lib/protege/loop/schedule.rb', line 30

def initialize(expression)
  @expression = expression.to_s.strip
  @cron       = Fugit::Cron.parse(zoned(@expression))
end

Instance Attribute Details

#expressionString (readonly)

Returns the raw cron expression as given.

Returns:

  • (String)

    the raw cron expression as given



26
27
28
# File 'lib/protege/loop/schedule.rb', line 26

def expression
  @expression
end

Class Method Details

.valid?(expression) ⇒ Boolean

Whether expression parses as a valid cron string. Never raises — safe for validations.

Parameters:

  • expression (String, nil)

    the cron expression to check

Returns:

  • (Boolean)

    true when parseable



20
21
22
# File 'lib/protege/loop/schedule.rb', line 20

def valid?(expression)
  new(expression).valid?
end

Instance Method Details

#due?(time) ⇒ Boolean

Whether time falls on this schedule, at minute granularity. Seconds are zeroed before the match because a 5-field cron fires at second zero, while the tick runs a few seconds into the minute.

Parameters:

  • time (Time)

    the moment to test (typically Time.current)

Returns:

  • (Boolean)

    true when this minute is a scheduled minute



46
47
48
49
50
# File 'lib/protege/loop/schedule.rb', line 46

def due?(time)
  return false unless valid?

  @cron.match?(time.change(sec: 0, usec: 0))
end

#valid?Boolean

Returns true when the expression parsed as valid cron.

Returns:

  • (Boolean)

    true when the expression parsed as valid cron



36
37
38
# File 'lib/protege/loop/schedule.rb', line 36

def valid?
  !@cron.nil?
end