1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-26 12:24:40 +01:00

Cleanup temporary files (#82)

Modified by @trowski.
This commit is contained in:
Daniil Gentili 2019-08-27 10:02:33 -05:00 committed by Aaron Piotrowski
parent 53aa422eaf
commit c73c6d0a77
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
2 changed files with 12 additions and 2 deletions

View File

@ -30,6 +30,9 @@ class ProcessHub
/** @var Deferred[] */
private $acceptor = [];
/** @var string|null */
private $toUnlink;
public function __construct()
{
$isWindows = \strncasecmp(\PHP_OS, "WIN", 3) === 0;
@ -37,7 +40,10 @@ class ProcessHub
if ($isWindows) {
$this->uri = "tcp://127.0.0.1:0";
} else {
$this->uri = "unix://" . \tempnam(\sys_get_temp_dir(), "amp-parallel-ipc-") . ".sock";
$suffix = \bin2hex(\random_bytes(10));
$path = \sys_get_temp_dir() . "/amp-parallel-ipc-" . $suffix . ".sock";
$this->uri = "unix://" . $path;
$this->toUnlink = $path;
}
$this->server = \stream_socket_server($this->uri, $errno, $errstr, \STREAM_SERVER_BIND | \STREAM_SERVER_LISTEN);
@ -88,6 +94,9 @@ class ProcessHub
{
Loop::cancel($this->watcher);
\fclose($this->server);
if ($this->toUnlink !== null) {
@\unlink($this->toUnlink);
}
}
public function getUri(): string

View File

@ -109,7 +109,8 @@ final class Process implements Context
$contents = \file_get_contents(self::SCRIPT_PATH);
$contents = \str_replace("__DIR__", \var_export($path, true), $contents);
self::$pharScriptPath = $scriptPath = \tempnam(\sys_get_temp_dir(), "amp-process-runner-");
$suffix = \bin2hex(\random_bytes(10));
self::$pharScriptPath = $scriptPath = \sys_get_temp_dir() . "/amp-process-runner-" . $suffix . ".php";
\file_put_contents($scriptPath, $contents);
\register_shutdown_function(static function () {