Skip to content

Commit

Permalink
Moving reload to it's own service
Browse files Browse the repository at this point in the history
  • Loading branch information
stovak committed Nov 13, 2024
1 parent 1471742 commit abbb048
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 37 deletions.
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']
7 changes: 3 additions & 4 deletions src/Plugin/SolrConnector/PantheonSolrConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,12 @@ public function __construct(
ContainerInterface $container,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configuration = array_merge($configuration, self::getPlatformConfig());
$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();
}
Expand Down Expand Up @@ -406,9 +405,9 @@ public function getEndpoint($key = 'search_api_solr') {
public function reloadCore() {
$sp = $this->container->get('search_api_pantheon.schema_poster');
if (!$sp instanceof SchemaPoster) {
throw new \RuntimeException('Unable to instantiate Schema Poster.');
$sp = \Drupal::getContainer()->get('search_api_pantheon.schema_poster');
}
$sp->reloadServer();
$sp->reloadCore();
$this->logger->info('Core reloaded.');
return TRUE;
}
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
83 changes: 83 additions & 0 deletions src/Services/Reload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Drupal\search_api_pantheon\Services;


use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\search_api_pantheon\Exceptions\PantheonSearchApiException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Drupal\search_api_pantheon\Services\PantheonGuzzle;

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->logger_factory = $logger_factory;
$this->client = $client;
}


/**
* Reload the server after schema upload.
*
* @throws \Drupal\search_api_pantheon\Exceptions\PantheonSearchApiException
*/
public function reloadServer(): void {
// 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;
}
$this->logger->error('Server not reloaded: {status_code} {reason}', $reload_logger_content);
throw new PantheonSearchApiException('Server not reloaded.');
}


}
33 changes: 2 additions & 31 deletions src/Services/SchemaPoster.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,37 +124,8 @@ public function postSchema(string $server_id, $files = []): array {
* @throws \Drupal\search_api_pantheon\Exceptions\PantheonSearchApiException
*/
public function reloadServer(): void {
// 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;
}
$this->logger->error('Server not reloaded: {status_code} {reason}', $reload_logger_content);
throw new PantheonSearchApiException('Server not reloaded.');
$reload = new Reload($this->logger, $this->client);
$reload->reloadServer();
}

/**
Expand Down

0 comments on commit abbb048

Please sign in to comment.