You are here

function feedapi_cron in FeedAPI 6

Same name and namespace in other branches
  1. 5 feedapi.module \feedapi_cron()

Implementation of hook_cron().

1 string reference to 'feedapi_cron'
feedapi_cron_time in ./feedapi.module
Check for time limits in cron processing.

File

./feedapi.module, line 691
Handle the submodules (for feed and item processing) Provide a basic management of feeds

Code

function feedapi_cron() {
  global $user;

  // Saves the currently logged in user and start safe impersonating
  $original_user = $user;
  session_save_session(FALSE);
  db_query('DELETE FROM {feedapi_stat} WHERE timestamp < %d', variable_get('cron_semaphore', FALSE) - FEEDAPI_CRON_STAT_LIFETIME);

  // Initialize counters
  $count = array(
    '%feeds' => 0,
    '%expired' => 0,
    '%new' => 0,
    '%updated' => 0,
  );

  // We get feeds in small lots, this will save memory and have the process adjusting to the
  // time limit even when we have many thousands of them.
  $now = time();
  $process = 0;

  // The counter process will be > 0 if we've selected less feeds
  while (!$process && feedapi_cron_time()) {
    $process = FEEDAPI_CRON_FEEDS;
    $result = db_query_range("SELECT f.nid, n.uid FROM {feedapi} f JOIN {node} n ON n.vid = f.vid WHERE next_refresh_time <= %d AND next_refresh_time <> %d ORDER BY next_refresh_time ASC", $now, FEEDAPI_CRON_NEVER_REFRESH, 0, FEEDAPI_CRON_FEEDS);
    while (feedapi_cron_time() && ($feed = db_fetch_object($result))) {
      $user = user_load(array(
        'uid' => $feed->uid,
      ));

      // Call the refresh process for each feed and store counters
      $counter = feedapi_invoke('refresh', $feed, TRUE);
      if ($counter) {
        foreach ($counter as $name => $value) {
          $count['%' . $name] += $value;
        }
      }
      $count['%feeds']++;
      $process--;
    }
  }

  // Loads back the logged in user
  $user = $original_user;
  session_save_session(TRUE);
}