function aggregator_cron

Same name and namespace in other branches
  1. 7.x modules/aggregator/aggregator.module \aggregator_cron()
  2. 8.9.x core/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 164

Code

function aggregator_cron() {
    $queue = \Drupal::queue('aggregator_feeds');
    $request_time = \Drupal::time()->getRequestTime();
    $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();
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.