Class: Wheneverd::Service

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

Overview

A long-running systemd user service managed alongside scheduled timers.

Constant Summary collapse

SAFE_SETTING_NAME =
/\A[A-Za-z][A-Za-z0-9]*\z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Service.

Parameters:

  • name (String)

    stable service name within the schedule

  • command (String, Array<String>)

    command to run as ExecStart

  • restart (String) (defaults to: "always")

    systemd Restart= value

  • restart_sec (String) (defaults to: "5s")

    systemd RestartSec= value

  • service (Hash, Array<String>) (defaults to: {})

    extra [Service] lines



28
29
30
31
32
33
34
# File 'lib/wheneverd/service.rb', line 28

def initialize(name:, command:, restart: "always", restart_sec: "5s", service: {})
  @name = normalize_name(name)
  @command = Wheneverd::Job::Command.new(command: command)
  @restart = normalize_required_value(restart, "restart")
  @restart_sec = normalize_required_value(restart_sec, "restart_sec")
  @service_lines = normalize_service_lines(service)
end

Instance Attribute Details

#commandWheneverd::Job::Command (readonly)



12
13
14
# File 'lib/wheneverd/service.rb', line 12

def command
  @command
end

#nameString (readonly)

Returns:

  • (String)


9
10
11
# File 'lib/wheneverd/service.rb', line 9

def name
  @name
end

#restartString (readonly)

Returns:

  • (String)


15
16
17
# File 'lib/wheneverd/service.rb', line 15

def restart
  @restart
end

#restart_secString (readonly)

Returns:

  • (String)


18
19
20
# File 'lib/wheneverd/service.rb', line 18

def restart_sec
  @restart_sec
end

#service_linesArray<String> (readonly)

Returns:

  • (Array<String>)


21
22
23
# File 'lib/wheneverd/service.rb', line 21

def service_lines
  @service_lines
end

Instance Method Details

#signatureString

Stable signature used for unit naming.

Returns:

  • (String)


39
40
41
42
43
44
45
46
47
# File 'lib/wheneverd/service.rb', line 39

def signature
  [
    "service:#{name}",
    command.signature,
    "restart:#{restart}",
    "restart_sec:#{restart_sec}",
    *service_lines
  ].join("\n")
end