You are here

function prod_monitor_cron in Production check & Production monitor 6

Same name and namespace in other branches
  1. 7 prod_monitor/prod_monitor.module \prod_monitor_cron()

Implementation of hook_cron()

File

prod_monitor/prod_monitor.module, line 330

Code

function prod_monitor_cron() {
  if (variable_get('prod_monitor_cron_running', FALSE)) {
    watchdog('prod_monitor', 'Last cron run was not properly terminated!', array(), WATCHDOG_ERROR);
  }
  $sites = _prod_monitor_get_sites(variable_get('prod_monitor_cron_start_at', 0));

  // Indicate we're running.
  variable_set('prod_monitor_cron_running', TRUE);
  $cron_start = time();

  // 180 seconds run max!
  $time_limit = 180;
  $elapsed = $process = 0;
  foreach ($sites as $id => $site_info) {
    $elapsed = time() - $cron_start;
    if ($elapsed < $time_limit) {

      // First: all checks.
      _prod_monitor_retrieve_data($id, $site_info);

      // MUST be second because of the status update!
      _prod_monitor_db_connect_check($id, $site_info);
      $process++;
    }
    else {

      // Time's up! Start with this site next time.
      variable_set('prod_monitor_cron_start_at', $id);
      break;
    }
  }

  // If all was processed, make sure we start from the top next time
  if ($process >= count($sites)) {
    variable_set('prod_monitor_cron_start_at', 0);
  }
  watchdog('prod_monitor', '!count sites updated successfully in !time seconds.', array(
    '!count' => $process,
    '!time' => $elapsed,
  ), WATCHDOG_NOTICE);

  // Indicate we've stopped.
  variable_del('prod_monitor_cron_running');
}