From 54ae001d7bd8cf0c7c78ddfc871c8567a42a2cce Mon Sep 17 00:00:00 2001 From: Phil Tyler Date: Wed, 13 Nov 2024 09:50:12 -0800 Subject: [PATCH] Reapply "toms hasily coded slapdash core reload." This reverts commit 41de0217501c47ddf08621a694d1883095189b0b. --- .gitignore | 2 +- .../SolrConnector/PantheonSolrConnector.php | 209 +++++++++--------- 2 files changed, 106 insertions(+), 105 deletions(-) diff --git a/.gitignore b/.gitignore index b580ef1d..6baa65bf 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ .DS_Store docs/Badge.confluence -.envrc \ No newline at end of file +.envrc diff --git a/src/Plugin/SolrConnector/PantheonSolrConnector.php b/src/Plugin/SolrConnector/PantheonSolrConnector.php index f2a4740c..8d263452 100644 --- a/src/Plugin/SolrConnector/PantheonSolrConnector.php +++ b/src/Plugin/SolrConnector/PantheonSolrConnector.php @@ -13,9 +13,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\search_api_pantheon\Services\PantheonGuzzle; use Drupal\search_api_pantheon\Services\SolariumClient as PantheonSolariumClient; -use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Datetime\DateFormatterInterface; -use Drupal\Core\Messenger\MessengerInterface; /** * Pantheon Solr connector. @@ -64,27 +62,31 @@ class PantheonSolrConnector extends SolrConnectorPluginBase implements */ protected $messenger; + /** + * The container. + * + * @var \Symfony\Component\DependencyInjection\ContainerInterface + */ + protected ContainerInterface $container; + /** * Class constructor. */ public function __construct( - array $configuration, - $plugin_id, - array $plugin_definition, - LoggerChannelFactoryInterface $logger_factory, - PantheonGuzzle $pantheon_guzzle, - PantheonSolariumClient $solarium_client, - DateFormatterInterface $date_formatter, - MessengerInterface $messenger + array $configuration, + $plugin_id, + array $plugin_definition, + ContainerInterface $container, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->pantheonGuzzle = $pantheon_guzzle; - $this->solariumClient = $solarium_client; - $this->dateFormatter = $date_formatter; - $this->messenger = $messenger; - $this->setLogger($logger_factory->get('PantheonSearch')); + $this->pantheonGuzzle = $container->get('search_api_pantheon.pantheon_guzzle'); + $this->solariumClient = $container->get('search_api_pantheon.solarium_client'); + $this->dateFormatter = $container->get('date.formatter'); + $this->messenger = $container->get('messenger'); + $this->setLogger($container->get('logger.factory')->get('PantheonSearch')); $this->configuration['core'] = self::getPlatformConfig()['core']; $this->configuration['schema'] = self::getPlatformConfig()['schema']; + $this->container = $container; $this->connect(); } @@ -98,21 +100,17 @@ public function __construct( * @throws \Exception */ public static function create( - ContainerInterface $container, - array $configuration, - $plugin_id, - $plugin_definition - ) { + ContainerInterface $container, + array $configuration, + $plugin_id, + $plugin_definition + ) { return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('logger.factory'), - $container->get('search_api_pantheon.pantheon_guzzle'), - $container->get('search_api_pantheon.solarium_client'), - $container->get('date.formatter'), - $container->get('messenger') - ); + $configuration, + $plugin_id, + $plugin_definition, + $container, + ); } /** @@ -171,9 +169,9 @@ public function defaultConfiguration() { * Form render array. */ public function buildConfigurationForm( - array $form, - FormStateInterface $form_state - ) { + array $form, + FormStateInterface $form_state + ) { $form = parent::buildConfigurationForm($form, $form_state); $fields = [ @@ -209,9 +207,9 @@ function ($field_name) use ($fields) { * Form state object. */ public function validateConfigurationForm( - array &$form, - FormStateInterface $form_state - ) { + array &$form, + FormStateInterface $form_state + ) { } /** @@ -223,9 +221,9 @@ public function validateConfigurationForm( * Form state object. */ public function submitConfigurationForm( - array &$form, - FormStateInterface $form_state - ) { + array &$form, + FormStateInterface $form_state + ) { $configuration = array_merge($this->defaultConfiguration(), $form_state->getValues()); $this->setConfiguration($configuration); @@ -294,16 +292,16 @@ public function getStatsSummary() { } $summary = [ - '@pending_docs' => '', - '@autocommit_time_seconds' => '', - '@autocommit_time' => '', - '@deletes_by_id' => '', - '@deletes_by_query' => '', - '@deletes_total' => '', - '@schema_version' => '', - '@core_name' => '', - '@index_size' => '', - ]; + '@pending_docs' => '', + '@autocommit_time_seconds' => '', + '@autocommit_time' => '', + '@deletes_by_id' => '', + '@deletes_by_query' => '', + '@deletes_total' => '', + '@schema_version' => '', + '@core_name' => '', + '@index_size' => '', + ]; if (empty($stats) || empty($indexStats)) { return $summary; @@ -312,33 +310,33 @@ public function getStatsSummary() { $max_time = -1; $update_handler_stats = $stats['UPDATE']['updateHandler']['stats'] ?? -1; $summary['@pending_docs'] = - (int) $update_handler_stats['UPDATE.updateHandler.docsPending'] ?? -1; + (int) $update_handler_stats['UPDATE.updateHandler.docsPending'] ?? -1; if ( - isset( - $update_handler_stats['UPDATE.updateHandler.softAutoCommitMaxTime'] - ) - ) { + isset( + $update_handler_stats['UPDATE.updateHandler.softAutoCommitMaxTime'] + ) + ) { $max_time = - (int) $update_handler_stats['UPDATE.updateHandler.softAutoCommitMaxTime']; + (int) $update_handler_stats['UPDATE.updateHandler.softAutoCommitMaxTime']; } $summary['@deletes_by_id'] = - (int) $update_handler_stats['UPDATE.updateHandler.deletesById'] ?? -1; + (int) $update_handler_stats['UPDATE.updateHandler.deletesById'] ?? -1; $summary['@deletes_by_query'] = - (int) $update_handler_stats['UPDATE.updateHandler.deletesByQuery'] ?? -1; + (int) $update_handler_stats['UPDATE.updateHandler.deletesByQuery'] ?? -1; $summary['@core_name'] = - $stats['CORE']['core']['class'] ?? - $this->t('No information available.'); + $stats['CORE']['core']['class'] ?? + $this->t('No information available.'); $summary['@index_size'] = - $indexStats['numDocs'] ?? $this->t('No information available.'); + $indexStats['numDocs'] ?? $this->t('No information available.'); $summary['@autocommit_time_seconds'] = $max_time / 1000; $summary['@autocommit_time'] = $this->dateFormatter ->formatInterval($max_time / 1000); $summary['@deletes_total'] = - ( - intval($summary['@deletes_by_id'] ?? 0) - + intval($summary['@deletes_by_query'] ?? 0) - ) ?? -1; + ( + intval($summary['@deletes_by_id'] ?? 0) + + intval($summary['@deletes_by_query'] ?? 0) + ) ?? -1; $summary['@schema_version'] = $this->getSchemaVersionString(TRUE); return $summary; } @@ -347,9 +345,9 @@ public function getStatsSummary() { * {@inheritdoc} */ public function useTimeout( - string $timeout = self::QUERY_TIMEOUT, - ?Endpoint $endpoint = NULL - ) { + string $timeout = self::QUERY_TIMEOUT, + ?Endpoint $endpoint = NULL + ) { } /** @@ -361,25 +359,25 @@ public function viewSettings() { $view_settings = []; $view_settings[] = [ - 'label' => $this->t('Pantheon Sitename'), - 'info' => $this->getEndpoint()->getCore(), - ]; + 'label' => $this->t('Pantheon Sitename'), + 'info' => $this->getEndpoint()->getCore(), + ]; $view_settings[] = [ - 'label' => $this->t('Pantheon Environment'), - 'info' => getenv('PANTHEON_ENVIRONMENT'), - ]; + 'label' => $this->t('Pantheon Environment'), + 'info' => getenv('PANTHEON_ENVIRONMENT'), + ]; $view_settings[] = [ - 'label' => $this->t('Schema Version'), - 'info' => $this->getSchemaVersion(TRUE), - ]; + 'label' => $this->t('Schema Version'), + 'info' => $this->getSchemaVersion(TRUE), + ]; $core_info = $this->getCoreInfo(TRUE); foreach ($core_info['core'] as $key => $value) { if (is_string($value)) { $view_settings[] = [ - 'label' => ucwords($key), - 'info' => $value, - ]; + 'label' => ucwords($key), + 'info' => $value, + ]; } } @@ -406,9 +404,12 @@ public function getEndpoint($key = 'search_api_solr') { * Success or Failure. */ public function reloadCore() { - $this->logger->notice( - $this->t('Reload Core action for Pantheon Solr is automatic when Schema is updated.') - ); + $sp = $this->container->get('search_api_pantheon.schema_poster'); + if (!$sp instanceof SchemaPoster) { + throw new \RuntimeException('Unable to instantiate Schema Poster.'); + } + $sp->reloadServer(); + $this->logger->info('Core reloaded.'); return TRUE; } @@ -463,29 +464,29 @@ protected function createClient(array &$configuration) { */ protected function getStatsQuery(string $handler) { return json_decode( - $this->pantheonGuzzle - ->get( - $handler, - [ - 'query' => - [ - 'stats' => 'true', - 'wt' => 'json', - 'accept' => 'application/json', - 'contenttype' => 'application/json', - 'json.nl' => 'flat', - ], - 'headers' => - [ - 'Content-Type' => 'application/json', - 'Accept' => 'application/json', - ], - ] - ) - ->getBody(), - TRUE, - JSON_THROW_ON_ERROR - ); + $this->pantheonGuzzle + ->get( + $handler, + [ + 'query' => + [ + 'stats' => 'true', + 'wt' => 'json', + 'accept' => 'application/json', + 'contenttype' => 'application/json', + 'json.nl' => 'flat', + ], + 'headers' => + [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + ], + ] + ) + ->getBody(), + TRUE, + JSON_THROW_ON_ERROR + ); } }