By berdir on
Change record status:
Draft (View all draft change records)
Project:
Introduced in branch:
10.3.x
Introduced in version:
10.3.0
Issue links:
Description:
Additionally to hooks, it is now possible that services are called directly when cron tasks are executed.
Before
/**
* Implements hook_cron().
*
* Fires updateIndex() in the plugins for all indexable active search pages,
* and cleans up dirty words.
*/
-function search_cron() {
/** @var \Drupal\search\SearchPageRepositoryInterface $search_page_repository */
$search_page_repository = \Drupal::service('search.search_page_repository');
foreach ($search_page_repository->getIndexableSearchPages() as $entity) {
$entity->getPlugin()->updateIndex();
}
}
After
1. Implement the runCron() method of Drupal\Core\CronSubscriberInterface in the chosen service:
class SearchPageRepository implements SearchPageRepositoryInterface, CronSubscriberInterface {
/**
* {@inheritdoc}
*/
public function runCron(): void {
// Fires updateIndex() in the plugins for all indexable active search pages,
// and cleans up dirty words.
foreach ($this->getIndexableSearchPages() as $entity) {
$entity->getPlugin()->updateIndex();
}
}
}
2. Add the cron tag to the service definition:
search.search_page_repository:
class: Drupal\search\SearchPageRepository
arguments: ['@config.factory', '@entity_type.manager']
tags:
- { name: cron }
Hooks are not yet deprecated, although that may happen in a future update. A single module can have have multiple services that all have their own cron task. Note that alternative implementations of cron runners like the ultimate_cron module will need to be updated to support discovery of such cron services.
Impacts:
Module developers