Skip to content

Commit

Permalink
[TASK] Consider queue initialization status
Browse files Browse the repository at this point in the history
Queue initialization status returned by the QueueInitializationService
isn't considered in the backend module and a success message is always
shown. This commit adds a check for the returned status and displays an
error message similiar to the message that is shown if an exceptions
occured.

Resolves: #3763
  • Loading branch information
dkd-friedrich committed Aug 22, 2023
1 parent e5c2623 commit 993f75a
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use ApacheSolrForTypo3\Solr\Domain\Site\Exception\UnexpectedTYPO3SiteInitializationException;
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
use ApacheSolrForTypo3\Solr\IndexQueue\QueueInterface;
use ApacheSolrForTypo3\Solr\System\Mvc\Backend\Service\ModuleDataStorageService;
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection as SolrCoreConnection;
Expand Down
135 changes: 100 additions & 35 deletions Classes/Controller/Backend/Search/IndexQueueModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use ApacheSolrForTypo3\Solr\Domain\Site\Exception\UnexpectedTYPO3SiteInitializationException;
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
use ApacheSolrForTypo3\Solr\IndexQueue\QueueInterface;
use ApacheSolrForTypo3\Solr\IndexQueue\QueueInitializationServiceAwareInterface;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\Exception as DBALException;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -35,10 +34,26 @@
/**
* Index Queue Module
*
* @todo: Support all index queues in actions beside "initializeIndexQueueAction" and
* "resetLogErrorsAction"
*
* @author Ingo Renner <[email protected]>
*/
class IndexQueueModuleController extends AbstractModuleController
{
protected array $enabledIndexQueues;

protected function initializeAction(): void
{
parent::initializeAction();

$this->enabledIndexQueues = $this->getIndexQueues();
if (!empty($this->enabledIndexQueues)) {
$this->indexQueue = $this->enabledIndexQueues[Queue::class] ?? reset($this->enabledIndexQueues);
}
}


public function setIndexQueue(QueueInterface $indexQueue): void
{
$this->indexQueue = $indexQueue;
Expand Down Expand Up @@ -74,6 +89,11 @@ protected function canQueueSelectedSite(): bool
if ($this->selectedSite === null || empty($this->solrConnectionManager->getConnectionsBySite($this->selectedSite))) {
return false;
}

if (!isset($this->indexQueue)) {
return false;
}

$enabledIndexQueueConfigurationNames = $this->selectedSite->getSolrConfiguration()->getEnabledIndexQueueConfigurationNames();
if (empty($enabledIndexQueueConfigurationNames)) {
return false;
Expand Down Expand Up @@ -108,24 +128,62 @@ public function initializeIndexQueueAction(): ResponseInterface

$indexingConfigurationsToInitialize = $this->request->getArgument('tx_solr-index-queue-initialization');
if ((!empty($indexingConfigurationsToInitialize)) && (is_array($indexingConfigurationsToInitialize))) {
// initialize selected indexing configuration
try {
if ($this->indexQueue instanceof QueueInitializationServiceAwareInterface) {
$initializationService = $this->indexQueue->getQueueInitializationService();
} else {
$initializationService = GeneralUtility::makeInstance(QueueInitializationService::class);
$initializationService = GeneralUtility::makeInstance(QueueInitializationService::class);
foreach ($indexingConfigurationsToInitialize as $configurationToInitialize) {
$indexQueueClass = $this->selectedSite->getSolrConfiguration()->getIndexQueueClassByConfigurationName($configurationToInitialize);
$indexQueue = $this->enabledIndexQueues[$indexQueueClass];

try {
$status = $initializationService->initializeBySiteAndIndexConfigurations($this->selectedSite, [$configurationToInitialize]);
$initializedIndexingConfiguration = [
'status' => $status[$configurationToInitialize],
'statistic' => 0
];
if ($status[$configurationToInitialize] === true) {
$initializedIndexingConfiguration['totalCount'] = $indexQueue->getStatisticsBySite($this->selectedSite, $configurationToInitialize)->getTotalCount();
}
$initializedIndexingConfigurations[$configurationToInitialize] = $initializedIndexingConfiguration;
} catch (Throwable $e) {
$this->addFlashMessage(
sprintf(
LocalizationUtility::translate(
'solr.backend.index_queue_module.flashmessage.initialize_failure',
'Solr'
),
$e->getMessage(),
$e->getCode()
),
LocalizationUtility::translate(
'solr.backend.index_queue_module.flashmessage.initialize_failure.title',
'Solr'
),
ContextualFeedbackSeverity::ERROR
);
}

$initializedIndexingConfigurations = $initializationService->initializeBySiteAndIndexConfigurations($this->selectedSite, $indexingConfigurationsToInitialize);
} catch (Throwable $e) {
}
} else {
$messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
$titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
$this->addFlashMessage(
LocalizationUtility::translate($messageLabel, 'Solr'),
LocalizationUtility::translate($titleLabel, 'Solr'),
ContextualFeedbackSeverity::WARNING
);
}

$messagesForConfigurations = [];
foreach ($initializedIndexingConfigurations as $indexingConfigurationName => $initializationData) {
if ($initializationData['status'] === true) {
$messagesForConfigurations[] = $indexingConfigurationName . ' (' . $initializationData['totalCount'] . ' records)';
} else {
$this->addFlashMessage(
sprintf(
LocalizationUtility::translate(
'solr.backend.index_queue_module.flashmessage.initialize_failure',
'Solr'
),
$e->getMessage(),
$e->getCode()
$indexingConfigurationName,
1662117020
),
LocalizationUtility::translate(
'solr.backend.index_queue_module.flashmessage.initialize_failure.title',
Expand All @@ -134,27 +192,15 @@ public function initializeIndexQueueAction(): ResponseInterface
ContextualFeedbackSeverity::ERROR
);
}
} else {
$messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
$titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
$this->addFlashMessage(
LocalizationUtility::translate($messageLabel, 'Solr'),
LocalizationUtility::translate($titleLabel, 'Solr'),
ContextualFeedbackSeverity::WARNING
);
}
$messagesForConfigurations = [];
foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
$itemCount = $this->indexQueue->getStatisticsBySite($this->selectedSite, $indexingConfigurationName)->getTotalCount();
$messagesForConfigurations[] = $indexingConfigurationName . ' (' . $itemCount . ' records)';
}

if (!empty($initializedIndexingConfigurations)) {
if (!empty($messagesForConfigurations)) {
$messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.success';
$titleLabel = 'solr.backend.index_queue_module.flashmessage.initialize.title';
$this->addFlashMessage(
LocalizationUtility::translate($messageLabel, 'Solr', [implode(', ', $messagesForConfigurations)]),
LocalizationUtility::translate($titleLabel, 'Solr')
LocalizationUtility::translate($titleLabel, 'Solr'),
ContextualFeedbackSeverity::OK
);
}

Expand All @@ -168,16 +214,18 @@ public function initializeIndexQueueAction(): ResponseInterface
*/
public function resetLogErrorsAction(): ResponseInterface
{
$resetResult = $this->indexQueue->resetAllErrors();
foreach ($this->enabledIndexQueues as $queue) {
$resetResult = $queue->resetAllErrors();

$label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors';
$severity = ContextualFeedbackSeverity::OK;
if (!$resetResult) {
$label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors';
$severity = ContextualFeedbackSeverity::ERROR;
}
$label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors';
$severity = ContextualFeedbackSeverity::OK;
if (!$resetResult) {
$label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors';
$severity = ContextualFeedbackSeverity::ERROR;
}

$this->addIndexQueueFlashMessage($label, $severity);
$this->addIndexQueueFlashMessage($label, $severity);
}

return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
}
Expand Down Expand Up @@ -264,4 +312,21 @@ protected function addIndexQueueFlashMessage(string $label, ContextualFeedbackSe
{
$this->addFlashMessage(LocalizationUtility::translate($label, 'Solr'), LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title', 'Solr'), $severity);
}

/**
* @return QueueInterface[]
*/
protected function getIndexQueues(): array
{
$queues = [];
$configuration = $this->selectedSite->getSolrConfiguration();
foreach ($configuration->getEnabledIndexQueueConfigurationNames() as $indexingConfiguration) {
$indexQueueClass = $configuration->getIndexQueueClassByConfigurationName($indexingConfiguration);
if (!isset($queues[$indexQueueClass])) {
$queues[$indexQueueClass] = GeneralUtility::makeInstance($indexQueueClass);
}
}

return $queues;
}
}
18 changes: 13 additions & 5 deletions Classes/Domain/Index/Queue/Statistic/QueueStatisticsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
class QueueStatisticsRepository extends AbstractRepository
{
protected string $table = 'tx_solr_indexqueue_item';
protected string $columnIndexed = 'indexed';
protected string $columnIndexingConfiguration = 'indexing_configuration';
protected string $columnChanged = 'changed';
protected string $columnErrors = 'errors';
protected string $columnRootpage = 'root';

/**
* Extracts the number of pending, indexed and erroneous items from the
Expand All @@ -43,23 +48,26 @@ public function findOneByRootPidAndOptionalIndexingConfigurationName(
$queryBuilder = $this->getQueryBuilder();
$queryBuilder
->add('select', vsprintf('(%s < %s) AS %s', [
$queryBuilder->quoteIdentifier('indexed'),
$queryBuilder->quoteIdentifier('changed'),
$queryBuilder->quoteIdentifier($this->columnIndexed),
$queryBuilder->quoteIdentifier($this->columnChanged),
$queryBuilder->quoteIdentifier('pending'),
]), true)
->add('select', vsprintf('(%s) AS %s', [
$queryBuilder->expr()->notLike('errors', $queryBuilder->createNamedParameter('')),
$queryBuilder->expr()->notLike($this->columnErrors, $queryBuilder->createNamedParameter('')),
$queryBuilder->quoteIdentifier('failed'),
]), true)
->add('select', $queryBuilder->expr()->count('*', 'count'), true)
->from($this->table)
->where(
$queryBuilder->expr()->eq('root', $queryBuilder->createNamedParameter($rootPid, PDO::PARAM_INT))
$queryBuilder->expr()->eq($this->columnRootpage, $queryBuilder->createNamedParameter($rootPid, PDO::PARAM_INT))
)->groupBy('pending', 'failed');

if (!empty($indexingConfigurationName)) {
$queryBuilder->andWhere(
$queryBuilder->expr()->eq('indexing_configuration', $queryBuilder->createNamedParameter($indexingConfigurationName))
$queryBuilder->expr()->eq(
$this->columnIndexingConfiguration,
$queryBuilder->createNamedParameter($indexingConfigurationName)
)
);
}

Expand Down

0 comments on commit 993f75a

Please sign in to comment.