Skip to content

Commit c549c17

Browse files
committed
Stop non-root homebrew services during restarts
This is intended to assist with avoiding having competing non-root instances of valet dependency services running due to userland use of brew CLI commands. Now, when running `valet stop` or `valet restart` it will (silently) attempt to stop the non-root instance (ignoring any errors thrown), before stopping (and/or starting) the sudo/root instance which is normally used. (Though obvious, I'll point out that this only applies to services Valet manages.) Fixes #1057
1 parent 1aad1e0 commit c549c17

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

cli/Valet/Brew.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@ function restartService($services)
197197
if ($this->installed($service)) {
198198
info("Restarting {$service}...");
199199

200+
// first we ensure that the service is not incorrectly running as non-root
201+
$this->cli->quietly('brew services stop '.$service);
202+
// stop the actual/correct sudo version
200203
$this->cli->quietly('sudo brew services stop '.$service);
204+
// start correctly as root
201205
$this->cli->quietly('sudo brew services start '.$service);
202206
}
203207
}
@@ -216,6 +220,10 @@ function stopService($services)
216220
if ($this->installed($service)) {
217221
info("Stopping {$service}...");
218222

223+
// first we ensure that the service is not incorrectly running as non-root
224+
$this->cli->quietly('brew services stop '.$service);
225+
226+
// stop the sudo version
219227
$this->cli->quietly('sudo brew services stop '.$service);
220228
}
221229
}

tests/BrewTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function test_restart_restarts_the_service_using_homebrew_services()
117117
{
118118
$cli = Mockery::mock(CommandLine::class);
119119
$cli->shouldReceive('runAsUser')->once()->with('brew info dnsmasq --json')->andReturn('[{"name":"dnsmasq","full_name":"dnsmasq","aliases":[],"versioned_formulae":[],"versions":{"stable":"1"},"installed":[{"version":"1"}]}]');
120+
$cli->shouldReceive('quietly')->once()->with('brew services stop dnsmasq');
120121
$cli->shouldReceive('quietly')->once()->with('sudo brew services stop dnsmasq');
121122
$cli->shouldReceive('quietly')->once()->with('sudo brew services start dnsmasq');
122123
swap(CommandLine::class, $cli);
@@ -127,6 +128,7 @@ public function test_stop_stops_the_service_using_homebrew_services()
127128
{
128129
$cli = Mockery::mock(CommandLine::class);
129130
$cli->shouldReceive('runAsUser')->once()->with('brew info dnsmasq --json')->andReturn('[{"name":"dnsmasq","full_name":"dnsmasq","aliases":[],"versioned_formulae":[],"versions":{"stable":"1"},"installed":[{"version":"1"}]}]');
131+
$cli->shouldReceive('quietly')->once()->with('brew services stop dnsmasq');
130132
$cli->shouldReceive('quietly')->once()->with('sudo brew services stop dnsmasq');
131133
swap(CommandLine::class, $cli);
132134
resolve(Brew::class)->stopService('dnsmasq');

0 commit comments

Comments
 (0)