Skip to content

Commit

Permalink
[Process] Use stream based storage to avoid memory issues
Browse files Browse the repository at this point in the history
  • Loading branch information
romainneutron committed Jan 18, 2016
1 parent b702448 commit 7663f4a
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions src/Symfony/Component/Process/Process.php
Expand Up @@ -378,7 +378,7 @@ public function getOutput()

$this->readPipes(false, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true);

return $this->stdout;
return (string) stream_get_contents($this->stdout, -1, 0);
}

/**
Expand All @@ -393,17 +393,14 @@ public function getOutput()
*/
public function getIncrementalOutput()
{
$this->requireProcessIsStarted(__FUNCTION__);

$data = $this->getOutput();

$latest = substr($data, $this->incrementalOutputOffset);

if (false === $latest) {
return '';
if ($this->outputDisabled) {
throw new LogicException('Output has been disabled.');
}

$this->incrementalOutputOffset = strlen($data);
$this->requireProcessIsStarted(__FUNCTION__);

$latest = (string) stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
$this->incrementalOutputOffset = ftell($this->stdout);

return $latest;
}
Expand All @@ -421,7 +418,7 @@ public function getErrorOutput()

$this->readPipes(false, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true);

return $this->stderr;
return (string) stream_get_contents($this->stderr, -1, 0);
}

/**
Expand All @@ -437,17 +434,14 @@ public function getErrorOutput()
*/
public function getIncrementalErrorOutput()
{
$this->requireProcessIsStarted(__FUNCTION__);

$data = $this->getErrorOutput();

$latest = substr($data, $this->incrementalErrorOutputOffset);

if (false === $latest) {
return '';
if ($this->outputDisabled) {
throw new LogicException('Output has been disabled.');
}

$this->incrementalErrorOutputOffset = strlen($data);
$this->requireProcessIsStarted(__FUNCTION__);

$latest = (string) stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
$this->incrementalErrorOutputOffset = ftell($this->stderr);

return $latest;
}
Expand Down Expand Up @@ -666,21 +660,29 @@ public function stop($timeout = 10, $signal = null)
/**
* Adds a line to the STDOUT stream.
*
* @internal
*
* @param string $line The line to append
*/
public function addOutput($line)
{
$this->stdout .= $line;
fseek($this->stdout, 0, SEEK_END);
fwrite($this->stdout, $line);
fseek($this->stdout, $this->incrementalOutputOffset);
}

/**
* Adds a line to the STDERR stream.
*
* @internal
*
* @param string $line The line to append
*/
public function addErrorOutput($line)
{
$this->stderr .= $line;
fseek($this->stderr, 0, SEEK_END);
fwrite($this->stderr, $line);
fseek($this->stderr, $this->incrementalErrorOutputOffset);
}

/**
Expand Down Expand Up @@ -1126,8 +1128,8 @@ private function resetProcessData()
$this->exitcode = null;
$this->fallbackStatus = array();
$this->processInformation = null;
$this->stdout = null;
$this->stderr = null;
$this->stdout = fopen('php://temp/maxmemory:'.(8 * 1024 * 1024), 'a+');
$this->stderr = fopen('php://temp/maxmemory:'.(8 * 1024 * 1024), 'a+');
$this->process = null;
$this->latestSignal = null;
$this->status = self::STATUS_READY;
Expand Down

0 comments on commit 7663f4a

Please sign in to comment.