MVC : Backend - split configdRun() in order to return a resource which the controller can stream with minimal memory consumption.

To be used in combination with 42fd27df77
This commit is contained in:
Ad Schellevis 2023-11-22 21:45:39 +01:00
parent 9e26e00eac
commit 28b99101d4

View File

@ -77,18 +77,16 @@ class Backend
}
/**
* send event to backend
* send event to backend and return resource (or null on failure)
* @param string $event event string
* @param bool $detach detach process
* @param int $timeout timeout in seconds
* @param int $connect_timeout connect timeout in seconds
* @return string
* @return resource|null
* @throws \Exception
*/
public function configdRun($event, $detach = false, $timeout = 120, $connect_timeout = 10)
public function configdStream($event, $detach = false, $timeout = 120, $connect_timeout = 10)
{
$endOfStream = chr(0) . chr(0) . chr(0);
$errorOfStream = 'Execute error';
$poll_timeout = 2; // poll timeout interval
// wait until socket exist for a maximum of $connect_timeout
@ -111,7 +109,6 @@ class Backend
}
}
$resp = '';
stream_set_timeout($stream, $poll_timeout);
// send command
@ -121,6 +118,51 @@ class Backend
fwrite($stream, $event);
}
return $stream;
}
/**
* send event to backend using command parameter list (which will be quoted for proper handling)
* @param string $event event string
* @param array $params list of parameters to send with command
* @param bool $detach detach process
* @param int $timeout timeout in seconds
* @param int $connect_timeout connect timeout in seconds
* @return resource|null
* @throws \Exception
*/
public function configdpStream($event, $params = [], $detach = false, $timeout = 120, $connect_timeout = 10)
{
if (!is_array($params)) {
/* just in case there's only one parameter */
$params = array($params);
}
foreach ($params as $param) {
$event .= ' ' . escapeshellarg($param ?? '');
}
return $this->configdStream($event, $detach, $timeout, $connect_timeout);
}
/**
* send event to backend
* @param string $event event string
* @param bool $detach detach process
* @param int $timeout timeout in seconds
* @param int $connect_timeout connect timeout in seconds
* @return string
* @throws \Exception
*/
public function configdRun($event, $detach = false, $timeout = 120, $connect_timeout = 10)
{
$endOfStream = chr(0) . chr(0) . chr(0);
$errorOfStream = 'Execute error';
$resp = '';
$stream = $this->configdStream($event, $detach, $timeout, $connect_timeout);
// read response data
$starttime = time();
while (true) {