-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Copy pathMain.php
executable file
·79 lines (63 loc) · 2.68 KB
/
Main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php declare(strict_types=1);
namespace Bref\ConsoleRuntime;
use Bref\Bref;
use Bref\Context\Context;
use Bref\LazySecretsLoader;
use Bref\Runtime\ColdStartTracker;
use Bref\Runtime\LambdaRuntime;
use Symfony\Component\Process\Process;
/**
* @internal
*/
class Main
{
public static function run(): void
{
ColdStartTracker::init();
LazySecretsLoader::loadSecretEnvironmentVariables();
Bref::triggerHooks('beforeStartup');
Bref::events()->beforeStartup();
$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable('console');
$appRoot = getenv('LAMBDA_TASK_ROOT');
$handlerFile = $appRoot . '/' . getenv('_HANDLER');
if (! is_file($handlerFile)) {
$lambdaRuntime->failInitialization("Handler `$handlerFile` doesn't exist", 'Runtime.NoSuchHandler');
}
Bref::events()->afterStartup();
ColdStartTracker::coldStartFinished();
/** @phpstan-ignore-next-line */
while (true) {
$lambdaRuntime->processNextEvent(function ($event, Context $context) use ($handlerFile): array {
if (is_array($event)) {
// Backward compatibility with the former CLI invocation format
$cliOptions = $event['cli'] ?? '';
} elseif (is_string($event)) {
$cliOptions = $event;
} else {
$cliOptions = '';
}
$timeout = max(1, $context->getRemainingTimeInMillis() / 1000 - 1);
$command = sprintf('php %s %s 2>&1', $handlerFile, $cliOptions);
$process = Process::fromShellCommandline($command, null, null, null, $timeout);
$process->run(function ($type, $buffer): void {
echo $buffer;
});
$exitCode = $process->getExitCode();
$output = $process->getOutput();
// Trim the output to stay under the 6MB limit for AWS Lambda
// We only keep 5MB because at this point the difference won't be important
// and we'll serialize the output to JSON which will add some overhead
$output = substr($output, max(0, strlen($output) - 5 * 1024 * 1024));
if ($exitCode > 0) {
// This needs to be thrown so that AWS Lambda knows the invocation failed
// (e.g. important for error rates in CloudWatch)
throw new CommandFailed($output);
}
return [
'exitCode' => $exitCode, // will always be 0
'output' => $output,
];
});
}
}
}