Skip to content

Commit

Permalink
chore(shell): use io streaming
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed May 7, 2022
1 parent a4fb106 commit bfab29c
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/Psl/Shell/execute.php
Expand Up @@ -4,7 +4,6 @@

namespace Psl\Shell;

use Psl\Async;
use Psl\Dict;
use Psl\Env;
use Psl\Filesystem;
Expand Down Expand Up @@ -156,25 +155,18 @@ static function (array $m) use (
$stderr = new IO\CloseReadStreamHandle($pipes[2]);

try {
[$stdout_content, $stderr_content] = Async\concurrently([
static fn(): string => $stdout->readAll(timeout: $timeout),
static fn(): string => $stderr->readAll(timeout: $timeout),
]);
// @codeCoverageIgnoreStart
} catch (Async\Exception\CompositeException $exception) {
$reasons = $exception->getReasons();
if ($reasons[0] instanceof IO\Exception\TimeoutException) {
throw new Exception\TimeoutException('reached timeout while the process output is still not readable.', 0, $reasons[0]);
}

if ($reasons[1] instanceof IO\Exception\TimeoutException) {
throw new Exception\TimeoutException('reached timeout while the process output is still not readable.', 0, $reasons[1]);
$stdout_content = '';
$stderr_content = '';
/** @psalm-suppress MissingThrowsDocblock */
foreach (IO\streaming(['out' => $stdout, 'err' => $stderr], $timeout) as $type => $chunk) {
if ('out' === $type) {
$stdout_content .= $chunk;
} else {
$stderr_content .= $chunk;
}
}

throw new Exception\RuntimeException('Failed to reach process output.', 0, $exception ?? null);
} catch (IO\Exception\TimeoutException $previous) {
throw new Exception\TimeoutException('reached timeout while the process output is still not readable.', 0, $previous);
// @codeCoverageIgnoreEnd
} finally {
/** @psalm-suppress MissingThrowsDocblock */
$stdout->close();
Expand Down

0 comments on commit bfab29c

Please sign in to comment.