Skip to content

Commit

Permalink
Merge pull request #15 from utopia-php/fix-exit-codes
Browse files Browse the repository at this point in the history
Fix: Exit Codes
  • Loading branch information
christyjacob4 authored Apr 26, 2022
2 parents 6d164b7 + 843d5a2 commit 69e68f8
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,24 @@ static public function exit(int $status = 0): void
*/
static public function execute(string $cmd, string $stdin, string &$stdout, string &$stderr, int $timeout = -1): int
{
$cmd = '( ' . $cmd . ' ) 3>/dev/null ; echo $? >&3';

$pipes = [];
$process = \proc_open(
$cmd,
[['pipe','r'],['pipe','w'],['pipe','w']],
[['pipe','r'],['pipe','w'],['pipe','w'],['pipe', 'w']],
$pipes
);
$start = \time();
$stdout = '';
$stderr = '';
$status = '';

if (\is_resource($process)) {
\stream_set_blocking($pipes[0], false);
\stream_set_blocking($pipes[1], false);
\stream_set_blocking($pipes[2], false);
\stream_set_blocking($pipes[3], false);

\fwrite($pipes[0], $stdin);
\fclose($pipes[0]);
Expand All @@ -155,20 +159,21 @@ static public function execute(string $cmd, string $stdin, string &$stdout, stri
while (\is_resource($process)) {
$stdout .= \stream_get_contents($pipes[1]);
$stderr .= \stream_get_contents($pipes[2]);
$status .= \stream_get_contents($pipes[3]);

if ($timeout > 0 && \time() - $start > $timeout) {
\proc_terminate($process, 9);
return 1;
}

$status = \proc_get_status($process);

if (!$status['running']) {
if (!\proc_get_status($process)['running']) {
\fclose($pipes[1]);
\fclose($pipes[2]);
\proc_close($process);

return (int)$status['exitcode'];
$exitCode = (int) str_replace("\n","",$status);

return $exitCode;
}

\usleep(10000);
Expand Down

0 comments on commit 69e68f8

Please sign in to comment.