Skip to content
Extraits de code Groupes Projets
Valider a2a586a0 rédigé par Thomas Steur's avatar Thomas Steur
Parcourir les fichiers

refs #4610 make sure the fallback mode works in case async is not supported

parent f06a7a1d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -12,16 +12,32 @@ use Piwik\CliMulti\Output; ...@@ -12,16 +12,32 @@ use Piwik\CliMulti\Output;
class CliMulti { class CliMulti {
/**
* If set to true or false it will overwrite whether async is supported or not.
*
* @var null|bool
*/
public $supportsAsync = null;
/** /**
* @var \Piwik\CliMulti\Process[] * @var \Piwik\CliMulti\Process[]
*/ */
private $pids = array(); private $processes = array();
/** /**
* @var \Piwik\CliMulti\Output[] * @var \Piwik\CliMulti\Output[]
*/ */
private $outputs = array(); private $outputs = array();
/**
* It will request all given URLs in parallel (async) using the CLI and wait until all requests are finished.
*
*
* @param string[] $piwikUrls An array of urls, for instance:
* array('/index.php?module=API', '/?module=API', 'http://www.example.com?module=API')
* @return array The response of each URL in the same order as the URLs. The array can contain null values in case
* there was a problem with a request, for instance if the process died unexpected.
*/
public function request(array $piwikUrls) public function request(array $piwikUrls)
{ {
$this->start($piwikUrls); $this->start($piwikUrls);
...@@ -39,18 +55,21 @@ class CliMulti { ...@@ -39,18 +55,21 @@ class CliMulti {
private function start($piwikUrls) private function start($piwikUrls)
{ {
foreach ($piwikUrls as $index => $url) { foreach ($piwikUrls as $index => $url) {
$cmdId = $this->generateCmdId($url); $cmdId = $this->generateCmdId($url);
$pid = $cmdId . $index . '_cli_multi_pid'; $pid = $cmdId . $index . '_cli_multi_pid';
$output = $cmdId . $index . '_cli_multi_output'; $outputId = $cmdId . $index . '_cli_multi_output';
$params = array('output' => $output, 'pid' => $pid); $this->processes[] = new Process($pid);
$command = $this->buildCommand($url, $params); $this->outputs[] = new Output($outputId);
$command = $this->buildCommand($url, array('outputId' => $outputId, 'pid' => $pid));
$appendix = $this->supportsAsync() ? ' > /dev/null 2>&1 &' : ''; $appendix = $this->supportsAsync() ? ' > /dev/null 2>&1 &' : '';
shell_exec($command . $appendix); shell_exec($command . $appendix);
$this->pids[] = new Process($pid); if (!$this->supportsAsync()) {
$this->outputs[] = new Output($output); end($this->processes)->finishProcess();
}
} }
} }
...@@ -88,12 +107,12 @@ class CliMulti { ...@@ -88,12 +107,12 @@ class CliMulti {
private function isFinished() private function isFinished()
{ {
foreach ($this->pids as $index => $pid) { foreach ($this->processes as $index => $process) {
if (!$pid->hasStarted()) { if (!$process->hasStarted()) {
return false; return false;
} }
if ($pid->isRunning() && !$this->outputs[$index]->exists()) { if ($process->isRunning() && !$this->outputs[$index]->exists()) {
return false; return false;
} }
} }
...@@ -112,12 +131,16 @@ class CliMulti { ...@@ -112,12 +131,16 @@ class CliMulti {
*/ */
private function supportsAsync() private function supportsAsync()
{ {
if (is_bool($this->supportsAsync)) {
return $this->supportsAsync;
}
return !SettingsServer::isWindows(); return !SettingsServer::isWindows();
} }
private function cleanup() private function cleanup()
{ {
foreach ($this->pids as $pid) { foreach ($this->processes as $pid) {
$pid->finishProcess(); $pid->finishProcess();
} }
...@@ -125,8 +148,8 @@ class CliMulti { ...@@ -125,8 +148,8 @@ class CliMulti {
$output->destroy(); $output->destroy();
} }
$this->pids = array(); $this->processes = array();
$this->outputs = array(); $this->outputs = array();
} }
} }
...@@ -43,8 +43,8 @@ require_once PIWIK_INCLUDE_PATH . "/index.php"; ...@@ -43,8 +43,8 @@ require_once PIWIK_INCLUDE_PATH . "/index.php";
$content = ob_get_contents(); $content = ob_get_contents();
ob_clean(); ob_clean();
if (!empty($_GET['output']) && \Piwik\Filesystem::isValidFilename($_GET['output'])) { if (!empty($_GET['outputId']) && \Piwik\Filesystem::isValidFilename($_GET['outputId'])) {
$cliMulti = new \Piwik\CliMulti\Output($_GET['output']); $cliMulti = new \Piwik\CliMulti\Output($_GET['outputId']);
$cliMulti->write($content); $cliMulti->write($content);
} else { } else {
echo $content; echo $content;
......
...@@ -137,6 +137,15 @@ class Core_CliMultiTest extends IntegrationTestCase ...@@ -137,6 +137,15 @@ class Core_CliMultiTest extends IntegrationTestCase
$this->assertTrue(false !== strpos($response[0], 'Widgetize the full dashboard')); $this->assertTrue(false !== strpos($response[0], 'Widgetize the full dashboard'));
} }
public function test_shouldFallback_IfAsyncIsNotSupported()
{
$this->cliMulti->supportsAsync = false;
$urls = $this->buildUrls('getPiwikVersion', 'getAnswerToLife', 'getPiwikVersion');
$this->assertRequestReturnsValidResponses($urls, array('getPiwikVersion', 'getAnswerToLife', 'getPiwikVersion'));
}
private function assertRequestReturnsValidResponses($urls, $expectedResponseIds) private function assertRequestReturnsValidResponses($urls, $expectedResponseIds)
{ {
$actualResponse = $this->cliMulti->request($urls); $actualResponse = $this->cliMulti->request($urls);
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter