function search_api_solr_cron in Search API Solr 4.x
Same name and namespace in other branches
- 8.3 search_api_solr.module \search_api_solr_cron()
- 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()
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 = [];
$end_of_day = 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);
$end_of_day = 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 ($end_of_day) {
$endpoint = $backend
->getCollectionEndpoint($index);
// Delete "cached" endpoint data and schema_parts once a day. We keep
// theses in states instead of a caches because we might need to
// access this data even if Solr is temporarily not reachable and
// caches have been cleared or Solr itself is used as caching backend.
\Drupal::state()
->delete('search_api_solr.endpoint.data');
\Drupal::state()
->delete('search_api_solr.endpoint.schema_parts');
if ($backend
->isOptimizeEnabled()) {
$connector
->optimize($endpoint);
$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);
}