-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.php
75 lines (63 loc) · 2.18 KB
/
services.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
<?php
declare(strict_types=1);
use Laminas\ServiceManager\ServiceManager;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Scoutapm\Agent;
use Scoutapm\Config;
use Scoutapm\Config\ConfigKey;
use Scoutapm\ScoutApmAgent;
$services = new ServiceManager();
const ENV_LONG_PROCESS_ITERATION_COUNT = 'LONG_PROCESS_ITERATION_COUNT';
const ENV_WAIT_AFTER_ITERATION_SECS = 'WAIT_AFTER_ITERATION_SECS';
const ENV_SINGLE_ITERATION_SLEEP_SECS = 'SINGLE_ITERATION_SLEEP_SECS';
$numericEnvFactoryFactory = static function (string $envVarName, int $defaultValueIfNegative) {
return static function () use ($envVarName, $defaultValueIfNegative): int {
$sleepTime = (int) getenv($envVarName);
if ($sleepTime <= 0) {
return $defaultValueIfNegative;
}
return $sleepTime;
};
};
function assertEnvNotEmpty(string $envVarName) {
$envVarValue = (string) getenv($envVarName);
if ($envVarValue === '') {
throw new InvalidArgumentException(sprintf('Environment variable "%s" was required but not set', $envVarName));
}
}
$services->setFactory(
ENV_LONG_PROCESS_ITERATION_COUNT,
$numericEnvFactoryFactory(ENV_LONG_PROCESS_ITERATION_COUNT, 3)
);
$services->setFactory(
ENV_WAIT_AFTER_ITERATION_SECS,
$numericEnvFactoryFactory(ENV_WAIT_AFTER_ITERATION_SECS, 60)
);
$services->setFactory(
ENV_SINGLE_ITERATION_SLEEP_SECS,
$numericEnvFactoryFactory(ENV_SINGLE_ITERATION_SLEEP_SECS, 3)
);
$services->setFactory(
LoggerInterface::class,
static function (): LoggerInterface {
$logger = new Logger('application');
$logger->pushHandler(new StreamHandler('php://stderr'));
return $logger;
}
);
$services->setFactory(
ScoutApmAgent::class,
static function (ContainerInterface $container): ScoutApmAgent {
assertEnvNotEmpty('SCOUT_KEY');
assertEnvNotEmpty('SCOUT_NAME');
assertEnvNotEmpty('SCOUT_MONITOR');
return Agent::fromConfig(
Config::fromArray([]), // Relying on environment variables for configuration
$container->get(LoggerInterface::class),
);
}
);
return $services;