forked from phpactor/language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceManager.php
More file actions
120 lines (99 loc) · 3.4 KB
/
ServiceManager.php
File metadata and controls
120 lines (99 loc) · 3.4 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace Phpactor\LanguageServer\Core\Service;
use Amp\CancellationTokenSource;
use Amp\Promise;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Throwable;
use function Amp\asyncCall;
class ServiceManager
{
/**
* @var CancellationTokenSource[]
*/
private array $cancellations = [];
public function __construct(private ServiceProviders $serviceProviders, private LoggerInterface $logger)
{
}
/**
* @return array<string>
*/
public function runningServices(): array
{
return array_keys($this->cancellations);
}
public function startAll(): void
{
foreach ($this->serviceProviders->names() as $serviceName) {
$this->start($serviceName);
}
}
public function start(string $serviceName): void
{
$provider = $this->serviceProviders->get($serviceName);
if (isset($this->cancellations[$serviceName])) {
throw new RuntimeException(sprintf(
'Service "%s" is already running',
$serviceName
));
}
$this->logger->info(sprintf('Starting service: %s (%s)', $serviceName, $provider::class));
if (!method_exists($provider, $serviceName)) {
throw new RuntimeException(sprintf(
'Service provider "%s" has no service method "%s"',
$provider::class,
$serviceName
));
}
$cancel = new CancellationTokenSource();
$this->cancellations[$serviceName] = $cancel;
$token = $cancel->getToken();
asyncCall(function () use ($provider, $serviceName, $token) {
$promise = $provider->$serviceName($token);
if (!$promise instanceof Promise) {
throw new RuntimeException(sprintf(
'Service method "%s" must return a Promise, got "%s"',
$provider::class . '::' . $serviceName,
get_debug_type($promise)
));
}
try {
yield $promise;
} catch (Throwable $error) {
$this->logger->error(sprintf(
'Error in service "%s" "%s:%s": %s',
$serviceName,
$provider::class,
__FUNCTION__,
$error->getMessage()
));
}
});
}
public function stop(string $serviceName): void
{
$this->serviceProviders->assertExists($serviceName);
if (!isset($this->cancellations[$serviceName])) {
throw new RuntimeException(sprintf(
'Cannot stop service "%s" it has not been started, running services: "%s"',
$serviceName,
implode('", "', array_keys($this->cancellations))
));
}
$tokenSource = $this->cancellations[$serviceName];
assert($tokenSource instanceof CancellationTokenSource);
$tokenSource->cancel();
unset($this->cancellations[$serviceName]);
}
public function isRunning(string $serviceName): bool
{
$this->serviceProviders->assertExists($serviceName);
return isset($this->cancellations[$serviceName]);
}
public function stopAll(): void
{
foreach ($this->serviceProviders->names() as $serviceName) {
$this->stop($serviceName);
}
}
}