From b98f41083838fa2a3e29c9811e80892b35663a6a Mon Sep 17 00:00:00 2001 From: terremoth Date: Mon, 2 Dec 2024 01:36:24 -0300 Subject: [PATCH] Fix for linux fallback to exec --- src/Terremoth/Async/File.php | 11 +++++--- src/Terremoth/Async/Process.php | 28 +++++++++----------- src/Terremoth/Async/background_processor.php | 14 ++-------- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/Terremoth/Async/File.php b/src/Terremoth/Async/File.php index 890d8e1..786ec59 100644 --- a/src/Terremoth/Async/File.php +++ b/src/Terremoth/Async/File.php @@ -8,7 +8,9 @@ readonly class File { /** + * @param string $file * @throws Exception + * @param array $args */ public function __construct(private string $file, private array $args = []) { @@ -19,13 +21,14 @@ public function __construct(private string $file, private array $args = []) public function run(): void { - $template = [PHP_BINARY, $this->file, ...$this->args, '&']; - if (PHP_OS_FAMILY === 'Windows') { $template = ['start', '""', '/B', PHP_BINARY, $this->file, ...$this->args]; + $process = new SymfonyProcess($template); + $process->start(); + return; } - $process = new SymfonyProcess($template); - $process->start(); + $args = implode(' ', $this->args); + exec(PHP_BINARY . ' ' . $this->file . ' ' . $args . ' > /dev/null 2>&1 &'); } } diff --git a/src/Terremoth/Async/Process.php b/src/Terremoth/Async/Process.php index 0bf4aaa..3a478ef 100644 --- a/src/Terremoth/Async/Process.php +++ b/src/Terremoth/Async/Process.php @@ -11,18 +11,10 @@ class Process { private const MAX_INT = 2147483647; - private array $processTemplate = [PHP_BINARY, __DIR__ . DIRECTORY_SEPARATOR . 'background_processor.php', '{key}', - '{length}', '&']; - private int $key; - - public function __construct() + public function __construct(private int $key = 0) { - $this->key = mt_rand(0, self::MAX_INT); // communication key - $this->processTemplate[2] = $this->key; - - if (PHP_OS_FAMILY === 'Windows') { - $this->processTemplate = ['start', '""', '/B', PHP_BINARY, __DIR__ . DIRECTORY_SEPARATOR . - 'background_processor.php', $this->key, '{length}']; + if (!$this->key) { + $this->key = mt_rand(0, self::MAX_INT); // communication key } } @@ -31,6 +23,7 @@ public function __construct() */ public function send(Closure $asyncFunction): void { + $separator = DIRECTORY_SEPARATOR; $serialized = serialize(new SerializableClosure($asyncFunction)); $serializedLength = strlen($serialized); $shmopInstance = shmop_open($this->key, 'c', 0660, $serializedLength); @@ -46,9 +39,14 @@ public function send(Closure $asyncFunction): void $serializedLength . '. Bytes written: ' . $bytesWritten); } - $key = array_search('{length}', $this->processTemplate); - $this->processTemplate[$key] = $serializedLength; - $process = new SymfonyProcess($this->processTemplate); - $process->start(); + if (PHP_OS_FAMILY === 'Windows') { + $arg = ['start', '""', '/B', PHP_BINARY, __DIR__ . $separator . 'background_processor.php', $this->key]; + $process = new SymfonyProcess($arg); + $process->start(); + return; + } + + exec(PHP_BINARY . __DIR__ . $separator . 'background_processor.php ' . $this->key . + ' > /dev/null 2>&1 &'); } } diff --git a/src/Terremoth/Async/background_processor.php b/src/Terremoth/Async/background_processor.php index d44e41a..6218dbe 100644 --- a/src/Terremoth/Async/background_processor.php +++ b/src/Terremoth/Async/background_processor.php @@ -9,20 +9,10 @@ exit(1); } -if (!isset($argv[2])) { - fwrite(STDERR, 'Error: length not provided'); - exit(1); -} - $key = (int)$argv[1]; -$length = (int)$argv[2]; - -if ($length === 0) { - fwrite(STDERR, 'Error: length cannot be zero'); - exit(1); -} -$shmopInstance = shmop_open($key, 'c', 0660, $length); +$shmopInstance = shmop_open($key, 'a', 0, 0); +$length = shmop_size($shmopInstance); $data = shmop_read($shmopInstance, 0, $length); /**