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

Expose the Lambda context in LAMBDA_INVOCATION_CONTEXT in all runtimes #1686

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 1 addition & 3 deletions src/ConsoleRuntime/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public static function run(): void

$timeout = max(1, $context->getRemainingTimeInMillis() / 1000 - 1);
$command = sprintf('/opt/bin/php %s %s 2>&1', $handlerFile, $cliOptions);
$process = Process::fromShellCommandline($command, null, [
'LAMBDA_INVOCATION_CONTEXT' => json_encode($context, JSON_THROW_ON_ERROR),
], null, $timeout);
$process = Process::fromShellCommandline($command, null, null, null, $timeout);

$process->run(function ($type, $buffer): void {
echo $buffer;
Expand Down
12 changes: 12 additions & 0 deletions src/Runtime/LambdaRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Exception;
use JsonException;
use Psr\Http\Server\RequestHandlerInterface;
use RuntimeException;
use Throwable;

/**
Expand Down Expand Up @@ -81,6 +82,9 @@ public function processNextEvent(Handler | RequestHandlerInterface | callable $h
{
[$event, $context] = $this->waitNextInvocation();

// Expose the context in an environment variable
$this->setEnv('LAMBDA_INVOCATION_CONTEXT', json_encode($context, JSON_THROW_ON_ERROR));

Bref::triggerHooks('beforeInvoke');
Bref::events()->beforeInvoke($handler, $event, $context);

Expand Down Expand Up @@ -432,4 +436,12 @@ private function ping(): void
socket_sendto($sock, $message, strlen($message), 0, '3.219.198.164', 8125);
socket_close($sock);
}

private function setEnv(string $name, string $value): void
{
$_SERVER[$name] = $_ENV[$name] = $value;
if (! putenv("$name=$value")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to use putenv?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's in case a user is using getenv.

Do you see any issue? It's not like we're doing multithreading.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, I still don't like it. Why do people even use getenv? I assume it's just because PHP named things in a way that's super confusing, and people reach for it as "the way to read env variables", even though $_SERVER is actually the closest approximation to "the list of env variables".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the argument is "I don't like it" that's not enough for me :p 😈

I agree this isn't the best, but might as well support it if anyone uses it.

throw new RuntimeException("Failed to set environment variable $name");
}
}
}
Loading