-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
194 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Concerns; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
|
||
trait InteractsWithServerState | ||
{ | ||
/** | ||
* The server state cache key. | ||
* | ||
* @var string | ||
*/ | ||
protected $key = 'reverb:server'; | ||
|
||
/** | ||
* Deteremine whether the server is running. | ||
*/ | ||
protected function serverIsRunning(): bool | ||
{ | ||
return Cache::has($this->key); | ||
} | ||
|
||
/** | ||
* Get the current state of the running server. | ||
* | ||
* @return null|array{HOST: string, PORT: int, DEBUG: bool, RESTART: bool} | ||
*/ | ||
protected function getState(): ?array | ||
{ | ||
return Cache::get($this->key); | ||
} | ||
|
||
/** | ||
* Set the state of the running server. | ||
*/ | ||
protected function setState(string $host, int $port, bool $debug, bool $restart = false): void | ||
{ | ||
Cache::forever($this->key, [ | ||
'HOST' => $host, | ||
'PORT' => $port, | ||
'DEBUG' => $debug ??= false, | ||
'RESTART' => $restart, | ||
]); | ||
} | ||
|
||
/** | ||
* Destroy the server state. | ||
*/ | ||
protected function destroyState(): void | ||
{ | ||
Cache::forget($this->key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Laravel\Reverb\Servers\Reverb\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Laravel\Reverb\Concerns\InteractsWithServerState; | ||
|
||
class RestartServer extends Command | ||
{ | ||
use InteractsWithServerState; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'reverb:restart'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Signal Reverb to restart the server'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): void | ||
{ | ||
if (! $state = $this->getState()) { | ||
$this->error('No Reverb server running.'); | ||
|
||
return; | ||
} | ||
|
||
$this->sendStopSignal($state); | ||
|
||
$this->waitForServerToStop(fn () => $this->call('reverb:start', [ | ||
'--host' => $state['HOST'], | ||
'--port' => $state['PORT'], | ||
'--debug' => $state['DEBUG'], | ||
])); | ||
} | ||
|
||
/** | ||
* Send the stop signal to the running server. | ||
* | ||
* @param array{HOST: string, PORT: int, DEBUG: bool, RESTART: bool} $state | ||
*/ | ||
protected function sendStopSignal(array $state): void | ||
{ | ||
$this->components->info('Sending stop signal to Reverb server.'); | ||
|
||
$this->setState($state['HOST'], $state['PORT'], $state['DEBUG'], true); | ||
} | ||
|
||
/** | ||
* Run the callback when the server has stopped. | ||
*/ | ||
protected function waitForServerToStop(callable $callback): void | ||
{ | ||
while ($this->serverIsRunning()) { | ||
usleep(1000); | ||
} | ||
|
||
$callback(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters