forked from skylerkatz/hover
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Warmer.php
61 lines (50 loc) · 1.63 KB
/
Warmer.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
<?php
use Aws\Lambda\LambdaClient;
use GuzzleHttp\Promise\Utils;
class Warmer
{
public LambdaClient $client;
public function __construct()
{
$this->client = new LambdaClient([
'region' => $_ENV['AWS_DEFAULT_REGION'],
'version' => 'latest',
'http' => [
'timeout' => 5,
'connect_timeout' => 5,
],
]);
}
public function warmContainers($containersToWarm): array
{
fwrite(STDERR, "Hover: Warming $containersToWarm containers.".PHP_EOL);
$promises = collect(range(1, $containersToWarm - 1))
->mapWithKeys(function ($i) {
return [
'warmer-'.$i => $this->client->invokeAsync([
'FunctionName' => $_ENV['AWS_LAMBDA_FUNCTION_NAME'],
'Qualifier' => $_ENV['AWS_LAMBDA_FUNCTION_VERSION'],
'LogType' => 'None',
'Payload' => json_encode(['warmer_ping' => true]),
])
];
})->all();
try {
Utils::settle($promises)->wait();
} catch (\Throwable $e) {
fwrite(STDERR, "Hover: Some warming invocations failed.".PHP_EOL);
}
fwrite(STDERR, "Hover: $containersToWarm containers have been warmed successfully.".PHP_EOL);
return [
'output' => 'Warming done!',
];
}
public function warmContainer(): array
{
usleep(50 * 1000);
fwrite(STDERR, "Hover: 1 container warmed.".PHP_EOL);
return [
'output' => 'Warmed!',
];
}
}