Skip to content

Commit

Permalink
reload core during schema update (#191)
Browse files Browse the repository at this point in the history
---------
Co-authored-by: Brian Weaver <[email protected]>
Co-authored-by: Kevin Porras <[email protected]>
Co-authored-by: Tom Stovall <[email protected]>
  • Loading branch information
pwtyler authored Nov 14, 2024
1 parent aa24a5f commit 1453eac
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 2 deletions.
1 change: 1 addition & 0 deletions .envrc.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export PANTHEON_INDEX_PORT=8983
export PANTHEON_INDEX_PATH=/solr
export PANTHEON_INDEX_CORE=${SOLR_CORE}
export PANTHEON_INDEX_SCHEME=http
export PANTHEON_INDEX_RELOAD_PATH=${SOLR_RELOAD_PATH}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
.idea
.DS_Store
docs/Badge.confluence

.envrc
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @pantheon-systems/site-experience
37 changes: 37 additions & 0 deletions RoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public function testFull(int $drupal_version = 9, string $site_name = NULL) {
// Test creating Solr index.
$this->testSolrIndexCreate($site_name, 'dev');

// Test running the reload
$this->testPantheonSolrReload($site_name, 'dev');
$this->testSolrReload($site_name, 'dev');

// Test select query.
$this->testSolrSelect($site_name, 'dev');

Expand Down Expand Up @@ -573,6 +577,7 @@ public function testModuleEnable(string $site_name, string $env = 'dev') {
'--yes',
'search_api_pantheon',
'search_api_pantheon_admin',
'search_api_solr_admin'
)
->run();
$this->taskExec(static::$TERMINUS_EXE)
Expand Down Expand Up @@ -802,9 +807,41 @@ public function testSolrIndexCreate(string $site_name, string $env = 'dev') {
if (!$result->wasSuccessful()) {
exit(1);
}
}

public function testPantheonSolrReload(string $site_name, string $env = 'dev') {
$result = $this->taskExec( static::$TERMINUS_EXE )
->args(
'drush',
"$site_name.$env",
'--',
'search-api-pantheon:reloadSchema',
)
->run();
if (!$result->wasSuccessful()) {
exit(1);
}
}

public function testSolrReload(string $site_name, string $env = 'dev') {
$result = $this->taskExec( static::$TERMINUS_EXE )
->args(
'drush',
"$site_name.$env",
'--',
'search-api-solr:reload',
'pantheon_solr8'
)
->run();
if (!$result->wasSuccessful()) {
exit(1);
}
}





/**
* Use search-api-pantheon:select command to ensure both Drupal index and the actual Solr index have the same amount of items.
*
Expand Down
12 changes: 12 additions & 0 deletions catalog-info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: search_api_pantheon
description: Auto-generated catalog info for pantheon-systems/search_api_pantheon
annotations:
backstage.io/techdocs-ref: dir:docs/
spec:
type: library
lifecycle: mature
owner: site
5 changes: 5 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ services:
arguments: [ "@logger.factory", "@search_api_pantheon.pantheon_guzzle", "@search_api_pantheon.endpoint", "@search_api_pantheon.solarium_client" ]
tags:
- { name: drush.command }
search_api_pantheon.drush_reload:
class: \Drupal\search_api_pantheon\Commands\Reload
arguments: [ "@logger.factory", "@search_api_pantheon.pantheon_guzzle", "@search_api_pantheon.schema_poster" ]
tags:
- { name: drush.command }
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<server name="PANTHEON_INDEX_PATH" value="${PANTHEON_INDEX_PATH}" />
<server name="PANTHEON_INDEX_CORE" value="${PANTHEON_INDEX_CORE}" />
<server name="PANTHEON_INDEX_SCHEMA" value="${PANTHEON_INDEX_SCHEMA}" />
<server name="PANTHEON_INDEX_RELOAD_PATH" value="${PANTHEON_INDEX_RELOAD_PATH}" />
</php>
<testsuites>
<testsuite name="unit">
Expand Down
3 changes: 3 additions & 0 deletions search_api_pantheon.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ services:
class: Drupal\search_api_pantheon\EventSubscriber\SearchApiPantheonSolrConfigFilesAlter
tags:
- { name: event_subscriber }
search_api_pantheon.reload:
class: Drupal\search_api_pantheon\Services\Reload
arguments: ['@logger.factory', '@search_api_pantheon.pantheon_guzzle']
3 changes: 3 additions & 0 deletions src/Commands/Diagnose.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public function diagnose() {
$this->logger->notice('Index PATH Value: {var}', [
'var' => $this->endpoint->getPath(),
]);
$this->logger->notice('Index RELOAD_PATH Value: {var}', [
'var' => $this->endpoint->getReloadPath(),
]);
$this->logger->notice('Testing bare Connection...');
$response = $this->pingSolrHost();
$this->logger->notice('Ping Received Response? {var}', [
Expand Down
68 changes: 68 additions & 0 deletions src/Commands/Reload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Drupal\search_api_pantheon\Commands;

use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\search_api_pantheon\Services\PantheonGuzzle;
use Drupal\search_api_pantheon\Services\SchemaPoster;
use Drush\Commands\DrushCommands;

/**
* Drush Search Api Pantheon Schema Commands.
*/
class Reload extends DrushCommands {
use LoggerChannelTrait;

/**
* Configured pantheon-solr-specific guzzle client.
*
* @var \Drupal\search_api_pantheon\Services\PantheonGuzzle
*/
private PantheonGuzzle $pantheonGuzzle;

/**
* Configured pantheon-solr-specific schema poster class.
*
* @var \Drupal\search_api_pantheon\Services\SchemaPoster
*/
private SchemaPoster $schemaPoster;

/**
* Class constructor.
*
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerChannelFactory
* Injected by container.
* @param \Drupal\search_api_pantheon\Services\PantheonGuzzle $pantheonGuzzle
* Injected by container.
* @param \Drupal\search_api_pantheon\Services\SchemaPoster $schemaPoster
* Injected by Container.
*/
public function __construct(
LoggerChannelFactoryInterface $loggerChannelFactory,
PantheonGuzzle $pantheonGuzzle,
SchemaPoster $schemaPoster
) {
$this->logger = $loggerChannelFactory->get('SearchAPIPantheon Drush');
$this->pantheonGuzzle = $pantheonGuzzle;
$this->schemaPoster = $schemaPoster;
}

/**
* Search_api_pantheon:reloadSchema.
*
* @usage search-api-pantheon:reloadSchema
* Reload the latest schema
*
* @command search-api-pantheon:reloadSchema
*/
public function reloadSchema() {
try {
$this->schemaPoster->reloadServer();
}
catch (\Exception $e) {
$this->logger->error((string) $e);
}
}

}
1 change: 1 addition & 0 deletions src/Plugin/SolrConnector/PantheonSolrConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public static function getPlatformConfig() {
'path' => getenv('PANTHEON_INDEX_PATH'),
'core' => getenv('PANTHEON_INDEX_CORE'),
'schema' => getenv('PANTHEON_INDEX_SCHEMA'),
'reload_path' => getenv('PANTHEON_INDEX_RELOAD_PATH'),
];
}

Expand Down
29 changes: 29 additions & 0 deletions src/Services/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ public function getSchemaUploadUri(): string {
);
}

/**
* Get URL in pantheon environment to POST reload requests.
*
* @return string
* URL of envrionment.
*/
public function getReloadUri(): string {
return vsprintf(
'%s://%s:%d/%s%s',
[
$this->getScheme(),
$this->getHost(),
$this->getPort(),
$this->getPath(),
$this->getReloadPath(),
]
);
}

/**
* Get the path for Schema Reloads.
*
* @return string
* The path for schema reloads
*/
public function getReloadPath(): string {
return $this->options['reload_path'];
}

/**
* Get the path for Schema Uploads.
*
Expand Down
10 changes: 8 additions & 2 deletions src/Services/PantheonGuzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ public function __construct(Endpoint $endpoint, LoggerChannelFactoryInterface $l
$config['cert'] = $cert;
}
parent::__construct($config);
$this->endpoint = $endpoint;
$this->logger = $logger_factory->get('PantheonGuzzle');
if (!$endpoint instanceof Endpoint) {
throw new \InvalidArgumentException('Endpoint must be an instance of Endpoint');
}
$this->setEndpoint($endpoint);
if ($logger_factory instanceof LoggerChannelFactoryInterface) {
$this->setLogger($logger_factory->get('PantheonGuzzle'));
}
$this->setLogger($logger_factory->get('PantheonGuzzle'));
}

/**
Expand Down
82 changes: 82 additions & 0 deletions src/Services/Reload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Drupal\search_api_pantheon\Services;

use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

class Reload implements LoggerAwareInterface {
use LoggerAwareTrait;

/**
* @var \Drupal\search_api_pantheon\Services\Reload
*/
protected $configuration;
/**
* @var \Drupal\search_api_pantheon\Services\Reload
*/
protected $logger_factory;

/**
* @var \Drupal\search_api_pantheon\Services\PantheonGuzzle
*/
protected PantheonGuzzle $client;

/**
* Class Constructor.
*/
public function __construct(
LoggerChannelFactoryInterface $logger_factory,
PantheonGuzzle $client,
) {
$this->setLogger($logger_factory->get('reload_service'));
$this->client = $client;
}

/**
* Reload the server after schema upload.
*
* @throws \Drupal\search_api_pantheon\Exceptions\PantheonSearchApiException
*/
public function reloadServer(): bool {
// Schema upload URL.
$uri = new Uri(
$this->getClient()
->getEndpoint()
->getReloadUri()
);

$this->logger->debug('Reload url: ' . (string) $uri);

// Send the request.
$request = new Request(
'POST',
$uri,
[
'Accept' => 'application/json',
'Content-Type' => 'application/json',
]
);
$response = $this->getClient()->sendRequest($request);

$status_code = $response->getStatusCode();
$reload_logger_content = [
'status_code' => $status_code,
'reason' => $response->getReasonPhrase(),
];
if ($status_code >= 200 && $status_code < 300) {
$this->logger->info('Server reloaded: {status_code} {reason}', $reload_logger_content);
return TRUE;
}
$this->logger->error('Server not reloaded: {status_code} {reason}', $reload_logger_content);
return FALSE;
}

public function getClient(): PantheonGuzzle {
return $this->client;
}

}
24 changes: 24 additions & 0 deletions src/Services/SchemaPoster.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class SchemaPoster implements LoggerAwareInterface {
*/
protected $moduleExtensionList;

protected LoggerChannelFactoryInterface $loggerFactory;

/**
* Class Constructor.
*/
Expand All @@ -68,6 +70,7 @@ public function __construct(
ModuleExtensionList $module_extension_list
) {
$this->logger = $logger_factory->get('PantheonSearch');
$this->loggerFactory = $logger_factory;
$this->client = $client;
$this->entityTypeManager = $entity_type_manager;
$this->moduleExtensionList = $module_extension_list;
Expand Down Expand Up @@ -105,9 +108,30 @@ public function postSchema(string $server_id, $files = []): array {
throw new \Exception('Cannot post schema to environment url.');
}

$status_code = $response->getStatusCode();
$this->logger->info('Status code: ' . $status_code);
if ($status_code >= 200 && $status_code < 300) {
// @TODO Maybe we need to capture exception here??
// Call reload on the server.
$this->reloadServer();
}
$this->logger->info('After server reload?');

return $this->processResponse($response);
}

/**
* Reload the server after schema upload.
*
* @throws \Drupal\search_api_pantheon\Exceptions\PantheonSearchApiException
*
* @return bool
*/
public function reloadServer(): bool {
$reload = new Reload($this->loggerFactory, $this->client);
return $reload->reloadServer();
}

/**
* Process response and return message to be shown to user.
*
Expand Down
Loading

0 comments on commit 1453eac

Please sign in to comment.