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

#odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled

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:



111
112
113
# File 'services/formula_wrapper.rb', line 111

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:



105
106
107
# File 'services/formula_wrapper.rb', line 105

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)


189
190
191
192
193
# File 'services/formula_wrapper.rb', line 189

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:

  • (Integer, nil)


208
209
210
# File 'services/formula_wrapper.rb', line 208

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)


117
118
119
# File 'services/formula_wrapper.rb', line 117

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.

TODO:

this should either be T::Boolean or renamed to keep_alive

Delegate access to formula.service.keep_alive?.

Returns:

  • (Boolean, nil)


62
63
64
# File 'services/formula_wrapper.rb', line 62

def keep_alive?
  @keep_alive ||= T.let((load_service.keep_alive? if service?), T.nilable(T::Boolean))
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)


141
142
143
144
145
146
147
148
# File 'services/formula_wrapper.rb', line 141

def loaded?(cached: false)
  if System.launchctl?
    reset_cache! unless cached
    status_success
  else # System.systemctl?
    System::Systemctl.quiet_run("status", 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:



213
214
215
# File 'services/formula_wrapper.rb', line 213

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:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'services/formula_wrapper.rb', line 165

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

#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:

  • (Integer, nil)


202
203
204
# File 'services/formula_wrapper.rb', line 202

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)


184
185
186
# File 'services/formula_wrapper.rb', line 184

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)


123
124
125
126
127
128
129
130
131
132
# File 'services/formula_wrapper.rb', line 123

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.



135
136
137
# File 'services/formula_wrapper.rb', line 135

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:



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

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)


153
154
155
156
157
158
159
160
161
162
# File 'services/formula_wrapper.rb', line 153

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:



69
70
71
72
73
74
75
76
77
# File 'services/formula_wrapper.rb', line 69

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)


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

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.

TODO:

this should either be T::Boolean or renamed to timed

Delegate access to formula.service.timed?.

Returns:

  • (Boolean, nil)


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

def timed?
  @timed ||= T.let((load_service.timed? if service?), T.nilable(T::Boolean))
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:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'services/formula_wrapper.rb', line 218

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)


196
197
198
# File 'services/formula_wrapper.rb', line 196

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