Skip to content

Commit

Permalink
Fixes global environment variables not being taken in by workers (#257)
Browse files Browse the repository at this point in the history
* Fixes the way Octane handles environment variables

* Updates changelog
  • Loading branch information
nunomaduro authored Apr 30, 2021
1 parent f54f249 commit a10804d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased](https://github.com/laravel/octane/compare/v0.4.0...master)

> **Requires to stop, and re-start your Octane server**
### Fixed
- Global environment variables not being taken in by workers ([#257](https://github.com/laravel/octane/pull/257))

## [v0.4.0 (2021-04-27)](https://github.com/laravel/octane/compare/v0.3.2...v0.4.0)

Expand Down
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',
]), 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

0 comments on commit a10804d

Please sign in to comment.