Module: Wheneverd::Systemd::CalendarSpec

Defined in:
lib/wheneverd/systemd/calendar_spec.rb

Overview

Translates higher-level schedule period specs into systemd OnCalendar= values.

The DSL produces values like "day@4:30 am" or "cron:0 0 27-31 * *", which are normalized here into systemd-friendly calendar expressions.

Constant Summary collapse

BASE_ALIASES =
{
  "hour" => "hourly",
  "day" => "daily",
  "month" => "monthly",
  "year" => "yearly"
}.freeze
PREFIXES =
{
  "day" => "*-*-*",
  "weekday" => "Mon..Fri *-*-*",
  "weekend" => "Sat,Sun *-*-*"
}.freeze
WEEKDAYS =
{
  "monday" => "Mon",
  "tuesday" => "Tue",
  "wednesday" => "Wed",
  "thursday" => "Thu",
  "friday" => "Fri",
  "saturday" => "Sat",
  "sunday" => "Sun"
}.freeze

Class Method Summary collapse

Class Method Details

.to_on_calendar(spec) ⇒ String

Convert a calendar spec into a systemd OnCalendar= value.

Parameters:

  • spec (String)

Returns:

  • (String)

Raises:



38
39
40
41
42
43
44
45
46
# File 'lib/wheneverd/systemd/calendar_spec.rb', line 38

def self.to_on_calendar(spec)
  values = to_on_calendar_values(spec)
  return values.fetch(0) if values.length == 1

  message =
    "Invalid calendar spec: #{spec.to_s.strip.inspect} " \
    "expands to multiple OnCalendar values"
  raise InvalidCalendarSpecError, message
end

.to_on_calendar_values(spec) ⇒ Array<String>

Convert a calendar spec into one or more systemd OnCalendar= values.

Some inputs (e.g., certain cron expressions) may require multiple OnCalendar= entries to preserve semantics.

Parameters:

  • spec (String)

Returns:

  • (Array<String>)

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wheneverd/systemd/calendar_spec.rb', line 56

def self.to_on_calendar_values(spec)
  input = spec.to_s.strip
  raise InvalidCalendarSpecError, "Invalid calendar spec: empty" if input.empty?

  return cron_to_on_calendar_values(input) if input.start_with?("cron:")

  base, at = input.split("@", 2)
  base = base.strip

  raise InvalidCalendarSpecError, "Invalid calendar spec: #{input.inspect}" if base.empty?

  [translate_base_with_optional_at(base, at)]
end