Repository navigation
Expand file tree
/
Copy pathWorkerPathsFile.php
More file actions
96 lines (74 loc) · 2.46 KB
/
Copy pathWorkerPathsFile.php
File metadata and controls
96 lines (74 loc) · 2.46 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
<?php
declare(strict_types=1);
namespace Rector\Parallel\ValueObject;
use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use Rector\Parallel\Exception\ParallelShouldNotHappenException;
/**
* The command-line paths for the workers, in a temp file, as the OS limits the length of their command line.
* The file is removed by remove(), on shutdown and, when ext-pcntl is loaded, on SIGINT and SIGTERM.
*
* @see \Rector\Tests\Parallel\ValueObject\WorkerPathsFileTest
*/
final class WorkerPathsFile
{
private bool $isRemoved = false;
/**
* @var array<int, (callable(int, mixed): void)|int>
*/
private array $previousSignalHandlers = [];
private ?bool $previousAsyncSignals = null;
private function __construct(
private readonly string $filePath
) {
}
/**
* @param string[] $paths
*/
public static function create(array $paths): self
{
$filePath = tempnam(sys_get_temp_dir(), 'rector_paths_');
if ($filePath === false) {
throw new ParallelShouldNotHappenException('Failed creating temp file.');
}
FileSystem::write($filePath, Json::encode($paths));
$workerPathsFile = new self($filePath);
$workerPathsFile->removeOnInterrupt();
return $workerPathsFile;
}
public function getFilePath(): string
{
return $this->filePath;
}
public function remove(): void
{
if ($this->isRemoved) {
return;
}
$this->isRemoved = true;
FileSystem::delete($this->filePath);
foreach ($this->previousSignalHandlers as $signal => $previousSignalHandler) {
pcntl_signal($signal, $previousSignalHandler);
}
if ($this->previousAsyncSignals !== null) {
pcntl_async_signals($this->previousAsyncSignals);
}
}
private function removeOnInterrupt(): void
{
register_shutdown_function($this->remove(...));
if (! extension_loaded('pcntl')) {
return;
}
$signalHandler = function (int $signal): never {
$this->remove();
// the conventional exit code of a process ended by a signal
exit(128 + $signal);
};
foreach ([SIGINT, SIGTERM] as $signal) {
$this->previousSignalHandlers[$signal] = pcntl_signal_get_handler($signal);
pcntl_signal($signal, $signalHandler);
}
$this->previousAsyncSignals = pcntl_async_signals(true);
}
}