1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
<?php
require_once 'Horde.php';
require_once 'VFS.php';
/**
* Horde_Scheduler
*
* $Horde: framework/Scheduler/Scheduler.php,v 1.23.10.8 2005/12/01 15:27:23 chuck Exp $
*
* @package Horde_Scheduler
*/
class Horde_Scheduler {
/**
* Name of the sleep function.
*
* @var string
*/
var $_sleep;
/**
* Adjustment factor to sleep in microseconds.
*
* @var integer
*/
var $_sleep_adj;
/**
* Constructor.
*
* Figures out how we can best sleep with microsecond precision
* based on what platform we're running on.
*/
function Horde_Scheduler()
{
if (substr(PHP_OS, 0, 3) == 'WIN') {
$this->_sleep = 'sleep';
$this->_sleep_adj = 1000000;
} else {
$this->_sleep = 'usleep';
$this->_sleep_adj = 1;
}
}
/**
* Main loop/action function.
*
* @abstract
*/
function run()
{
}
/**
* Preserve the internal state of the scheduler object that we are
* passed, and save it to the Horde VFS backend. Horde_Scheduler
* objects should define __sleep() and __wakeup() serialization
* callbacks for anything that needs to be done at object
* serialization or deserialization - handling database
* connections, etc.
*
* @param string $id An id to uniquely identify this scheduler from
* others of the same class.
*/
function serialize($id = '')
{
$vfs = &VFS::singleton($GLOBALS['conf']['vfs']['type'],
Horde::getDriverConfig('vfs', $GLOBALS['conf']['vfs']['type']));
if (is_a($vfs, 'PEAR_Error')) {
Horde::logMessage($vfs, __FILE__, __LINE__, PEAR_LOG_ERR);
return false;
}
$result = $vfs->writeData('.horde/scheduler', String::lower(get_class($this)) . $id, serialize($this), true);
if (is_a($result, 'PEAR_Error')) {
Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
return false;
}
return true;
}
/**
* Restore a Horde_Scheduler object from the cache.
*
* @param string $class The name of the Horde_Scheduler object to restore.
* @param string $id An id to uniquely identify this
* scheduler from others of the same class.
* @param boolean $autosave Automatically store (serialize) the returned
* object at script shutdown.
*
* @see Horde_Scheduler::serialize()
*/
function &unserialize($class, $id = '', $autosave = true)
{
// Need a lowercase version of the classname, and a default
// instance of the scheduler object in case we can't retrieve
// one.
$class = strtolower($class);
$scheduler = &new $class;
$vfs = &VFS::singleton($GLOBALS['conf']['vfs']['type'],
Horde::getDriverConfig('vfs', $GLOBALS['conf']['vfs']['type']));
if (is_a($vfs, 'PEAR_Error')) {
Horde::logMessage($vfs, __FILE__, __LINE__, PEAR_LOG_ERR);
} else {
$data = $vfs->read('.horde/scheduler', $class . $id);
if (is_a($data, 'PEAR_Error')) {
Horde::logMessage($data, __FILE__, __LINE__, PEAR_LOG_INFO);
} else {
$scheduler = @unserialize($data);
if (!$scheduler) {
$scheduler = &new $class;
}
}
}
if ($autosave) {
register_shutdown_function(array(&$scheduler, 'serialize'));
}
return $scheduler;
}
/**
* Platform-independant sleep $msec microseconds.
*
* @param integer $msec Microseconds to sleep.
*/
function sleep($msec)
{
call_user_func($this->_sleep, $msec / $this->_sleep_adj);
}
/**
* Attempts to return a concrete Horde_Scheduler instance based on $driver.
*
* @param mixed $driver The type of concrete Horde_Scheduler subclass to
* return. If $driver is an array, then we will look
* in $driver[0]/lib/Horde_Scheduler/ for the
* subclass implementation named $driver[1].php.
* @param array $params A hash containing any additional configuration or
* connection parameters a subclass might need.
*
* @return Horde_Scheduler The newly created concrete Horde_Scheduler
* instance, or false on an error.
*/
function &factory($driver, $params = null)
{
if (is_array($driver)) {
$app = $driver[0];
$driver = $driver[1];
}
$driver = basename($driver);
if (empty($driver) || (strcmp($driver, 'none') == 0)) {
$scheduler = &new Horde_Scheduler();
return $scheduler;
}
if (!empty($app)) {
include_once $GLOBALS['registry']->get('fileroot', $app) . '/lib/Scheduler/' . $driver . '.php';
} elseif (@file_exists(dirname(__FILE__) . '/Scheduler/' . $driver . '.php')) {
include_once dirname(__FILE__) . '/Scheduler/' . $driver . '.php';
} else {
// Use include_once here to avoid a fatal error if the
// class isn't found.
@include_once 'Horde/Scheduler/' . $driver . '.php';
}
$class = 'Horde_Scheduler_' . $driver;
if (class_exists($class)) {
$scheduler = &new $class($params);
} else {
$scheduler = PEAR::raiseError('Class definition of ' . $class . ' not found.');
}
return $scheduler;
}
/**
* Attempts to return a reference to a concrete Horde_Scheduler
* instance based on $driver. It will only create a new instance
* if no Horde_Scheduler instance with the same parameters
* currently exists.
*
* This should be used if multiple schedulers (and, thus, multiple
* Horde_Scheduler instances) are required.
*
* This method must be invoked as: $var = &Horde_Scheduler::singleton()
*
* @param string $driver The type of concrete Horde_Scheduler subclass to
* return.
* @param array $params A hash containing any additional configuration or
* connection parameters a subclass might need.
*
* @return Horde_Scheduler The concrete Horde_Scheduler reference, or
* false on an error.
*/
function &singleton($driver, $params = null)
{
static $instances = array();
$signature = serialize(array($driver, $params));
if (!isset($instances[$signature])) {
$instances[$signature] = &Horde_Scheduler::factory($driver, $params);
}
return $instances[$signature];
}
}
|