1: <?php
2: /**
3: * Horde_Scheduler
4: *
5: * @package Scheduler
6: */
7: class Horde_Scheduler
8: {
9: /**
10: * Name of the sleep function.
11: *
12: * @var string
13: */
14: protected $_sleep;
15:
16: /**
17: * Adjustment factor to sleep in microseconds.
18: *
19: * @var integer
20: */
21: protected $_sleep_adj;
22:
23: /**
24: * Attempts to return a concrete Horde_Scheduler instance based on $driver.
25: *
26: * @param string $driver The type of concrete subclass to return.
27: * @param array $params A hash containing any additional configuration or
28: * connection parameters a subclass might need.
29: *
30: * @return Horde_Scheduler The newly created concrete instance.
31: * @throws Horde_Scheduler_Exception
32: */
33: static public function factory($driver, $params = null)
34: {
35: $driver = basename($driver);
36: $class = 'Horde_Scheduler_' . $driver;
37:
38: if (class_exists($class)) {
39: return new $class($params);
40: }
41:
42: throw new Horde_Scheduler_Exception('Class definition of ' . $class . ' not found.');
43: }
44:
45: /**
46: * Constructor.
47: *
48: * Figures out how we can best sleep with microsecond precision
49: * based on what platform we're running on.
50: */
51: public function __construct()
52: {
53: if (!strncasecmp(PHP_OS, 'WIN', 3)) {
54: $this->_sleep = 'sleep';
55: $this->_sleep_adj = 1000000;
56: } else {
57: $this->_sleep = 'usleep';
58: $this->_sleep_adj = 1;
59: }
60: }
61:
62: /**
63: * Main loop/action function.
64: */
65: public function run()
66: {
67: }
68:
69: /**
70: * Preserve the internal state of the scheduler object that we are
71: * passed, and save it to the Horde VFS backend. Horde_Scheduler
72: * objects should define __sleep() and __wakeup() serialization
73: * callbacks for anything that needs to be done at object
74: * serialization or deserialization - handling database
75: * connections, etc.
76: *
77: * @param string $id An id to uniquely identify this scheduler from
78: * others of the same class.
79: *
80: * @return boolean Success result.
81: */
82: public function serialize($id = '')
83: {
84: try {
85: $vfs = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create();
86: $vfs->writeData('.horde/scheduler', Horde_String::lower(get_class($this)) . $id, serialize($this), true);
87: return true;
88: } catch (Horde_Vfs_Exception $e) {
89: Horde::logMessage($e, 'ERR');
90: return false;
91: }
92: }
93:
94: /**
95: * Restore a Horde_Scheduler object from the cache.
96: *
97: * @param string $class The name of the object to restore.
98: * @param string $id An id to uniquely identify this
99: * scheduler from others of the same class.
100: * @param boolean $autosave Automatically store (serialize) the returned
101: * object at script shutdown.
102: *
103: * @see Horde_Scheduler::serialize()
104: */
105: public function unserialize($class, $id = '', $autosave = true)
106: {
107: /* Need a lowercase version of the classname, and a default instance of
108: * the scheduler object in case we can't retrieve one. */
109: $scheduler = new $class;
110:
111: try {
112: $vfs = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create();
113: $data = $vfs->read('.horde/scheduler', $class . $id);
114: if ($tmp = @unserialize($data)) {
115: $scheduler = $tmp;
116: }
117: } catch (Horde_Vfs_Exception $e) {
118: Horde::logMessage($e, 'ERR');
119: }
120:
121: if ($autosave) {
122: register_shutdown_function(array($scheduler, 'serialize'));
123: }
124:
125: return $scheduler;
126: }
127:
128: /**
129: * Platform-independant sleep $msec microseconds.
130: *
131: * @param integer $msec Microseconds to sleep.
132: */
133: public function sleep($msec)
134: {
135: call_user_func($this->_sleep, $msec / $this->_sleep_adj);
136: }
137:
138: }
139: