Skip to content

Commit

Permalink
Merge pull request #593 from MikeMcQuaid/no_wait
Browse files Browse the repository at this point in the history
Add `--no-wait` argument
  • Loading branch information
MikeMcQuaid authored Oct 6, 2023
2 parents dcb50f3 + 03b4185 commit f9d59c8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <subcommand> 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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/service/commands/restart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/service/commands/stop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions lib/service/services_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f9d59c8

Please sign in to comment.