[Process] Use stream based storage to avoid memory issues

This commit is contained in:
Romain Neutron 2016-01-18 17:23:05 +01:00
parent b7024483e3
commit da73125a9c

View File

@ -378,7 +378,11 @@ class Process
$this->readPipes(false, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true); $this->readPipes(false, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true);
return $this->stdout; if (false === $ret = stream_get_contents($this->stdout, -1, 0)) {
return '';
}
return $ret;
} }
/** /**
@ -395,16 +399,13 @@ class Process
{ {
$this->requireProcessIsStarted(__FUNCTION__); $this->requireProcessIsStarted(__FUNCTION__);
$data = $this->getOutput(); $latest = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
$this->incrementalOutputOffset = ftell($this->stdout);
$latest = substr($data, $this->incrementalOutputOffset);
if (false === $latest) { if (false === $latest) {
return ''; return '';
} }
$this->incrementalOutputOffset = strlen($data);
return $latest; return $latest;
} }
@ -421,7 +422,11 @@ class Process
$this->readPipes(false, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true); $this->readPipes(false, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true);
return $this->stderr; if (false === $ret = stream_get_contents($this->stderr, -1, 0)) {
return '';
}
return $ret;
} }
/** /**
@ -439,16 +444,13 @@ class Process
{ {
$this->requireProcessIsStarted(__FUNCTION__); $this->requireProcessIsStarted(__FUNCTION__);
$data = $this->getErrorOutput(); $latest = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
$this->incrementalErrorOutputOffset = ftell($this->stderr);
$latest = substr($data, $this->incrementalErrorOutputOffset);
if (false === $latest) { if (false === $latest) {
return ''; return '';
} }
$this->incrementalErrorOutputOffset = strlen($data);
return $latest; return $latest;
} }
@ -666,21 +668,29 @@ class Process
/** /**
* Adds a line to the STDOUT stream. * Adds a line to the STDOUT stream.
* *
* @internal
*
* @param string $line The line to append * @param string $line The line to append
*/ */
public function addOutput($line) 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. * Adds a line to the STDERR stream.
* *
* @internal
*
* @param string $line The line to append * @param string $line The line to append
*/ */
public function addErrorOutput($line) public function addErrorOutput($line)
{ {
$this->stderr .= $line; fseek($this->stderr, 0, SEEK_END);
fwrite($this->stderr, $line);
fseek($this->stderr, $this->incrementalErrorOutputOffset);
} }
/** /**
@ -1126,8 +1136,8 @@ class Process
$this->exitcode = null; $this->exitcode = null;
$this->fallbackStatus = array(); $this->fallbackStatus = array();
$this->processInformation = null; $this->processInformation = null;
$this->stdout = null; $this->stdout = fopen('php://temp/maxmemory:'.(1024 * 1024), 'wb+');
$this->stderr = null; $this->stderr = fopen('php://temp/maxmemory:'.(1024 * 1024), 'wb+');
$this->process = null; $this->process = null;
$this->latestSignal = null; $this->latestSignal = null;
$this->status = self::STATUS_READY; $this->status = self::STATUS_READY;