Skip to content

Commit

Permalink
Fix for linux fallback to exec
Browse files Browse the repository at this point in the history
  • Loading branch information
terremoth committed Dec 2, 2024
1 parent b79414d commit b98f410
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
11 changes: 7 additions & 4 deletions src/Terremoth/Async/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
readonly class File
{
/**
* @param string $file
* @throws Exception
* @param array<array-key, null> $args
*/
public function __construct(private string $file, private array $args = [])
{
Expand All @@ -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 &');
}
}
28 changes: 13 additions & 15 deletions src/Terremoth/Async/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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);
Expand All @@ -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 &');
}
}
14 changes: 2 additions & 12 deletions src/Terremoth/Async/background_processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down

0 comments on commit b98f410

Please sign in to comment.