| 1 |
<?php |
<?php |
| 2 |
// $Id: dbcron.module,v 1.3 2006/01/01 09:21:05 jaza Exp $ |
// $Id: dbcron.module,v 1.4 2006/04/13 15:49:31 unconed Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 74 |
|
|
| 75 |
/** |
/** |
| 76 |
* Implementation of hook_cron(). |
* Implementation of hook_cron(). |
| 77 |
* |
* |
| 78 |
* Runs all cron queries that are due to run when this hook is fired. If any |
* Runs all cron queries that are due to run when this hook is fired. If any |
| 79 |
* queries have multiple parts, the multiple parts are broken up and run |
* queries have multiple parts, the multiple parts are broken up and run |
| 80 |
* separately. Each query has its execution speed timed, and this speed (along |
* separately. Each query has its execution speed timed, and this speed (along |
| 81 |
* with the current time) is saved to the database after execution. |
* with the current time) is saved to the database after execution. |
| 82 |
*/ |
*/ |
| 83 |
function dbcron_cron() { |
function dbcron_cron() { |
| 84 |
static $reset; |
static $reset; |
| 85 |
|
|
| 86 |
$leeway = (int)variable_get('dbcron_interval_leeway', 10); |
$leeway = (int)variable_get('dbcron_interval_leeway', 10); |
| 87 |
$result = db_query('SELECT * FROM {dbcron} WHERE (%d - last_run >= (run_interval - %d)) AND run_interval > 0', time(), $leeway); |
$result = db_query('SELECT * FROM {dbcron} WHERE (%d - last_run >= (run_interval - %d)) AND run_interval > 0', time(), $leeway); |
| 88 |
|
|
| 89 |
if (!isset($reset)) { |
if (!isset($reset)) { |
| 90 |
$reset = FALSE; |
$reset = FALSE; |
| 91 |
} |
} |
| 92 |
|
|
| 93 |
while ($dq = db_fetch_object($result)) { |
while ($dq = db_fetch_object($result)) { |
| 94 |
$queries = preg_split('/;(\n|\r)/', $dq->body); |
$queries = preg_split('/;(\n|\r)/', $dq->body); |
| 95 |
|
|
| 96 |
timer_start('dbcron'); |
timer_start('dbcron'); |
| 97 |
foreach ($queries as $query) { |
foreach ($queries as $query) { |
| 98 |
$query = preg_replace('/\n|\r/', '', $query); |
$query = preg_replace('/\n|\r/', '', $query); |
| 99 |
if (!empty($query)) { |
if (!empty($query)) { |
| 100 |
db_query($query); |
db_query($query); |
| 101 |
|
|
| 102 |
if (!$reset && variable_get('dbcron_search_reset', 0)) { |
if (!$reset && variable_get('dbcron_search_reset', 0)) { |
| 103 |
search_wipe(); |
search_wipe(); |
| 104 |
$reset = TRUE; |
$reset = TRUE; |
| 107 |
} |
} |
| 108 |
$timer = timer_stop('dbcron'); |
$timer = timer_stop('dbcron'); |
| 109 |
$speed = (int)$timer['time']; |
$speed = (int)$timer['time']; |
| 110 |
|
|
| 111 |
db_query('UPDATE {dbcron} SET last_run = %d, exec_speed = %d WHERE dqid = %d', time(), $speed, $dq->dqid); |
db_query('UPDATE {dbcron} SET last_run = %d, exec_speed = %d WHERE dqid = %d', time(), $speed, $dq->dqid); |
| 112 |
} |
} |
| 113 |
} |
} |
| 118 |
function dbcron_settings() { |
function dbcron_settings() { |
| 119 |
$form = array(); |
$form = array(); |
| 120 |
$options = array(0 => t('None'), 5 => format_interval(5), 10 => format_interval(10), 15 => format_interval(15), 20 => format_interval(20), 30 => format_interval(30), 60 => format_interval(60)); |
$options = array(0 => t('None'), 5 => format_interval(5), 10 => format_interval(10), 15 => format_interval(15), 20 => format_interval(20), 30 => format_interval(30), 60 => format_interval(60)); |
| 121 |
|
|
| 122 |
$form['dbcron_interval_leeway'] = array( |
$form['dbcron_interval_leeway'] = array( |
| 123 |
'#type' => 'select', |
'#type' => 'select', |
| 124 |
'#title' => t('Leeway in cron run intervals'), |
'#title' => t('Leeway in cron run intervals'), |
| 126 |
'#options' => $options, |
'#options' => $options, |
| 127 |
'#description' => t('The amount of leeway (in seconds) to be given to each cron query when determining (from its running interval) if it\'s time for the query to run.') |
'#description' => t('The amount of leeway (in seconds) to be given to each cron query when determining (from its running interval) if it\'s time for the query to run.') |
| 128 |
); |
); |
| 129 |
|
|
| 130 |
$options = array( |
$options = array( |
| 131 |
1 => t('Enabled'), |
1 => t('Enabled'), |
| 132 |
0 => t('Disabled'), |
0 => t('Disabled'), |
| 138 |
'#options' => $options, |
'#options' => $options, |
| 139 |
'#description' => t('If enabled, the site\'s search index will be reset every time a cron query is executed. Useful for cron queries that clear the site\'s content.') |
'#description' => t('If enabled, the site\'s search index will be reset every time a cron query is executed. Useful for cron queries that clear the site\'s content.') |
| 140 |
); |
); |
| 141 |
|
|
| 142 |
return $form; |
return $form; |
| 143 |
} |
} |
| 144 |
|
|
| 231 |
function dbcron_save_query($dq) { |
function dbcron_save_query($dq) { |
| 232 |
$period = _dbcron_get_run_intervals(); |
$period = _dbcron_get_run_intervals(); |
| 233 |
$run_interval = $period[$dq->run_interval]; |
$run_interval = $period[$dq->run_interval]; |
| 234 |
|
|
| 235 |
if ($dq->dqid) { |
if ($dq->dqid) { |
| 236 |
db_query("UPDATE {dbcron} SET title = '%s', body = '%s', run_interval = %d, last_run = 0 WHERE dqid = %d", $dq->title, $dq->body, $run_interval, $dq->dqid); |
db_query("UPDATE {dbcron} SET title = '%s', body = '%s', run_interval = %d, last_run = 0 WHERE dqid = %d", $dq->title, $dq->body, $run_interval, $dq->dqid); |
| 237 |
} |
} |
| 247 |
if (is_array($dq)) { |
if (is_array($dq)) { |
| 248 |
$dq = (object)$dq; |
$dq = (object)$dq; |
| 249 |
} |
} |
| 250 |
|
|
| 251 |
$form['title'] = array( |
$form['title'] = array( |
| 252 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 253 |
'#title' => t('Title'), |
'#title' => t('Title'), |
| 264 |
'#rows' => 20, |
'#rows' => 20, |
| 265 |
'#required' => TRUE, |
'#required' => TRUE, |
| 266 |
); |
); |
| 267 |
|
|
| 268 |
$period = _dbcron_get_run_intervals('form'); |
$period = _dbcron_get_run_intervals('form'); |
| 269 |
$period_vals = array_flip(_dbcron_get_run_intervals()); |
$period_vals = array_flip(_dbcron_get_run_intervals()); |
| 270 |
$form['run_interval'] = array( |
$form['run_interval'] = array( |
| 300 |
*/ |
*/ |
| 301 |
function dbcron_form_validate($form_id, $edit) { |
function dbcron_form_validate($form_id, $edit) { |
| 302 |
$dq = _dbcron_edit_into_object($edit); |
$dq = _dbcron_edit_into_object($edit); |
| 303 |
|
|
|
node_validate_title($dq); |
|
|
|
|
| 304 |
if (db_result(db_query("SELECT COUNT(run_interval) FROM {dbcron} WHERE dqid != %d AND title = '%s'", $dq->dqid, $dq->title))) { |
if (db_result(db_query("SELECT COUNT(run_interval) FROM {dbcron} WHERE dqid != %d AND title = '%s'", $dq->dqid, $dq->title))) { |
| 305 |
form_set_error('title', t('The title %title is already in use.', array('%title' => theme('placeholder', $dq->title)))); |
form_set_error('title', t('The title %title is already in use.', array('%title' => theme('placeholder', $dq->title)))); |
| 306 |
} |
} |
| 330 |
* |
* |
| 331 |
* @param $edit |
* @param $edit |
| 332 |
* The array of values, as returned by form interfaces. |
* The array of values, as returned by form interfaces. |
| 333 |
* |
* |
| 334 |
* @return |
* @return |
| 335 |
* Equivalent values in an object. |
* Equivalent values in an object. |
| 336 |
*/ |
*/ |
| 340 |
$dq->body = $edit['body']; |
$dq->body = $edit['body']; |
| 341 |
$dq->run_interval = $edit['run_interval']; |
$dq->run_interval = $edit['run_interval']; |
| 342 |
$dq->dqid = $edit['dqid']; |
$dq->dqid = $edit['dqid']; |
| 343 |
|
|
| 344 |
return $dq; |
return $dq; |
| 345 |
} |
} |
| 346 |
|
|
| 349 |
*/ |
*/ |
| 350 |
function _dbcron_get_run_intervals($type = NULL) { |
function _dbcron_get_run_intervals($type = NULL) { |
| 351 |
$period = array(300, 600, 900, 1800, 2700, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 432000, 604800); |
$period = array(300, 600, 900, 1800, 2700, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 432000, 604800); |
| 352 |
|
|
| 353 |
if ($type == 'form') { |
if ($type == 'form') { |
| 354 |
$period = drupal_map_assoc($period, 'format_interval'); |
$period = drupal_map_assoc($period, 'format_interval'); |
| 355 |
return array_merge(array(0 => t('Never')), $period); |
return array_merge(array(0 => t('Never')), $period); |