Class: Wheneverd::DSL::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/wheneverd/dsl/context.rb

Overview

The evaluation context used for schedule files.

The schedule file is evaluated via instance_eval, so methods defined here become available as the schedule DSL (every, command, shell).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ Context

Returns a new instance of Context.

Parameters:

  • path (String)


17
18
19
20
21
22
# File 'lib/wheneverd/dsl/context.rb', line 17

def initialize(path:)
  @path = path
  @schedule = Wheneverd::Schedule.new
  @current_entry = nil
  @period_parser = Wheneverd::DSL::PeriodParser.new(path: path)
end

Instance Attribute Details

#pathString (readonly)

Returns absolute schedule path.

Returns:

  • (String)

    absolute schedule path



11
12
13
# File 'lib/wheneverd/dsl/context.rb', line 11

def path
  @path
end

#scheduleWheneverd::Schedule (readonly)

Returns schedule being built during evaluation.

Returns:



14
15
16
# File 'lib/wheneverd/dsl/context.rb', line 14

def schedule
  @schedule
end

Instance Method Details

#command(command_value) ⇒ void

This method returns an undefined value.

Add a oneshot command job to the current every entry.

Examples:

String command

command "echo hello"

argv command

command ["echo", "hello world"]

Parameters:

  • command_value (String, Array<String>)


55
56
57
58
59
60
61
# File 'lib/wheneverd/dsl/context.rb', line 55

def command(command_value)
  ensure_in_every_block!("command")

  @current_entry.add_job(Wheneverd::Job::Command.new(command: command_value))
rescue Wheneverd::InvalidCommandError => e
  raise LoadError.new(e.message, path: path)
end

#every(*periods, at: nil, &block) ⇒ Wheneverd::Entry

Define a scheduled entry and evaluate its jobs block.

Parameters:

  • periods (Array<String, Symbol, Wheneverd::Duration, Array<Symbol>>)
  • at (String, Array<String>, nil) (defaults to: nil)

Returns:

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wheneverd/dsl/context.rb', line 29

def every(*periods, at: nil, &block)
  raise InvalidPeriodError.new("every() requires a block", path: path) unless block

  raise InvalidPeriodError.new("every() requires a period", path: path) if periods.empty?

  period = periods.length == 1 ? periods.first : periods
  trigger = @period_parser.trigger_for(period, at: at)
  entry = Wheneverd::Entry.new(trigger: trigger)

  schedule.add_entry(entry)

  with_current_entry(entry) { instance_eval(&block) }

  entry
end

#service(name, command: nil, shell: nil, restart: "always", restart_sec: "5s", service: {}) ⇒ Wheneverd::Service

Add a long-running systemd user service to the schedule.

Examples:

Shell service

service "worker", shell: "bundle exec bin/worker"

Argv service

service "worker", command: ["bundle", "exec", "bin/worker"]

Parameters:

  • name (String)

    stable service name within the schedule

  • command (String, Array<String>, nil) (defaults to: nil)
  • shell (String, nil) (defaults to: nil)

    shell script to run via /bin/bash -lc

  • restart (String) (defaults to: "always")
  • restart_sec (String) (defaults to: "5s")
  • service (Hash, Array<String>) (defaults to: {})

    extra [Service] lines

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/wheneverd/dsl/context.rb', line 93

def service(name, command: nil, shell: nil, restart: "always", restart_sec: "5s",
            service: {})
  command_value = normalize_service_command(command: command, shell: shell)
  service_obj = Wheneverd::Service.new(
    name: name,
    command: command_value,
    restart: restart,
    restart_sec: restart_sec,
    service: service
  )
  schedule.add_service(service_obj)
  service_obj
rescue Wheneverd::InvalidCommandError => e
  raise LoadError.new(e.message, path: path)
end

#shell(script, shell: "/bin/bash") ⇒ void

This method returns an undefined value.

Add a oneshot command job that runs via /bin/bash -lc.

Examples:

shell "echo hello | sed -e s/hello/hi/"

Parameters:

  • script (String)

    non-empty script to pass as bash -lc <script>

  • shell (String) (defaults to: "/bin/bash")

    shell executable (default: "/bin/bash")



71
72
73
74
75
76
# File 'lib/wheneverd/dsl/context.rb', line 71

def shell(script, shell: "/bin/bash")
  ensure_in_every_block!("shell")
  script_stripped = normalize_shell_script(script)
  shell_executable = normalize_shell_executable(shell)
  command([shell_executable, "-lc", script_stripped])
end