Skip to content

Commit 7499244

Browse files
authored
Merge pull request #19703 from Homebrew/bundle-exec-simultaneous-services
bundle: handle simultaneous `exec --services` better
2 parents 79d5115 + 1ef00cb commit 7499244

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

Library/Homebrew/bundle/commands/exec.rb

+12-4
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]],
@@ -245,15 +246,20 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false)
245246

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

248-
yield info, service_file, conflicting_services
249+
yield entry, info, service_file, conflicting_services
249250
end
250251
end
251252

252253
sig { params(entries: T::Array[Homebrew::Bundle::Dsl::Entry], _block: T.nilable(T.proc.void)).void }
253254
private_class_method def self.run_services(entries, &_block)
255+
entries_to_stop = []
254256
services_to_restart = []
255257

256-
map_service_info(entries) do |info, service_file, conflicting_services|
258+
map_service_info(entries) do |entry, info, service_file, conflicting_services|
259+
# Don't restart if already running this version
260+
loaded_file = Pathname.new(info["loaded_file"].to_s)
261+
next if info["running"] && loaded_file&.file? && loaded_file&.realpath == service_file.realpath
262+
257263
if info["running"] && !Bundle::BrewServices.stop(info["name"], keep: true)
258264
opoo "Failed to stop #{info["name"]} service"
259265
end
@@ -269,6 +275,8 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false)
269275
unless Bundle::BrewServices.run(info["name"], file: service_file)
270276
opoo "Failed to start #{info["name"]} service"
271277
end
278+
279+
entries_to_stop << entry
272280
end
273281

274282
return unless block_given?
@@ -277,7 +285,7 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false)
277285
yield
278286
ensure
279287
# Do a full re-evaluation of services instead state has changed
280-
stop_services(entries)
288+
stop_services(entries_to_stop)
281289

282290
services_to_restart.each do |service|
283291
next if Bundle::BrewServices.run(service)
@@ -289,7 +297,7 @@ def self.run(*args, global: false, file: nil, subcommand: "", services: false)
289297

290298
sig { params(entries: T::Array[Homebrew::Bundle::Dsl::Entry]).void }
291299
private_class_method def self.stop_services(entries)
292-
map_service_info(entries) do |info, _, _|
300+
map_service_info(entries) do |_, info, _, _|
293301
next unless info["loaded"]
294302

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

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

+1
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)