You are here

function replication_cron in Replication 5

Same name and namespace in other branches
  1. 5.0 replication.module \replication_cron()

Implementation of hook_cron note that the variable 'replication_online' is an internal variable - there is no admin option to set it in the long term it will also be switched off when any db_query fails in database.inc

File

./replication.module, line 219

Code

function replication_cron() {
  if (variable_get('replication_server_mode', '0') != 2) {
    return;
  }

  // Check the timestamp from the last run
  $previoustimestamp = variable_get('replication_timestamp', 0);
  $recoverytheme = variable_get('replication_admin_warning_theme', 'garland');
  $normaltheme = variable_get('replication_admin_normal_theme', 'garland');

  // Here are the queries we run to check if replication is working
  $deletequery = sprintf("DELETE from {replication} where slaveid='%s'", variable_get('replication_admin_identifier', ''));
  $insertquery = sprintf("INSERT into {replication} (slaveid) VALUES ('%s')", variable_get('replication_admin_identifier', ''));
  $selectquery = sprintf("SELECT timestamp FROM {replication} WHERE slaveid='%s'", variable_get('replication_admin_identifier', 'slaveid001'));
  $delresult = db_query($deletequery);
  $insresult = db_query($insertquery);
  $retries = 0;

  // if we havne't tried more than the defined max times then...
  while ($retries < variable_get('replication_admin_max_recheck', '10')) {
    $selresult = db_query($selectquery);
    $links = db_fetch_object($selresult);

    // If the current timestamp is different then it's a success and we can stop
    if ($links->timestamp != $previoustimestamp) {
      variable_set('replication_online', '1');
      variable_set('replication_timestamp', $links->timestamp);
      variable_set('theme_default', $normaltheme);
      $retries = variable_get('replication_admin_max_recheck', '10');
    }
    else {
      sleep(variable_get('replication_admin_wait_recheck', '1'));
    }
    $retries++;
  }
  if ($links->timestamp == $previoustimestamp) {
    variable_set('replication_online', '0');
    variable_set('theme_default', $recoverytheme);
  }
}