<?php
/**
* @file includes/cron.inc
* @brief Cron Library
* @author Kenneth Smith <hide@address.com>
*
* Modularized Information Environment (MIE)
* Copyright (C) 2005-2006 by Kenneth Smith. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*/
defined('VALIE_MIE') || die(_('Direct access not allowed'));
defined('CRON_INC') && exit;
define('CRON_INC', true);
class CRON {
function insert($_name, $_class, $_file, $_conf, $_start, $_repeat = 0) {
global $db;
$sql = $db->rewrite('INSERT INTO t{crons} (k{name}, k{class}, k{file}, k{conf}, k{start}, k{repeat}) VALUES (%c, %c, %c, %c, %s, %i)', $_name, $_class, $_file, $_conf, $_start, $_repeat);
return $db->query($sql);
}
function mark($_id) {
global $db, $log;
$sql = $db->rewrite('UPDATE t{crons} SET k{done} = %s WHERE k{id} = %i', time(), $_id);
$db->query($sql);
if(isset($log)) {
$log->insert('cron', _('completed') . ': ' . $_id);
}
}
function clean() {
global $db;
$sql = $db->rewrite('DELETE FROM t{crons} WHERE k{done} IS NOT NULL AND k{done} < %s', time() - mie_conf('cron_loglife', 86400));
$db->query($sql);
}
function execute() {
global $db;
$this->clean();
$sql = $db->rewrite('SELECT * FROM t{crons} WHERE k{done} IS NULL AND k{start} <= %s ORDER BY k{start} LIMIT 0, %i', time(), mie_conf('cron_maxjobs', 10));
foreach($db->table($sql) as $r) {
$script_conf = unserialize((string)$r['conf']);
if(is_file($file = mie_root($r['file']))) {
include $file;
}
if(!empty($r['class'])) {
$c = new $r['class']($script_conf);
if(method_exists($c, 'initialize_script')) {
$c->initialize_script();
}
if(method_exists($c, 'execute')) {
$c->execute();
}
}
if($r['repeat'] > 0) {
$this->insert($r['name'], $r['class'], $r['file'], $r['conf'], strtotime($r['start']) + $r['repeat'], $r['repeat']);
}
$this->mark($r['id']);
}
}
}
?>