function search_api_solr_cron in Search API Solr 8.3
Same name and namespace in other branches
- 8 search_api_solr.module \search_api_solr_cron()
- 8.2 search_api_solr.module \search_api_solr_cron()
- 7 search_api_solr.module \search_api_solr_cron()
- 4.x search_api_solr.module \search_api_solr_cron()
Implements hook_cron().
Used to execute an optimization operation on all enabled Solr servers once a day.
Throws
\Drupal\Component\Plugin\Exception\PluginException
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
\Drupal\search_api\SearchApiException
\Drupal\search_api_solr\SearchApiSolrException
File
- ./
search_api_solr.module, line 53
Code
function search_api_solr_cron() {
$document_counts = [];
$optimize = FALSE;
$request_time = \Drupal::time()
->getRequestTime();
// 86400 seconds are one day. We use slightly less here to allow for some
// variation in the request time of the cron run, so that the time of day will
// (more or less) stay the same.
if ($request_time - \Drupal::state()
->get('search_api_solr.last_optimize') > 86340) {
\Drupal::state()
->set('search_api_solr.last_optimize', $request_time);
// Delete cached endpoint data once a day.
\Drupal::state()
->delete('search_api_solr.endpoint.data');
$optimize = TRUE;
}
foreach (search_api_solr_get_servers() as $server) {
try {
/** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
$backend = $server
->getBackend();
$connector = $backend
->getSolrConnector();
if ($indexes = $server
->getIndexes()) {
$document_counts[$server
->id()] = $backend
->getDocumentCounts();
foreach ($indexes as $index) {
if ($backend
->finalizeIndex($index)) {
$backend
->getLogger()
->info('Cron finalized Solr server @server.', [
'@server' => $server
->label(),
]);
}
}
if ($optimize && $backend
->isOptimizeEnabled()) {
$connector
->optimize($backend
->getCollectionEndpoint($index));
$backend
->getLogger()
->info('Optimized Solr server @server.', [
'@server' => $server
->label(),
]);
}
}
} catch (SearchApiException $e) {
watchdog_exception('search_api', $e, '%type while maintaining Solr server @server: @message in %function (line %line of %file).', [
'@server' => $server
->label(),
]);
}
}
$search_all_rows = [];
foreach ($document_counts as $server_id => $counts) {
$search_all_rows[$server_id]['#total'] = Utility::normalizeMaxRows($counts['#total']);
unset($counts['#total']);
foreach ($counts as $site_hash => $index_doc_counts) {
foreach ($index_doc_counts as $index_id => $count) {
$search_all_rows[$server_id][$site_hash][$index_id] = Utility::normalizeMaxRows($count);
}
}
}
\Drupal::state()
->set('search_api_solr.search_all_rows', $search_all_rows);
}