Pseudo-cron first cut. Props Owen. #2425

git-svn-id: http://svn.automattic.com/wordpress/trunk@3512 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2006-02-11 02:24:47 +00:00
parent 67968a6127
commit 9f50b562b5
1 changed files with 67 additions and 0 deletions

View File

@ -2317,4 +2317,71 @@ function get_num_queries() {
return $wpdb->num_queries;
}
function wp_schedule_event($timestamp, $recurrence, $hook) {
$args = array_slice(func_get_args(), 3);
$crons = get_option('cron');
$crons[$timestamp][$hook] = array('recur' => $recurrence, 'args' => $args);
ksort($crons);
update_option('cron', $crons);
}
function wp_unschedule_event($timestamp, $hook) {
$crons = get_option('cron');
unset($crons[$timestamp][$hook]);
if ( empty($crons[$timestamp]) )
unset($crons[$timestamp]);
update_option('cron', $crons);
}
function wp_clear_scheduled_hook($hook) {
while($timestamp = next_scheduled('scheduled_hook'))
wp_unschedule_event($timestamp, 'scheduled_hook');
}
function next_scheduled($hook) {
$crons = get_option('cron');
if ( empty($crons) )
return false;
foreach($crons as $timestamp => $cron) {
//if($timestamp <= time()) continue;
if(isset($cron[$hook])) return $timestamp;
}
return false;
}
function spawn_cron() {
if (array_shift(array_keys(get_option('cron'))) > time()) return;
$cron_url = get_settings('siteurl') . '/wp-cron.php';
$parts = parse_url($cron_url);
$argyle = @ fsockopen($parts['host'], $_SERVER['SERVER_PORT'], $errno, $errstr, 0.01);
if ( $argyle )
fputs($argyle, "GET {$parts['path']}?time=" . time() . '&check='
. md5(DB_PASS . '187425') . " HTTP/1.0\r\nHost: {$_SERVER['HTTP_HOST']}\r\n\r\n");
}
function wp_cron() {
if (array_shift(array_keys(get_option('cron'))) > time())
return;
$crons = get_option('cron');
$newcrons = $crons;
foreach ($crons as $timestamp => $cronhooks) {
if ($timestamp > time()) break;
foreach($cronhooks as $hook => $args) {
do_action($hook, $args['args']);
$recurrence = $args['recur'];
if ( 'hourly' == $recurrence ) {
$args = array_merge( array($timestamp + 3600, $recurrence, $hook), $args['args']);
call_user_func_array('wp_schedule_event', $args);
} else if ( 'daily' == $recurrence ) {
$args = array_merge( array($timestamp + 86400, $recurrence, $hook), $args['args']);
call_user_func_array('wp_schedule_event', $args);
}
wp_unschedule_event($timestamp, $hook);
}
}
}
?>