Skip to content

Commit b98f410

Browse files
committed
Fix for linux fallback to exec
1 parent b79414d commit b98f410

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

src/Terremoth/Async/File.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
readonly class File
99
{
1010
/**
11+
* @param string $file
1112
* @throws Exception
13+
* @param array<array-key, null> $args
1214
*/
1315
public function __construct(private string $file, private array $args = [])
1416
{
@@ -19,13 +21,14 @@ public function __construct(private string $file, private array $args = [])
1921

2022
public function run(): void
2123
{
22-
$template = [PHP_BINARY, $this->file, ...$this->args, '&'];
23-
2424
if (PHP_OS_FAMILY === 'Windows') {
2525
$template = ['start', '""', '/B', PHP_BINARY, $this->file, ...$this->args];
26+
$process = new SymfonyProcess($template);
27+
$process->start();
28+
return;
2629
}
2730

28-
$process = new SymfonyProcess($template);
29-
$process->start();
31+
$args = implode(' ', $this->args);
32+
exec(PHP_BINARY . ' ' . $this->file . ' ' . $args . ' > /dev/null 2>&1 &');
3033
}
3134
}

src/Terremoth/Async/Process.php

+13-15
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,10 @@ class Process
1111
{
1212
private const MAX_INT = 2147483647;
1313

14-
private array $processTemplate = [PHP_BINARY, __DIR__ . DIRECTORY_SEPARATOR . 'background_processor.php', '{key}',
15-
'{length}', '&'];
16-
private int $key;
17-
18-
public function __construct()
14+
public function __construct(private int $key = 0)
1915
{
20-
$this->key = mt_rand(0, self::MAX_INT); // communication key
21-
$this->processTemplate[2] = $this->key;
22-
23-
if (PHP_OS_FAMILY === 'Windows') {
24-
$this->processTemplate = ['start', '""', '/B', PHP_BINARY, __DIR__ . DIRECTORY_SEPARATOR .
25-
'background_processor.php', $this->key, '{length}'];
16+
if (!$this->key) {
17+
$this->key = mt_rand(0, self::MAX_INT); // communication key
2618
}
2719
}
2820

@@ -31,6 +23,7 @@ public function __construct()
3123
*/
3224
public function send(Closure $asyncFunction): void
3325
{
26+
$separator = DIRECTORY_SEPARATOR;
3427
$serialized = serialize(new SerializableClosure($asyncFunction));
3528
$serializedLength = strlen($serialized);
3629
$shmopInstance = shmop_open($this->key, 'c', 0660, $serializedLength);
@@ -46,9 +39,14 @@ public function send(Closure $asyncFunction): void
4639
$serializedLength . '. Bytes written: ' . $bytesWritten);
4740
}
4841

49-
$key = array_search('{length}', $this->processTemplate);
50-
$this->processTemplate[$key] = $serializedLength;
51-
$process = new SymfonyProcess($this->processTemplate);
52-
$process->start();
42+
if (PHP_OS_FAMILY === 'Windows') {
43+
$arg = ['start', '""', '/B', PHP_BINARY, __DIR__ . $separator . 'background_processor.php', $this->key];
44+
$process = new SymfonyProcess($arg);
45+
$process->start();
46+
return;
47+
}
48+
49+
exec(PHP_BINARY . __DIR__ . $separator . 'background_processor.php ' . $this->key .
50+
' > /dev/null 2>&1 &');
5351
}
5452
}

src/Terremoth/Async/background_processor.php

+2-12
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,10 @@
99
exit(1);
1010
}
1111

12-
if (!isset($argv[2])) {
13-
fwrite(STDERR, 'Error: length not provided');
14-
exit(1);
15-
}
16-
1712
$key = (int)$argv[1];
18-
$length = (int)$argv[2];
19-
20-
if ($length === 0) {
21-
fwrite(STDERR, 'Error: length cannot be zero');
22-
exit(1);
23-
}
2413

25-
$shmopInstance = shmop_open($key, 'c', 0660, $length);
14+
$shmopInstance = shmop_open($key, 'a', 0, 0);
15+
$length = shmop_size($shmopInstance);
2616
$data = shmop_read($shmopInstance, 0, $length);
2717

2818
/**

0 commit comments

Comments
 (0)