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
|
<?php
namespace gipfl\Process;
use React\ChildProcess\Process as ChildProcess;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use function React\Promise\resolve;
class ProcessKiller
{
/**
* @param ChildProcess $process
* @param LoopInterface $loop
* @param int $timeout
* @return \React\Promise\ExtendedPromiseInterface
*/
public static function terminateProcess(ChildProcess $process, LoopInterface $loop, $timeout = 0)
{
$processes = new ProcessList();
$processes->attach($process);
return static::terminateProcesses($processes, $loop, $timeout);
}
/**
* @param ProcessList $processes
* @param LoopInterface $loop
* @param int $timeout
* @return \React\Promise\ExtendedPromiseInterface
*/
public static function terminateProcesses(ProcessList $processes, LoopInterface $loop, $timeout = 5)
{
if ($processes->count() === 0) {
return resolve();
}
$deferred = new Deferred();
$killTimer = $loop->addTimer($timeout, function () use ($deferred, $processes, $loop) {
/** @var ChildProcess $process */
foreach ($processes as $process) {
$pid = $process->getPid();
// Logger::error("Process $pid is still running, sending SIGKILL");
$process->terminate(SIGKILL);
}
// Let's add a bit of a delay after KILLing
$loop->addTimer(0.1, function () use ($deferred) {
$deferred->resolve();
});
});
$timer = $loop->addPeriodicTimer($timeout / 20, function () use (
$deferred,
$processes,
$loop,
&$timer,
$killTimer
) {
$stopped = [];
/** @var ChildProcess $process */
foreach ($processes as $process) {
if (! $process->isRunning()) {
$stopped[] = $process;
}
}
foreach ($stopped as $process) {
$processes->detach($process);
}
if ($processes->count() === 0) {
$loop->cancelTimer($timer);
$loop->cancelTimer($killTimer);
$deferred->resolve();
}
});
/** @var ChildProcess $process */
foreach ($processes as $process) {
$process->terminate(SIGTERM);
}
return $deferred->promise();
}
}
|