Class: Homebrew::Services::FormulaWrapper Private

Inherits:
Object
  • Object
show all
Includes:
Utils::Output::Mixin
Defined in:
services/formula_wrapper.rb

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: StatusOutputSuccessType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_uninstalled, #pretty_upgradable

Constructor Details

#initialize(formula) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Initialize a new Service instance with supplied formula.

Parameters:

Raises:



31
32
33
34
35
36
37
38
# File 'services/formula_wrapper.rb', line 31

def initialize(formula)
  @formula = formula
  @status_output_success_type = T.let(nil, T.nilable(StatusOutputSuccessType))

  return if System.launchctl? || System.systemctl?

  raise UsageError, System::MISSING_DAEMON_MANAGER_EXCEPTION_MESSAGE
end

Instance Attribute Details

#formulaFormula (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Access the Formula instance.

Returns:



15
16
17
# File 'services/formula_wrapper.rb', line 15

def formula
  @formula
end

Class Method Details

.from(path_or_label) ⇒ FormulaWrapper?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Create a new Service instance from either a path or label.

Parameters:

Returns:



19
20
21
22
23
24
25
26
27
# File 'services/formula_wrapper.rb', line 19

def self.from(path_or_label)
  return unless path_or_label =~ path_or_label_regex

  begin
    new(Formulary.factory(T.must(Regexp.last_match(1))))
  rescue
    nil
  end
end

Instance Method Details

#destPathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Path to destination service. If run as root, it's in boot_path, else user_path.

Returns:



137
138
139
# File 'services/formula_wrapper.rb', line 137

def dest
  dest_dir + service_file.basename
end

#dest_dirPathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Path to destination service directory. If run as root, it's boot_path, else user_path.

Returns:



131
132
133
# File 'services/formula_wrapper.rb', line 131

def dest_dir
  System.root? ? System.boot_path : System.user_path
end

#error?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


215
216
217
218
219
# File 'services/formula_wrapper.rb', line 215

def error?
  return false if pid?

  (exit_code = self.exit_code).present? && !exit_code.zero?
end

#exit_codeInteger?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Get current exit code of daemon process from status output.

Returns:



234
235
236
# File 'services/formula_wrapper.rb', line 234

def exit_code
  Regexp.last_match(1).to_i if status_output =~ exit_code_regex(status_type)
end

#installed?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if any version of the formula is installed.

Returns:

  • (Boolean)


143
144
145
# File 'services/formula_wrapper.rb', line 143

def installed?
  formula.any_version_installed?
end

#keep_alive?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Delegate access to formula.service.keep_alive?.

Returns:

  • (Boolean)


63
64
65
66
67
68
# File 'services/formula_wrapper.rb', line 63

def keep_alive?
  return @keep_alive unless @keep_alive.nil?

  @keep_alive = T.let(service? && load_service.keep_alive?, T.nilable(T::Boolean))
  @keep_alive ||= false
end

#loaded?(cached: false) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if the service is loaded, else false.

Parameters:

  • cached (Boolean) (defaults to: false)

Returns:

  • (Boolean)


167
168
169
170
171
172
173
174
# File 'services/formula_wrapper.rb', line 167

def loaded?(cached: false)
  if System.launchctl?
    reset_cache! unless cached
    status_success
  else # System.systemctl?
    System::Systemctl.quiet_run("status", timed? ? timer_name : service_file.basename)
  end
end

#loaded_fileString?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



239
240
241
# File 'services/formula_wrapper.rb', line 239

def loaded_file
  Regexp.last_match(1) if status_output =~ loaded_file_regex(status_type)
end

#nameString

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Delegate access to formula.name.

Returns:



42
43
44
# File 'services/formula_wrapper.rb', line 42

def name
  @name ||= T.let(formula.name, T.nilable(String))
end

#ownerString?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'services/formula_wrapper.rb', line 191

def owner
  if System.launchctl? && dest.exist?
    # read the username from the plist file
    plist = begin
      Plist.parse_xml(dest.read, marshal: false)
    rescue
      nil
    end
    plist_username = plist["UserName"] if plist

    return plist_username if plist_username.present?
  end
  return "root" if boot_path_service_file_present?
  return System.user if user_path_service_file_present?

  nil
end

#path_dirsArray<Pathname>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



71
72
73
74
75
# File 'services/formula_wrapper.rb', line 71

def path_dirs
  return [] unless service?

  load_service.path_dirs
end

#pidInteger?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Get current PID of daemon process from status output.

Returns:



228
229
230
# File 'services/formula_wrapper.rb', line 228

def pid
  Regexp.last_match(1).to_i if status_output =~ pid_regex(status_type)
end

#pid?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


210
211
212
# File 'services/formula_wrapper.rb', line 210

def pid?
  (pid = self.pid).present? && pid.positive?
end

#plist?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if the plist file exists.

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
158
# File 'services/formula_wrapper.rb', line 149

def plist?
  return false unless installed?
  return true if service_file.file?
  return false unless formula.opt_prefix.exist?
  return true if Keg.for(formula.opt_prefix).plist_installed?

  false
rescue NotAKegError
  false
end

#reset_cache!void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



161
162
163
# File 'services/formula_wrapper.rb', line 161

def reset_cache!
  @status_output_success_type = nil
end

#service?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Delegate access to formula.service?.

Returns:

  • (Boolean)


48
49
50
# File 'services/formula_wrapper.rb', line 48

def service?
  @service ||= T.let(formula.service?, T.nilable(T::Boolean))
end

#service_filePathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

service_file delegates with formula.launchd_service_path or formula.systemd_service_path for systemd.

Returns:



92
93
94
95
96
97
98
99
100
# File 'services/formula_wrapper.rb', line 92

def service_file
  @service_file ||= T.let(
    if System.launchctl?
      formula.launchd_service_path
    else # System.systemctl?
      formula.systemd_service_path
    end, T.nilable(Pathname)
  )
end

#service_file_present?(type: nil) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if service is present (e.g. .plist is present in boot or user service path), else false Accepts type with values :root for boot path or :user for user path.

Parameters:

  • type (Symbol, nil) (defaults to: nil)

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
188
# File 'services/formula_wrapper.rb', line 179

def service_file_present?(type: nil)
  case type
  when :root
    boot_path_service_file_present?
  when :user
    user_path_service_file_present?
  else
    boot_path_service_file_present? || user_path_service_file_present?
  end
end

#service_nameString

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

service_name delegates with formula.plist_name or formula.service_name for systemd (e.g., homebrew.<formula>).

Returns:



80
81
82
83
84
85
86
87
88
# File 'services/formula_wrapper.rb', line 80

def service_name
  @service_name ||= T.let(
    if System.launchctl?
      formula.plist_name
    else # System.systemctl?
      formula.service_name
    end, T.nilable(String)
  )
end

#service_startup?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Whether the service should be launched at startup

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
# File 'services/formula_wrapper.rb', line 119

def service_startup?
  @service_startup ||= T.let(
    if service?
      load_service.requires_root?
    else
      false
    end, T.nilable(T::Boolean)
  )
end

#timed?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Delegate access to formula.service.timed?.

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'services/formula_wrapper.rb', line 54

def timed?
  return @timed unless @timed.nil?

  @timed = T.let(service? && load_service.timed?, T.nilable(T::Boolean))
  @timed ||= false
end

#timer_destPathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



113
114
115
# File 'services/formula_wrapper.rb', line 113

def timer_dest
  dest_dir + timer_file.basename
end

#timer_filePathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



103
104
105
# File 'services/formula_wrapper.rb', line 103

def timer_file
  @timer_file ||= T.let(formula.systemd_timer_path, T.nilable(Pathname))
end

#timer_nameString

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



108
109
110
# File 'services/formula_wrapper.rb', line 108

def timer_name
  @timer_name ||= T.let(timer_file.basename.to_s, T.nilable(String))
end

#to_hashHash{Symbol => T.anything}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'services/formula_wrapper.rb', line 244

def to_hash
  hash = {
    name:,
    service_name:,
    running:      pid?,
    loaded:       loaded?(cached: true),
    schedulable:  timed?,
    pid:,
    exit_code:,
    user:         owner,
    status:       status_symbol,
    file:         service_file_present? ? dest : service_file,
    registered:   service_file_present?,
    loaded_file:,
  }

  return hash unless service?

  service = load_service

  return hash if service.command.blank?

  hash[:command] = service.manual_command
  hash[:working_dir] = service.working_dir
  hash[:root_dir] = service.root_dir
  hash[:log_path] = service.log_path
  hash[:error_log_path] = service.error_log_path
  hash[:interval] = service.interval
  hash[:cron] = service.cron.presence

  hash
end

#unknown_status?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


222
223
224
# File 'services/formula_wrapper.rb', line 222

def unknown_status?
  status_output.blank? && !pid?
end