forked from TYPO3-Solr/ext-solr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK:BP:11.6] Consider queue initialization status
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. Ports: TYPO3-Solr#3764 Resolves: TYPO3-Solr#3763
- Loading branch information
1 parent
65e1ef2
commit 1bb4381
Showing
3 changed files
with
117 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ | |
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\QueueInitializationService; | ||
use ApacheSolrForTypo3\Solr\IndexQueue\Queue; | ||
use ApacheSolrForTypo3\Solr\IndexQueue\QueueInterface; | ||
use ApacheSolrForTypo3\Solr\IndexQueue\QueueInitializationServiceAwareInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use TYPO3\CMS\Backend\Form\Exception as BackendFormException; | ||
use TYPO3\CMS\Core\Http\RedirectResponse; | ||
|
@@ -32,25 +31,43 @@ | |
/** | ||
* Index Queue Module | ||
* | ||
* @todo: Support all index queues in actions beside "initializeIndexQueueAction" and | ||
* "resetLogErrorsAction" | ||
* | ||
* @author Ingo Renner <[email protected]> | ||
*/ | ||
class IndexQueueModuleController extends AbstractModuleController | ||
{ | ||
/** | ||
* The default Solr Queue | ||
* @var QueueInterface | ||
*/ | ||
protected QueueInterface $indexQueue; | ||
|
||
/** | ||
* Enabled Solr index queues | ||
* | ||
* @var QueueInterface[] | ||
*/ | ||
protected array $enabledIndexQueues; | ||
|
||
/** | ||
* Initializes the controller before invoking an action method. | ||
*/ | ||
protected function initializeAction() | ||
{ | ||
parent::initializeAction(); | ||
$this->indexQueue = GeneralUtility::makeInstance(Queue::class); | ||
|
||
$this->enabledIndexQueues = $this->getIndexQueues(); | ||
if (!empty($this->enabledIndexQueues)) { | ||
$this->indexQueue = $this->enabledIndexQueues[Queue::class] ?? reset($this->enabledIndexQueues); | ||
} | ||
} | ||
|
||
/** | ||
* Sets the default queue to use | ||
* | ||
* @param QueueInterface $indexQueue | ||
*/ | ||
public function setIndexQueue(QueueInterface $indexQueue) | ||
|
@@ -88,6 +105,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; | ||
|
@@ -122,24 +144,63 @@ public function initializeIndexQueueAction(): ResponseInterface | |
|
||
$indexingConfigurationsToInitialize = GeneralUtility::_POST('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); | ||
/** @var QueueInitializationService $initializationService */ | ||
$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' | ||
), | ||
FlashMessage::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'), | ||
FlashMessage::WARNING | ||
); | ||
} | ||
|
||
$initializedIndexingConfigurations = $initializationService->initializeBySiteAndIndexConfigurations($this->selectedSite, $indexingConfigurationsToInitialize); | ||
} catch (\Throwable $e) { | ||
$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', | ||
|
@@ -148,22 +209,9 @@ public function initializeIndexQueueAction(): ResponseInterface | |
FlashMessage::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'), | ||
FlashMessage::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( | ||
|
@@ -183,17 +231,19 @@ 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 = FlashMessage::OK; | ||
if (!$resetResult) { | ||
$label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors'; | ||
$severity = FlashMessage::ERROR; | ||
} | ||
|
||
$label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors'; | ||
$severity = FlashMessage::OK; | ||
if (!$resetResult) { | ||
$label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors'; | ||
$severity = FlashMessage::ERROR; | ||
$this->addIndexQueueFlashMessage($label, $severity); | ||
} | ||
|
||
$this->addIndexQueueFlashMessage($label, $severity); | ||
|
||
return new RedirectResponse($this->uriBuilder->uriFor('index'), 303); | ||
} | ||
|
||
|
@@ -278,4 +328,23 @@ protected function addIndexQueueFlashMessage(string $label, int $severity) | |
{ | ||
$this->addFlashMessage(LocalizationUtility::translate($label, 'Solr'), LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title', 'Solr'), $severity); | ||
} | ||
|
||
/** | ||
* Get index queues | ||
* | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters