Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Ensure PWD is propagated to workers in mode SWOOLE_PROCESS
Browse files Browse the repository at this point in the history
During testing, I discovered that when running in mode SWOOLE_PROCESS,
the issue in #15 resurfaced. The solution was to add a `workerstart`
event that resets the PWD based on what is present in the
`RequestHandlerSwooleRunner`. The listener also logs the worker process
ID.

Fixes #15
  • Loading branch information
weierophinney committed Aug 28, 2018
1 parent a7835de commit 8ff24fd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
43 changes: 33 additions & 10 deletions src/RequestHandlerSwooleRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ class RequestHandlerSwooleRunner extends RequestHandlerRunner
*/
private $staticResourceHandler;

/**
* @throws Exception\InvalidConfigException if the configured or default
* document root does not exist.
*/
public function __construct(
RequestHandlerInterface $handler,
callable $serverRequestFactory,
Expand Down Expand Up @@ -139,6 +135,10 @@ function (Throwable $exception) use ($serverRequestErrorResponseGenerator) : Res

/**
* Run the application
*
* Determines which action was requested from the command line, and then
* executes the task associated with it. If no action was provided, it
* assumes "start".
*/
public function run() : void
{
Expand All @@ -165,20 +165,21 @@ public function run() : void
}

/**
* Start swoole server
* Start the swoole HTTP server
*
* @param array $options Swoole server options
*/
public function startServer(array $options = []) : void
{
$swooleServer = $this->serverFactory->createSwooleServer($options);
$swooleServer->on('start', [$this, 'onStart']);
$swooleServer->on('workerstart', [$this, 'onWorkerStart']);
$swooleServer->on('request', [$this, 'onRequest']);
$swooleServer->start();
}

/**
* Stop swoole server
* Stop the swoole HTTP server
*/
public function stopServer() : void
{
Expand Down Expand Up @@ -231,7 +232,7 @@ public function reloadWorker() : void
}

/**
* Is swoole server running ?
* Is the swoole HTTP server running?
*/
public function isRunning() : bool
{
Expand All @@ -245,21 +246,43 @@ public function isRunning() : bool
}

/**
* On start event for swoole http server
* Handle a start event for swoole HTTP server manager process.
*
* Writes the master and manager PID values to the PidManager, and ensures
* the manager and/or workers use the same PWD as the master process.
*/
public function onStart(SwooleHttpServer $server) : void
{
$this->pidManager->write($server->master_pid, $server->manager_pid);
$this->logger->info('Swoole is running at {host}:{port}', [

// Reset CWD
chdir($this->cwd);

$this->logger->info('Swoole is running at {host}:{port}, in {cwd}', [
'host' => $server->host,
'port' => $server->port,
'cwd' => getcwd(),
]);
}

/**
* Handle a workerstart event for swoole HTTP server worker process
*
* Ensures workers all use the same PWD as the master process.
*/
public function onWorkerStart(SwooleHttpServer $server, int $workerId) : void
{
// Reset CWD
chdir($this->cwd);

$this->logger->info('Worker started in {cwd} with ID {pid}', [
'cwd' => getcwd(),
'pid' => $workerId,
]);
}

/**
* On HTTP request event for swoole http server
* Handle an incoming HTTP request
*/
public function onRequest(
SwooleHttpRequest $request,
Expand Down
11 changes: 9 additions & 2 deletions test/RequestHandlerSwooleRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public function testRun()
$swooleServer->expects($this->once())
->method('start');

$swooleServer->expects($this->exactly(2))
// Listeners are attached to each of:
// - start
// - workerstart
// - request
$swooleServer->expects($this->exactly(3))
->method('on');

// Clear command options, like phpunit --colors=always
Expand All @@ -129,7 +133,10 @@ public function testOnStart()
);

$runner->onStart($swooleServer = $this->createMock(SwooleHttpServer::class));
$this->expectOutputString("Swoole is running at :0\n");
$this->expectOutputString(sprintf(
"Swoole is running at :0, in %s\n",
getcwd()
));
}

public function testOnRequestDelegatesToApplicationWhenNoStaticResourceHandlerPresent()
Expand Down

0 comments on commit 8ff24fd

Please sign in to comment.