Class: Homebrew::Service
- Inherits:
-
Object
- Object
- Homebrew::Service
- Extended by:
- Forwardable
- Includes:
- OnSystem::MacOSAndLinux
- 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
- PROCESS_TYPE_BACKGROUND =
:background
- PROCESS_TYPE_STANDARD =
:standard
- PROCESS_TYPE_INTERACTIVE =
:interactive
- PROCESS_TYPE_ADAPTIVE =
:adaptive
- KEEP_ALIVE_KEYS =
[:always, :successful_exit, :crashed, :path].freeze
Instance Method Summary collapse
-
#command ⇒ Array<String>?
-
#cron(value = nil) ⇒ Hash?
-
#default_cron_values ⇒ Hash{Symbol => Integer, String}
-
#environment_variables(variables = {}) ⇒ Hash{String => String}?
-
#error_log_path(path = nil) ⇒ String?
-
#f ⇒ Formula
-
#initialize(formula, &block) ⇒ Service
constructor
sig { params(formula: Formula).void }.
-
#input_path(path = nil) ⇒ String?
-
#interval(value = nil) ⇒ Integer?
-
#keep_alive(value = nil) ⇒ Hash{Symbol => T.untyped}?
-
#keep_alive? ⇒ Boolean
Returns a
Boolean
describing if a service is set to be kept alive. -
#launch_only_once(value = nil) ⇒ Boolean?
-
#log_path(path = nil) ⇒ String?
-
#macos_legacy_timers(value = nil) ⇒ Boolean?
-
#manual_command ⇒ String
Returns the
String
command to run manually instead of the service. -
#parse_cron(cron_statement) ⇒ Hash{Symbol => Integer, String}
-
#process_type(value = nil) ⇒ Symbol?
-
#require_root(value = nil) ⇒ Boolean?
-
#requires_root? ⇒ Boolean
Returns a
Boolean
describing if a service requires root access. -
#restart_delay(value = nil) ⇒ Integer?
-
#root_dir(path = nil) ⇒ String?
-
#run(command = nil, macos: nil, linux: nil) ⇒ Array?
-
#run_type(value = nil) ⇒ Symbol?
-
#sockets(value = nil) ⇒ Hash{Symbol => String}?
-
#std_service_path_env ⇒ String
-
#timed? ⇒ Boolean
Returns a
Boolean
describing if a service is timed. -
#to_plist ⇒ String
Returns a
String
plist. -
#to_systemd_timer ⇒ String
Returns a
String
systemd unit timer. -
#to_systemd_unit ⇒ String
Returns a
String
systemd unit. -
#working_dir(path = nil) ⇒ String?
Methods included from OnSystem::MacOSAndLinux
Constructor Details
#initialize(formula, &block) ⇒ Service
sig { params(formula: Formula).void }
27 28 29 30 31 32 |
# File 'service.rb', line 27 def initialize(formula, &block) @formula = formula @run_type = RUN_TYPE_IMMEDIATE @environment_variables = {} @service_block = block end |
Instance Method Details
#command ⇒ Array<String>?
340 341 342 343 |
# File 'service.rb', line 340 def command instance_eval(&@service_block) @run&.map(&:to_s) end |
#cron(value = nil) ⇒ Hash?
253 254 255 256 257 258 259 260 261 262 |
# File 'service.rb', line 253 def cron(value = nil) case T.unsafe(value) when nil @cron when String @cron = parse_cron(T.must(value)) else raise TypeError, "Service#cron expects a String" end end |
#default_cron_values ⇒ Hash{Symbol => Integer, String}
265 266 267 268 269 270 271 272 273 |
# File 'service.rb', line 265 def default_cron_values { Month: "*", Day: "*", Weekday: "*", Hour: "*", Minute: "*", } end |
#environment_variables(variables = {}) ⇒ Hash{String => String}?
311 312 313 314 315 316 317 318 |
# File 'service.rb', line 311 def environment_variables(variables = {}) case T.unsafe(variables) when Hash @environment_variables = variables.transform_values(&:to_s) else raise TypeError, "Service#environment_variables expects a hash" end end |
#error_log_path(path = nil) ⇒ String?
109 110 111 112 113 114 115 116 117 118 |
# File 'service.rb', line 109 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 |
#input_path(path = nil) ⇒ String?
85 86 87 88 89 90 91 92 93 94 |
# File 'service.rb', line 85 def input_path(path = nil) case T.unsafe(path) when nil @input_path when String, Pathname @input_path = path.to_s else raise TypeError, "Service#input_path expects a String or Pathname" end end |
#interval(value = nil) ⇒ Integer?
241 242 243 244 245 246 247 248 249 250 |
# File 'service.rb', line 241 def interval(value = nil) case T.unsafe(value) when nil @interval when Integer @interval = value else raise TypeError, "Service#interval expects an Integer" end end |
#keep_alive(value = nil) ⇒ Hash{Symbol => T.untyped}?
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'service.rb', line 124 def keep_alive(value = nil) case T.unsafe(value) when nil @keep_alive when true, false @keep_alive = { always: value } when Hash hash = T.cast(value, Hash) unless (hash.keys - KEEP_ALIVE_KEYS).empty? raise TypeError, "Service#keep_alive allows only #{KEEP_ALIVE_KEYS}" end @keep_alive = value else raise TypeError, "Service#keep_alive expects a Boolean or Hash" end end |
#keep_alive? ⇒ Boolean
Returns a Boolean
describing if a service is set to be kept alive.
181 182 183 184 |
# File 'service.rb', line 181 def keep_alive? instance_eval(&@service_block) @keep_alive.present? && @keep_alive[:always] != false end |
#launch_only_once(value = nil) ⇒ Boolean?
187 188 189 190 191 192 193 194 195 196 |
# File 'service.rb', line 187 def launch_only_once(value = nil) case T.unsafe(value) when nil @launch_only_once when true, false @launch_only_once = value else raise TypeError, "Service#launch_only_once expects a Boolean" end end |
#log_path(path = nil) ⇒ String?
97 98 99 100 101 102 103 104 105 106 |
# File 'service.rb', line 97 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 |
#macos_legacy_timers(value = nil) ⇒ Boolean?
321 322 323 324 325 326 327 328 329 330 |
# File 'service.rb', line 321 def macos_legacy_timers(value = nil) case T.unsafe(value) when nil @macos_legacy_timers when true, false @macos_legacy_timers = value else raise TypeError, "Service#macos_legacy_timers expects a Boolean" end end |
#manual_command ⇒ String
Returns the String
command to run manually instead of the service.
348 349 350 351 352 353 354 355 356 |
# File 'service.rb', line 348 def manual_command instance_eval(&@service_block) vars = @environment_variables.except(:PATH) .map { |k, v| "#{k}=\"#{v}\"" } cmd = command out = vars + cmd if cmd.present? out.join(" ") end |
#parse_cron(cron_statement) ⇒ Hash{Symbol => Integer, String}
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'service.rb', line 276 def parse_cron(cron_statement) parsed = default_cron_values case cron_statement when "@hourly" parsed[:Minute] = 0 when "@daily" parsed[:Minute] = 0 parsed[:Hour] = 0 when "@weekly" parsed[:Minute] = 0 parsed[:Hour] = 0 parsed[:Weekday] = 0 when "@monthly" parsed[:Minute] = 0 parsed[:Hour] = 0 parsed[:Day] = 1 when "@yearly", "@annually" parsed[:Minute] = 0 parsed[:Hour] = 0 parsed[:Day] = 1 parsed[:Month] = 1 else cron_parts = cron_statement.split raise TypeError, "Service#parse_cron expects a valid cron syntax" if cron_parts.length != 5 [:Minute, :Hour, :Day, :Month, :Weekday].each_with_index do |selector, index| parsed[selector] = Integer(cron_parts.fetch(index)) if cron_parts.fetch(index) != "*" end end parsed end |
#process_type(value = nil) ⇒ Symbol?
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'service.rb', line 211 def process_type(value = nil) case T.unsafe(value) when nil @process_type when :background, :standard, :interactive, :adaptive @process_type = value when Symbol raise TypeError, "Service#process_type allows: " \ "'#{PROCESS_TYPE_BACKGROUND}'/'#{PROCESS_TYPE_STANDARD}'/" \ "'#{PROCESS_TYPE_INTERACTIVE}'/'#{PROCESS_TYPE_ADAPTIVE}'" else raise TypeError, "Service#process_type expects a Symbol" end end |
#require_root(value = nil) ⇒ Boolean?
143 144 145 146 147 148 149 150 151 152 |
# File 'service.rb', line 143 def require_root(value = nil) case T.unsafe(value) when nil @require_root when true, false @require_root = value else raise TypeError, "Service#require_root expects a Boolean" end end |
#requires_root? ⇒ Boolean
Returns a Boolean
describing if a service requires root access.
157 158 159 160 |
# File 'service.rb', line 157 def requires_root? instance_eval(&@service_block) @require_root.present? && @require_root == true end |
#restart_delay(value = nil) ⇒ Integer?
199 200 201 202 203 204 205 206 207 208 |
# File 'service.rb', line 199 def restart_delay(value = nil) case T.unsafe(value) when nil @restart_delay when Integer @restart_delay = value else raise TypeError, "Service#restart_delay expects an Integer" end end |
#root_dir(path = nil) ⇒ String?
73 74 75 76 77 78 79 80 81 82 |
# File 'service.rb', line 73 def root_dir(path = nil) case T.unsafe(path) when nil @root_dir when String, Pathname @root_dir = path.to_s else raise TypeError, "Service#root_dir expects a String or Pathname" end end |
#run(command = nil, macos: nil, linux: nil) ⇒ Array?
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'service.rb', line 46 def run(command = nil, macos: nil, linux: nil) command ||= on_system_conditional(macos: macos, linux: linux) 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(value = nil) ⇒ Symbol?
227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'service.rb', line 227 def run_type(value = nil) case T.unsafe(value) when nil @run_type when :immediate, :interval, :cron @run_type = value when Symbol raise TypeError, "Service#run_type allows: '#{RUN_TYPE_IMMEDIATE}'/'#{RUN_TYPE_INTERVAL}'/'#{RUN_TYPE_CRON}'" else raise TypeError, "Service#run_type expects a Symbol" end end |
#sockets(value = nil) ⇒ Hash{Symbol => String}?
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'service.rb', line 163 def sockets(value = nil) case T.unsafe(value) when nil @sockets when String match = T.must(value).match(%r{([a-z]+)://([a-z0-9.]+):([0-9]+)}i) raise TypeError, "Service#sockets a formatted socket definition as <type>://<host>:<port>" if match.blank? type, host, port = match.captures @sockets = { host: host, port: port, type: type } else raise TypeError, "Service#sockets expects a String" end end |
#std_service_path_env ⇒ String
335 336 337 |
# File 'service.rb', line 335 def std_service_path_env "#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin" end |
#timed? ⇒ Boolean
Returns a Boolean
describing if a service is timed.
361 362 363 364 |
# File 'service.rb', line 361 def timed? instance_eval(&@service_block) @run_type == RUN_TYPE_CRON || @run_type == RUN_TYPE_INTERVAL end |
#to_plist ⇒ String
Returns a String
plist.
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'service.rb', line 369 def to_plist # command needs to be first because it initializes all other values base = { Label: @formula.plist_name, ProgramArguments: command, RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE, } base[:LaunchOnlyOnce] = @launch_only_once if @launch_only_once == true base[:LegacyTimers] = @macos_legacy_timers if @macos_legacy_timers == true base[:TimeOut] = @restart_delay if @restart_delay.present? base[:ProcessType] = @process_type.to_s.capitalize if @process_type.present? base[:StartInterval] = @interval if @interval.present? && @run_type == RUN_TYPE_INTERVAL base[:WorkingDirectory] = @working_dir if @working_dir.present? base[:RootDirectory] = @root_dir if @root_dir.present? base[:StandardInPath] = @input_path if @input_path.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? if keep_alive? if (always = @keep_alive[:always].presence) base[:KeepAlive] = always elsif @keep_alive.key?(:successful_exit) base[:KeepAlive] = { SuccessfulExit: @keep_alive[:successful_exit] } elsif @keep_alive.key?(:crashed) base[:KeepAlive] = { Crashed: @keep_alive[:crashed] } elsif @keep_alive.key?(:path) && @keep_alive[:path].present? base[:KeepAlive] = { PathState: @keep_alive[:path].to_s } end end if @sockets.present? base[:Sockets] = {} base[:Sockets][:Listeners] = { SockNodeName: @sockets[:host], SockServiceName: @sockets[:port], SockProtocol: @sockets[:type].upcase, SockFamily: "IPv4v6", } end if @cron.present? && @run_type == RUN_TYPE_CRON base[:StartCalendarInterval] = @cron.reject { |_, value| value == "*" } end # Adding all session types has as the primary effect that if you initialise it through e.g. a Background session # and you later "physically" sign in to the owning account (Aqua session), things shouldn't flip out. # Also, we're not checking @process_type here because that is used to indicate process priority and not # necessarily if it should run in a specific session type. Like database services could run with ProcessType # Interactive so they have no resource limitations enforced upon them, but they aren't really interactive in the # general sense. base[:LimitLoadToSessionType] = %w[Aqua Background LoginWindow StandardIO System] base.to_plist end |
#to_systemd_timer ⇒ String
Returns a String
systemd unit timer.
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'service.rb', line 462 def to_systemd_timer timer = <<~EOS [Unit] Description=Homebrew generated timer for #{@formula.name} [Install] WantedBy=timers.target [Timer] Unit=#{@formula.service_name} EOS instance_eval(&@service_block) = [] << "Persistent=true" if @run_type == RUN_TYPE_CRON << "OnUnitActiveSec=#{@interval}" if @run_type == RUN_TYPE_INTERVAL if @run_type == RUN_TYPE_CRON minutes = (@cron[:Minute] == "*") ? "*" : format("%02d", @cron[:Minute]) hours = (@cron[:Hour] == "*") ? "*" : format("%02d", @cron[:Hour]) << "OnCalendar=#{@cron[:Weekday]}-*-#{@cron[:Month]}-#{@cron[:Day]} #{hours}:#{minutes}:00" end timer + .join("\n") end |
#to_systemd_unit ⇒ String
Returns a String
systemd unit.
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'service.rb', line 429 def to_systemd_unit unit = <<~EOS [Unit] Description=Homebrew generated unit for #{@formula.name} [Install] WantedBy=default.target [Service] EOS # command needs to be first because it initializes all other values cmd = command&.join(" ") = [] << "Type=#{(@launch_only_once == true) ? "oneshot" : "simple"}" << "ExecStart=#{cmd}" << "Restart=always" if @keep_alive.present? && @keep_alive[:always].present? << "RestartSec=#{restart_delay}" if @restart_delay.present? << "WorkingDirectory=#{@working_dir}" if @working_dir.present? << "RootDirectory=#{@root_dir}" if @root_dir.present? << "StandardInput=file:#{@input_path}" if @input_path.present? << "StandardOutput=append:#{@log_path}" if @log_path.present? << "StandardError=append:#{@error_log_path}" if @error_log_path.present? += @environment_variables.map { |k, v| "Environment=\"#{k}=#{v}\"" } if @environment_variables.present? unit + .join("\n") end |