From 03b41854e421913b845de4c8449d5ef297642ad8 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 5 Oct 2023 17:07:28 +0100 Subject: [PATCH] Add `--no-wait` argument This prevents us from looping and waiting for `brew services stop` to finish stopping the service if we just want to attempt once and immediately exit. While we're here, also fix verbose output for `brew services restart`. --- cmd/services.rb | 3 ++- lib/service/commands/restart.rb | 6 +++--- lib/service/commands/stop.rb | 4 ++-- lib/service/services_cli.rb | 19 ++++++++++++++----- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cmd/services.rb b/cmd/services.rb index 80a855cec8..f4e3481328 100755 --- a/cmd/services.rb +++ b/cmd/services.rb @@ -45,6 +45,7 @@ def services_args flag "--sudo-service-user=", description: "When run as root on macOS, run the service(s) as this user." switch "--all", description: "Run on all services." switch "--json", description: "Output as JSON." + switch "--no-wait", description: "Don't wait for `stop` to finish stopping the service." named_args max: 2 end end @@ -124,7 +125,7 @@ def services when *::Service::Commands::Start::TRIGGERS ::Service::Commands::Start.run(targets, args.file, verbose: args.verbose?) when *::Service::Commands::Stop::TRIGGERS - ::Service::Commands::Stop.run(targets, verbose: args.verbose?) + ::Service::Commands::Stop.run(targets, verbose: args.verbose?, no_wait: args.no_wait?) when *::Service::Commands::Kill::TRIGGERS ::Service::Commands::Kill.run(targets, verbose: args.verbose?) else diff --git a/lib/service/commands/restart.rb b/lib/service/commands/restart.rb index 6e58770989..10dc24f74c 100644 --- a/lib/service/commands/restart.rb +++ b/lib/service/commands/restart.rb @@ -26,11 +26,11 @@ def run(targets, custom_plist, verbose:) # group not-started services with started ones for restart started << service end - ServicesCli.stop([service]) if service.loaded? + ServicesCli.stop([service], verbose: verbose) if service.loaded? end - ServicesCli.run(ran) if ran.present? - ServicesCli.start(started) if started.present? + ServicesCli.run(ran, verbose: verbose) if ran.present? + ServicesCli.start(started, verbose: verbose) if started.present? end end end diff --git a/lib/service/commands/stop.rb b/lib/service/commands/stop.rb index bc524a33fc..ce39f61c1d 100644 --- a/lib/service/commands/stop.rb +++ b/lib/service/commands/stop.rb @@ -7,9 +7,9 @@ module Stop TRIGGERS = %w[stop unload terminate term t u].freeze - def run(targets, verbose:) + def run(targets, verbose:, no_wait:) ServicesCli.check(targets) && - ServicesCli.stop(targets, verbose: verbose) + ServicesCli.stop(targets, verbose: verbose, no_wait: no_wait) end end end diff --git a/lib/service/services_cli.rb b/lib/service/services_cli.rb index c11de30f5b..81e5ee89a7 100644 --- a/lib/service/services_cli.rb +++ b/lib/service/services_cli.rb @@ -124,7 +124,7 @@ def start(targets, service_file = nil, verbose: false) end # Stop a service and unload it. - def stop(targets, verbose: false) + def stop(targets, verbose: false, no_wait: false) targets.each do |service| unless service.loaded? rm service.dest if service.dest.exist? # get rid of installed service file anyway, dude @@ -142,14 +142,23 @@ def stop(targets, verbose: false) next end - puts "Stopping `#{service.name}`... (might take a while)" + systemctl_args = System.systemctl_args + if no_wait + systemctl_args << "--no-block" + puts "Stopping `#{service.name}`..." + else + puts "Stopping `#{service.name}`... (might take a while)" + end + if System.systemctl? quiet_system(*System.systemctl_args, "disable", "--now", service.service_name) elsif System.launchctl? quiet_system System.launchctl, "bootout", "#{System.domain_target}/#{service.service_name}" - while $CHILD_STATUS.to_i == 9216 || service.loaded? - sleep(1) - quiet_system System.launchctl, "bootout", "#{System.domain_target}/#{service.service_name}" + unless no_wait + while $CHILD_STATUS.to_i == 9216 || service.loaded? + sleep(1) + quiet_system System.launchctl, "bootout", "#{System.domain_target}/#{service.service_name}" + end end quiet_system System.launchctl, "stop", "#{System.domain_target}/#{service.service_name}" if service.pid? end