Skip to content

Commit fbc86fb

Browse files
smortexanunomadurotaylorotwell
authored
Ensure HTTP server port is available (#852)
* Ensure HTTP server port is available * Uses specific code for frankenphp * Update InteractsWithServers.php * Update StartRoadRunnerCommand.php --------- Co-authored-by: Nuno Maduro <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 518e768 commit fbc86fb

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

src/Commands/Concerns/InteractsWithServers.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ protected function getPort()
144144
return $this->option('port') ?? config('octane.port') ?? $_ENV['OCTANE_PORT'] ?? '8000';
145145
}
146146

147+
/**
148+
* Ensure the Octane HTTP server port is available.
149+
*
150+
* @return void
151+
*/
152+
protected function ensurePortIsAvailable(): void
153+
{
154+
$host = $this->getHost();
155+
156+
$port = $this->getPort();
157+
158+
$connection = @fsockopen($host, $port);
159+
160+
if (is_resource($connection)) {
161+
@fclose($connection);
162+
163+
throw new InvalidArgumentException("Unable to start server. Port {$port} is already in use.");
164+
}
165+
}
166+
147167
/**
148168
* Returns the list of signals to subscribe.
149169
*/

src/Commands/StartFrankenPhpCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class StartFrankenPhpCommand extends Command implements SignalableCommandInterfa
5959
public function handle(ServerProcessInspector $inspector, ServerStateFile $serverStateFile)
6060
{
6161
$this->ensureFrankenPhpWorkerIsInstalled();
62-
$this->ensureHostsAreAvailable();
62+
$this->ensurePortIsAvailable();
6363

6464
$frankenphpBinary = $this->ensureFrankenPhpBinaryIsInstalled();
6565

@@ -116,7 +116,7 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve
116116
*
117117
* @return void
118118
*/
119-
protected function ensureHostsAreAvailable()
119+
protected function ensurePortIsAvailable()
120120
{
121121
$host = $this->getHost();
122122

src/Commands/StartRoadRunnerCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve
6464

6565
$roadRunnerBinary = $this->ensureRoadRunnerBinaryIsInstalled();
6666

67+
$this->ensurePortIsAvailable();
68+
6769
if ($inspector->serverIsRunning()) {
6870
$this->error('RoadRunner server is already running.');
6971

src/Commands/StartSwooleCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public function handle(
6060
return 1;
6161
}
6262

63+
$this->ensurePortIsAvailable();
64+
6365
if ($inspector->serverIsRunning()) {
6466
$this->error('Server is already running.');
6567

0 commit comments

Comments
 (0)