Skip to content

Commit 25214f1

Browse files
authored
Merge pull request #19714 from Homebrew/bundle-sim-services-2
Reland "bundle: handle simultaneous exec --services better"
2 parents 568b707 + 80f5c42 commit 25214f1

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Library/Homebrew/bundle/commands/exec.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false)
179179
params(
180180
entries: T::Array[Homebrew::Bundle::Dsl::Entry],
181181
_block: T.proc.params(
182+
entry: Homebrew::Bundle::Dsl::Entry,
182183
info: T::Hash[String, T.anything],
183184
service_file: Pathname,
184185
conflicting_services: T::Array[T::Hash[String, T.anything]],
@@ -195,6 +196,8 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false)
195196
[entry, formula]
196197
end.to_h
197198

199+
return if entries_formulae.empty?
200+
198201
conflicts = entries_formulae.to_h do |entry, formula|
199202
[
200203
entry,
@@ -245,15 +248,20 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false)
245248

246249
raise "Failed to get service info for #{entry.name}" if info.nil?
247250

248-
yield info, service_file, conflicting_services
251+
yield entry, info, service_file, conflicting_services
249252
end
250253
end
251254

252255
sig { params(entries: T::Array[Homebrew::Bundle::Dsl::Entry], _block: T.nilable(T.proc.void)).void }
253256
private_class_method def self.run_services(entries, &_block)
257+
entries_to_stop = []
254258
services_to_restart = []
255259

256-
map_service_info(entries) do |info, service_file, conflicting_services|
260+
map_service_info(entries) do |entry, info, service_file, conflicting_services|
261+
# Don't restart if already running this version
262+
loaded_file = Pathname.new(info["loaded_file"].to_s)
263+
next if info["running"] && loaded_file&.file? && loaded_file&.realpath == service_file.realpath
264+
257265
if info["running"] && !Bundle::BrewServices.stop(info["name"], keep: true)
258266
opoo "Failed to stop #{info["name"]} service"
259267
end
@@ -269,6 +277,8 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false)
269277
unless Bundle::BrewServices.run(info["name"], file: service_file)
270278
opoo "Failed to start #{info["name"]} service"
271279
end
280+
281+
entries_to_stop << entry
272282
end
273283

274284
return unless block_given?
@@ -277,7 +287,7 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false)
277287
yield
278288
ensure
279289
# Do a full re-evaluation of services instead state has changed
280-
stop_services(entries)
290+
stop_services(entries_to_stop)
281291

282292
services_to_restart.each do |service|
283293
next if Bundle::BrewServices.run(service)
@@ -289,7 +299,7 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false)
289299

290300
sig { params(entries: T::Array[Homebrew::Bundle::Dsl::Entry]).void }
291301
private_class_method def self.stop_services(entries)
292-
map_service_info(entries) do |info, _, _|
302+
map_service_info(entries) do |_, info, _, _|
293303
next unless info["loaded"]
294304

295305
# Try avoid services not started by `brew bundle services`

Library/Homebrew/test/bundle/commands/exec_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
allow(pkgconf).to receive(:any_version_installed?).and_return(false)
186186

187187
allow_any_instance_of(Pathname).to receive(:file?).and_return(true)
188+
allow_any_instance_of(Pathname).to receive(:realpath) { |path| path }
188189

189190
allow(described_class).to receive(:exit!).and_return(nil)
190191
end

0 commit comments

Comments
 (0)