View source
<?php
function replication_help($section = '') {
$output = '';
switch ($section) {
case "admin/help#replication":
$output = '<p>' . t("Database Replication Tool") . '</p>';
break;
}
return $output;
}
function replication_perm() {
return array(
'administer replication',
);
}
function replication_menu() {
$items = array();
$items[] = array(
'path' => 'admin/settings/replication',
'title' => t('Replication settings'),
'callback' => 'drupal_get_form',
'callback arguments' => 'replication_admin',
'access' => user_access('administer replication'),
'description' => 'Configure database replication',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function replication_admin() {
$options = array();
$i = 0;
$themes = system_theme_data();
foreach ($themes as $key => $values) {
$options[$key] = $key;
$i++;
}
$form['replication'] = array(
'#type' => 'fieldset',
'#title' => t('General Settings'),
'#weight' => 0,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['replication']['replication_server_mode'] = array(
'#type' => 'select',
'#title' => 'Server mode',
'#required' => 'TRUE',
'#options' => array(
'0' => 'Single server',
'1' => 'Master mode',
'2' => 'Replication slave',
),
'#default_value' => variable_get('replication_server_mode', '0'),
'#description' => t('If you are not running a multi-server system, you should leave this set to "Single Server" mode'),
);
$form['replication']['replication_admin_identifier'] = array(
'#type' => 'textfield',
'#default_value' => variable_get('replication_admin_identifier', ''),
'#title' => t('Slave Server identifier'),
'#maxlength' => '10',
'#size' => '10',
'#description' => t("Enter a unique server identifier - e.g 'slaveid001'"),
);
$form['replication']['replication_admin_max_recheck'] = array(
'#type' => 'textfield',
'#default_value' => variable_get('replication_admin_max_recheck', '10'),
'#title' => t('Maximum re-tests'),
'#required' => 'TRUE',
'#maxlength' => '2',
'#size' => '2',
'#description' => t("To test connectivity with the master server a slave writes data to the master then reads locally. Enter a value for the maximum number of times the slave will repeat this test during a replication check."),
);
$form['replication']['replication_admin_wait_recheck'] = array(
'#type' => 'textfield',
'#default_value' => variable_get('replication_admin_wait_recheck', '1'),
'#title' => t('Re-test delay.'),
'#required' => 'TRUE',
'#maxlength' => '2',
'#size' => '2',
'#description' => t("Enter a value in seconds to wait before re-checking connectivity after a failed test. Note that a longer delay may cause cron to fail."),
);
$form['replication_theme'] = array(
'#type' => 'fieldset',
'#title' => t('Theme configuration'),
'#weight' => 0,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['replication_theme']['replication_admin_normal_theme'] = array(
'#type' => 'select',
'#options' => $options,
'#default_value' => variable_get('replication_admin_normal_theme', 'garland'),
'#title' => t('Regular theme'),
'#required' => 'TRUE',
'#description' => t("The theme to be used when the mysql master is available."),
);
$form['replication_theme']['replication_admin_warning_theme'] = array(
'#type' => 'select',
'#options' => $options,
'#default_value' => variable_get('replication_admin_warning_theme', 'garland'),
'#title' => t('Recovery theme'),
'#required' => 'TRUE',
'#description' => t("The theme to be used when the mysql master is unavailable."),
);
$form['replication_http'] = array(
'#type' => 'fieldset',
'#title' => 'HTTP replication settings',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['replication_http']['replication_server_url'] = array(
'#type' => 'textfield',
'#title' => 'Master URL',
'#maxlength' => 255,
'#default_value' => variable_get('replication_server_url', ''),
'#description' => t('If this node is a replication slave, you should specify the master server url here - including the leading http:// or https:// etc. Otherwise, leave this field blank. DO NOT include a trailing slash'),
);
$form['replication_http']['replication_http_port'] = array(
'#type' => 'textfield',
'#title' => 'Replication Port',
'#maxlength' => 5,
'#size' => 5,
'#default_value' => variable_get('replication_http_port', ''),
'#description' => t('By default the port used for http transfers is 80 for http and 443 for https. If you wish to use a different port you should specify it here. Otherwise, leave this field blank'),
);
$form['replication_http']['replication_http_connect'] = array(
'#type' => 'textfield',
'#title' => 'Replication connection timeout (in Seconds)',
'#maxlength' => 5,
'#size' => 5,
'#default_value' => variable_get('replication_http_connect', '5'),
'#description' => t('This is the time allowed to establish the http connection - Enter a value in seconds.'),
);
$form['replication_http']['replication_http_timeout'] = array(
'#type' => 'textfield',
'#title' => 'Replication timeout (Milliseconds per Kilobyte)',
'#maxlength' => 6,
'#size' => 6,
'#default_value' => variable_get('replication_http_timeout', '500'),
'#description' => t('This controls the MINIMUM data speed. Enter a value in milliseconds.'),
);
$form['replication_http']['replication_http_speedlimit'] = array(
'#type' => 'textfield',
'#title' => 'Maximum speed (Kilobits per second)',
'#maxlength' => 10,
'#size' => 10,
'#default_value' => variable_get('replication_http_speedlimit', '128'),
'#description' => t('Enter a value in kilobits.'),
);
$form['replication_http']['replication_http_retries'] = array(
'#type' => 'textfield',
'#title' => 'Maximum number of retries',
'#maxlength' => 3,
'#size' => 3,
'#default_value' => variable_get('replication_http_retries', '5'),
'#description' => t('Enter a number for the maximum number of attempts.'),
);
return system_settings_form($form);
}
function replication_cron() {
if (variable_get('replication_server_mode', '0') != 2) {
return;
}
$previoustimestamp = variable_get('replication_timestamp', 0);
$recoverytheme = variable_get('replication_admin_warning_theme', 'garland');
$normaltheme = variable_get('replication_admin_normal_theme', 'garland');
$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;
while ($retries < variable_get('replication_admin_max_recheck', '10')) {
$selresult = db_query($selectquery);
$links = db_fetch_object($selresult);
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);
}
}