Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.1] - Add command to view mview state and queue #12050

Merged
merged 12 commits into from
Dec 11, 2017
89 changes: 77 additions & 12 deletions app/code/Magento/Indexer/Console/Command/IndexerStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Framework\Indexer;
use Magento\Framework\Mview;

/**
* Command for displaying status of indexers.
Expand All @@ -30,21 +32,84 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$table = $this->getHelperSet()->get('table');
$table->setHeaders(['Title', 'Status', 'Update On', 'Schedule Status', 'Schedule Updated']);

$rows = [];

$indexers = $this->getIndexers($input);
foreach ($indexers as $indexer) {
$status = 'unknown';
switch ($indexer->getStatus()) {
case \Magento\Framework\Indexer\StateInterface::STATUS_VALID:
$status = 'Ready';
break;
case \Magento\Framework\Indexer\StateInterface::STATUS_INVALID:
$status = 'Reindex required';
break;
case \Magento\Framework\Indexer\StateInterface::STATUS_WORKING:
$status = 'Processing';
break;
$view = $indexer->getView();

$rowData = [
'Title' => $indexer->getTitle(),
'Status' => $this->getStatus($indexer),
'Update On' => $indexer->isScheduled() ? 'Schedule' : 'Save',
'Schedule Status' => '',
'Updated' => '',
];

if ($indexer->isScheduled()) {
$state = $view->getState();
$rowData['Schedule Status'] = "{$state->getStatus()} ({$this->getPendingCount($view)} in backlog)";
$rowData['Updated'] = $state->getUpdated();
}
$output->writeln(sprintf('%-50s ', $indexer->getTitle() . ':') . $status);

$rows[] = $rowData;
}

usort($rows, function ($comp1, $comp2) {
return strcmp($comp1['Title'], $comp2['Title']);
});

$table->addRows($rows);
$table->render($output);
}

/**
* @param Indexer\IndexerInterface $indexer
* @return string
*/
private function getStatus(Indexer\IndexerInterface $indexer)
{
$status = 'unknown';
switch ($indexer->getStatus()) {
case \Magento\Framework\Indexer\StateInterface::STATUS_VALID:
$status = 'Ready';
break;
case \Magento\Framework\Indexer\StateInterface::STATUS_INVALID:
$status = 'Reindex required';
break;
case \Magento\Framework\Indexer\StateInterface::STATUS_WORKING:
$status = 'Processing';
break;
}
return $status;
}

/**
* @param Mview\ViewInterface $view
* @return string
*/
private function getPendingCount(Mview\ViewInterface $view)
{
$changelog = $view->getChangelog();

try {
$currentVersionId = $changelog->getVersion();
} catch (Mview\View\ChangelogTableNotExistsException $e) {
return '';
}

$state = $view->getState();

$pendingCount = count($changelog->getList($state->getVersionId(), $currentVersionId));

$pendingString = "<error>$pendingCount</error>";
if ($pendingCount <= 0) {
$pendingString = "<info>$pendingCount</info>";
}

return $pendingString;
}
}
Loading