Class: Homebrew::Services::FormulaWrapper Private

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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:



27
28
29
# File 'services/formula_wrapper.rb', line 27

def initialize(formula)
  @formula = formula
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:



11
12
13
# File 'services/formula_wrapper.rb', line 11

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:



15
16
17
18
19
20
21
22
23
# File 'services/formula_wrapper.rb', line 15

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

#destObject

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.



93
94
95
# File 'services/formula_wrapper.rb', line 93

def dest
  dest_dir + service_file.basename
end

#dest_dirObject

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.



88
89
90
# File 'services/formula_wrapper.rb', line 88

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)


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

def error?
  return false if pid?

  exit_code.present? && !exit_code.zero?
end

#exit_codeObject

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.



186
187
188
189
# File 'services/formula_wrapper.rb', line 186

def exit_code
  status_output, _, status_type = status_output_success_type
  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)


99
100
101
# File 'services/formula_wrapper.rb', line 99

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)


53
54
55
# File 'services/formula_wrapper.rb', line 53

def keep_alive?
  @keep_alive ||= (load_service.keep_alive? if service?)
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.

TODO:

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

Returns true if the service is loaded, else false.

Parameters:

  • cached (Boolean) (defaults to: false)

Returns:

  • (Boolean, nil)


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

def loaded?(cached: false)
  if System.launchctl?
    @status_output_success_type = nil unless cached
    _, status_success, = status_output_success_type
    status_success
  elsif System.systemctl?
    System::Systemctl.quiet_run("status", service_file.basename)
  end
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:



33
34
35
# File 'services/formula_wrapper.rb', line 33

def name
  @name ||= formula.name
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:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'services/formula_wrapper.rb', line 144

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

#pidObject

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.



180
181
182
183
# File 'services/formula_wrapper.rb', line 180

def pid
  status_output, _, status_type = status_output_success_type
  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)


163
164
165
# File 'services/formula_wrapper.rb', line 163

def pid?
  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)


105
106
107
108
109
110
111
112
113
114
# File 'services/formula_wrapper.rb', line 105

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

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


39
40
41
# File 'services/formula_wrapper.rb', line 39

def service?
  @service ||= @formula.service?
end

#service_fileObject

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.



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

def service_file
  @service_file ||= if System.launchctl?
    formula.launchd_service_path
  elsif System.systemctl?
    formula.systemd_service_path
  end
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)


132
133
134
135
136
137
138
139
140
141
# File 'services/formula_wrapper.rb', line 132

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:



60
61
62
63
64
65
66
# File 'services/formula_wrapper.rb', line 60

def service_name
  @service_name ||= if System.launchctl?
    formula.plist_name
  elsif System.systemctl?
    formula.service_name
  end
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)


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

def service_startup?
  @service_startup ||= if service?
    load_service.requires_root?
  else
    false
  end
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)


46
47
48
# File 'services/formula_wrapper.rb', line 46

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'services/formula_wrapper.rb', line 192

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?,
  }

  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

  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)


175
176
177
# File 'services/formula_wrapper.rb', line 175

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