You are here

function aggregator_cron in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/aggregator/aggregator.module \aggregator_cron()
  2. 4 modules/aggregator.module \aggregator_cron()
  3. 5 modules/aggregator/aggregator.module \aggregator_cron()
  4. 6 modules/aggregator/aggregator.module \aggregator_cron()
  5. 7 modules/aggregator/aggregator.module \aggregator_cron()

Implements hook_cron().

Queues news feeds for updates once their refresh interval has elapsed.

File

core/modules/aggregator/aggregator.module, line 160
Used to aggregate syndicated content (RSS, RDF, and Atom).

Code

function aggregator_cron() {
  $queue = \Drupal::queue('aggregator_feeds');
  $ids = \Drupal::entityTypeManager()
    ->getStorage('aggregator_feed')
    ->getFeedIdsToRefresh();
  foreach (Feed::loadMultiple($ids) as $feed) {
    if ($queue
      ->createItem($feed)) {

      // Add timestamp to avoid queueing item more than once.
      $feed
        ->setQueuedTime(REQUEST_TIME);
      $feed
        ->save();
    }
  }

  // Delete queued timestamp after 6 hours assuming the update has failed.
  $ids = \Drupal::entityQuery('aggregator_feed')
    ->accessCheck(FALSE)
    ->condition('queued', REQUEST_TIME - 3600 * 6, '<')
    ->execute();
  if ($ids) {
    $feeds = Feed::loadMultiple($ids);
    foreach ($feeds as $feed) {
      $feed
        ->setQueuedTime(0);
      $feed
        ->save();
    }
  }
}