2

To prevent fatal errors, I need to check if solr is running.

Given:

$solrIndex = \Drupal::entityTypeManager()
  ->getStorage('search_api_index')
  ->load('global');

How do I check if the solr server that $solrIndex is pointing to is online?

There is an ->isAvailable() method on the server object when you call the $index's ->getServerInstance() method, but it returns a false positive when the solr server is stopped:

I'm surprised I can't check it from the server object, but this returns a false positive when solr is stopped:

$false_positive = $solrIndex->getServerInstance()->isAvailable();
3

The SolrConnector plugin provides a pingServer() function that will determine it the endpoint can be accessed, so to really test it you likely need to be able to call that.

I'd first check to see if there is an abled server by calling $solrIndex->isServerEnabled().

Then I'd get the server, its backend, the connector, and run the ping:

$server = $solrIndex->getServerInstance();
$backend = $server->getBackend();
$connector = $backend->getSolrConnector();
$response = $connector->pingServer();

or just

if( $solrIndex->getServerInstance()->getBackend()->getSolrConnector()->pingServer() ){ 
  // Yup
} 
else {
 // nope
}

The return value from pingServer() is:

The latency in milliseconds if the core can be accessed, otherwise FALSE.

  • thanks, you nailed it. I just found the answer myself after poking around. Any idea why $solrIndex->getServerInstance()->isAvailable() returns a false positive? – oknate Nov 16 '16 at 19:07
  • 1
    From a quick look: it's a bug. The BackendPluginBase defines isAvailable as just return TRUE, and SearchApiSolrBackend does not override that function. I suspect they would welcome a patch that implments isAvialable properly (using pingServer() or doPing() most likely). – acrosman Nov 16 '16 at 19:12
  • I'll create a ticket. – oknate Nov 16 '16 at 19:19
  • drupal.org/node/2828204 – oknate Nov 16 '16 at 19:41
1

I was able to check if the server is up this way:

$solrConnector = $solrIndex
  ->getServerInstance()
  ->getBackend()
  ->getSolrConnector();

$solr_status = ($solrConnector->pingServer() !== FALSE);

if (!$solr_status) {
  // handle Solr being unavailable
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.