Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes global environment variables not being taken in by workers #257

Merged
merged 2 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Commands/Concerns/InteractsWithEnvironmentVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Laravel\Octane\Commands\Concerns;

use Dotenv\Exception\InvalidPathException;
use Dotenv\Parser\Parser;
use Dotenv\Store\StoreBuilder;
use Illuminate\Support\Env;

trait InteractsWithEnvironmentVariables
{
/**
* Forgets the current process environment variables.
*
* @return void
*/
public function forgetEnvironmentVariables()
{
$variables = collect();

try {
$content = StoreBuilder::createWithNoNames()
->addPath(app()->environmentPath())
->addName(app()->environmentFile())
->make()
->read();

foreach ((new Parser())->parse($content) as $entry) {
$variables->push($entry->getName());
}
} catch (InvalidPathException $e) {
// ..
}

$variables->each(fn ($name) => Env::getRepository()->clear($name));
}
}
16 changes: 9 additions & 7 deletions src/Commands/StartRoadRunnerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

class StartRoadRunnerCommand extends Command implements SignalableCommandInterface
{
use Concerns\InstallsRoadRunnerDependencies, Concerns\InteractsWithServers;
use Concerns\InstallsRoadRunnerDependencies,
Concerns\InteractsWithServers,
Concerns\InteractsWithEnvironmentVariables;

/**
* The command's signature.
Expand Down Expand Up @@ -69,6 +71,8 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve

touch(base_path('.rr.yaml'));

$this->forgetEnvironmentVariables();

$server = tap(new Process(array_filter([
$roadRunnerBinary,
'-c', base_path('.rr.yaml'),
Expand All @@ -85,13 +89,11 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve
'-o', 'logs.output=stdout',
'-o', 'logs.encoding=json',
'--dotenv=""', 'serve',
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
]), base_path(), collect(array_merge($_ENV, [
]), base_path(), [
'APP_ENV' => app()->environment(),
'APP_BASE_PATH' => base_path(),
'LARAVEL_OCTANE' => 1, ]))->mapWithKeys(function ($value, $key) {
return in_array($key, ['APP_ENV', 'APP_BASE_PATH', 'LARAVEL_OCTANE'])
? [$key => $value]
: [$key => false];
})->all(), null, null))->start();
'LARAVEL_OCTANE' => 1,
]))->start();

$serverStateFile->writeProcessId($server->getPid());

Expand Down
14 changes: 7 additions & 7 deletions src/Commands/StartSwooleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class StartSwooleCommand extends Command implements SignalableCommandInterface
{
use Concerns\InteractsWithServers;
use Concerns\InteractsWithServers, Concerns\InteractsWithEnvironmentVariables;

/**
* The command's signature.
Expand Down Expand Up @@ -68,15 +68,15 @@ public function handle(

$this->writeServerStateFile($serverStateFile, $extension);

$this->forgetEnvironmentVariables();

$server = tap(new Process([
(new PhpExecutableFinder)->find(), 'swoole-server', $serverStateFile->path(),
], realpath(__DIR__.'/../../bin'), collect(array_merge($_ENV, [
], realpath(__DIR__.'/../../bin'), [
'APP_ENV' => app()->environment(),
'APP_BASE_PATH' => base_path(),
'LARAVEL_OCTANE' => 1, ]))->mapWithKeys(function ($value, $key) {
return in_array($key, ['APP_ENV', 'APP_BASE_PATH', 'LARAVEL_OCTANE'])
? [$key => $value]
: [$key => false];
})->all(), null, null))->start();
'LARAVEL_OCTANE' => 1,
]))->start();

return $this->runServer($server, $inspector, 'swoole');
}
Expand Down