Class: Homebrew::Service

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, T::Sig
Defined in:
service.rb

Overview

The Service class implements the DSL methods used in a formula’s service block and stores related instance variables. Most of these methods also return the related instance variable when no argument is provided.

Constant Summary collapse

RUN_TYPE_IMMEDIATE =
"immediate"
RUN_TYPE_INTERVAL =
"interval"
RUN_TYPE_CRON =
"cron"

Instance Method Summary collapse

Constructor Details

#initialize(formula, &block) ⇒ Service

sig { params(formula: Formula).void }



17
18
19
20
21
22
# File 'service.rb', line 17

def initialize(formula, &block)
  @formula = formula
  @run_type = RUN_TYPE_IMMEDIATE
  @environment_variables = {}
  @service_block = block
end

Instance Method Details

#commandArray<String>

Returns:



122
123
124
125
# File 'service.rb', line 122

def command
  instance_eval(&@service_block)
  @run.map(&:to_s)
end

#environment_variables(variables = {}) ⇒ Hash{String => String}?

Parameters:

Returns:



103
104
105
106
107
108
109
110
111
112
# File 'service.rb', line 103

def environment_variables(variables = {})
  case T.unsafe(variables)
  when nil
    @environment_variables
  when Hash
    @environment_variables = variables
  else
    raise TypeError, "Service#environment_variables expects a hash"
  end
end

#error_log_path(path = nil) ⇒ String?

Parameters:

Returns:



63
64
65
66
67
68
69
70
71
72
# File 'service.rb', line 63

def error_log_path(path = nil)
  case T.unsafe(path)
  when nil
    @error_log_path
  when String, Pathname
    @error_log_path = path.to_s
  else
    raise TypeError, "Service#error_log_path expects a String"
  end
end

#keep_alive(value = nil) ⇒ Boolean?

Parameters:

  • value (Boolean, nil) (defaults to: nil)

Returns:

  • (Boolean, nil)


75
76
77
78
79
80
81
82
83
84
# File 'service.rb', line 75

def keep_alive(value = nil)
  case T.unsafe(value)
  when nil
    @keep_alive
  when true, false
    @keep_alive = value
  else
    raise TypeError, "Service#keep_alive expects a Boolean"
  end
end

#log_path(path = nil) ⇒ String?

Parameters:

Returns:



51
52
53
54
55
56
57
58
59
60
# File 'service.rb', line 51

def log_path(path = nil)
  case T.unsafe(path)
  when nil
    @log_path
  when String, Pathname
    @log_path = path.to_s
  else
    raise TypeError, "Service#log_path expects a String"
  end
end

#run(command = nil) ⇒ Array?

Parameters:

Returns:

  • (Array, nil)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'service.rb', line 25

def run(command = nil)
  case T.unsafe(command)
  when nil
    @run
  when String, Pathname
    @run = [command]
  when Array
    @run = command
  else
    raise TypeError, "Service#run expects an Array"
  end
end

#run_type(type = nil) ⇒ String?

Parameters:

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'service.rb', line 87

def run_type(type = nil)
  case T.unsafe(type)
  when nil
    @run_type
  when "immediate", :immediate
    @run_type = type.to_s
  when RUN_TYPE_INTERVAL, RUN_TYPE_CRON
    raise TypeError, "Service#run_type does not support timers"
  when String
    raise TypeError, "Service#run_type allows: '#{RUN_TYPE_IMMEDIATE}'/'#{RUN_TYPE_INTERVAL}'/'#{RUN_TYPE_CRON}'"
  else
    raise TypeError, "Service#run_type expects a string"
  end
end

#std_service_path_envString

Returns:



117
118
119
# File 'service.rb', line 117

def std_service_path_env
  "#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
end

#to_plistString

Returns a String plist.

Returns:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'service.rb', line 130

def to_plist
  base = {
    Label:            @formula.plist_name,
    RunAtLoad:        @run_type == RUN_TYPE_IMMEDIATE,
    ProgramArguments: command,
  }

  base[:KeepAlive] = @keep_alive if @keep_alive == true
  base[:WorkingDirectory] = @working_dir if @working_dir.present?
  base[:StandardOutPath] = @log_path if @log_path.present?
  base[:StandardErrorPath] = @error_log_path if @error_log_path.present?
  base[:EnvironmentVariables] = @environment_variables unless @environment_variables.empty?

  base.to_plist
end

#to_systemd_unitString

Returns a String systemd unit.

Returns:



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'service.rb', line 149

def to_systemd_unit
  unit = <<~EOS
    [Unit]
    Description=Homebrew generated unit for #{@formula.name}

    [Service]
    Type=simple
    ExecStart=#{command.join(" ")}
  EOS

  options = []
  options << "Restart=always" if @keep_alive == true
  options << "WorkingDirectory=#{@working_dir}" if @working_dir.present?
  options << "StandardOutput=append:#{@log_path}" if @log_path.present?
  options << "StandardError=append:#{@error_log_path}" if @error_log_path.present?
  if @environment_variables.present?
    list = @environment_variables.map { |k, v| "#{k}=#{v}" }.join("&")
    options << "Environment=\"#{list}\""
  end

  unit + options.join("\n")
end

#working_dir(path = nil) ⇒ String?

Parameters:

Returns:



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

def working_dir(path = nil)
  case T.unsafe(path)
  when nil
    @working_dir
  when String, Pathname
    @working_dir = path.to_s
  else
    raise TypeError, "Service#working_dir expects a String"
  end
end