1: <?php
2:
3:
4: if(!isset($config)) {
5: require_once 'AlphaConfig.inc';
6: $config = AlphaConfig::getInstance();
7:
8: require_once $config->get('app.root').'alpha/util/AlphaAutoLoader.inc';
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: class AlphaCronManager {
58: 59: 60: 61: 62: 63:
64: private static $logger = null;
65:
66: 67: 68: 69: 70:
71: public function __construct() {
72: global $config;
73:
74: self::$logger = new Logger('AlphaCronManager');
75: self::$logger->setLogFile($config->get('app.file.store.dir').'logs/tasks.log');
76:
77: self::$logger->debug('>>__construct()');
78:
79: self::$logger->info('New AlphaCronManager invoked');
80:
81: $taskList = self::getTaskClassNames();
82:
83: self::$logger->info('Found ['.count($taskList).'] tasks in the directory ['.$config->get('app.root').'tasks]');
84:
85: foreach($taskList as $taskClass) {
86: self::$logger->info('Loading task ['.$taskClass.']');
87: self::loadClassDef($taskClass);
88: $task = new $taskClass;
89:
90: $startTime = microtime(true);
91: $maxAllowedTime = $startTime+$task->getMaxRunTime();
92:
93: self::$logger->info('Start time is ['.$startTime.'], maximum task run time is ['.$task->getMaxRunTime().']');
94:
95:
96: set_time_limit($task->getMaxRunTime());
97: $task->doTask();
98:
99: self::$logger->info('Done in ['.round(microtime(true)-$startTime, 5).'] seconds');
100: }
101:
102: self::$logger->info('Finished processing all cron tasks');
103:
104: self::$logger->debug('<<__construct');
105: }
106:
107: 108: 109: 110: 111: 112: 113:
114: public static function getTaskClassNames() {
115: global $config;
116:
117: if(self::$logger == null) {
118: self::$logger = new Logger('AlphaCronManager');
119: self::$logger->setLogFile($config->get('app.file.store.dir').'logs/tasks.log');
120: }
121: self::$logger->debug('>>getTaskClassNames()');
122:
123: global $config;
124:
125: $classNameArray = array();
126:
127: $handle = opendir($config->get('app.root').'tasks');
128:
129:
130: while (false !== ($file = readdir($handle))) {
131: if (preg_match("/Task.inc/", $file)) {
132: $classname = mb_substr($file, 0, -4);
133:
134: array_push($classNameArray, $classname);
135: }
136: }
137:
138: $handle = opendir($config->get('app.root').'alpha/tasks');
139:
140:
141: while (false !== ($file = readdir($handle))) {
142: if (preg_match("/Task.inc/", $file)) {
143: $classname = mb_substr($file, 0, -4);
144:
145: array_push($classNameArray, $classname);
146: }
147: }
148:
149: self::$logger->debug('<<getTaskClassNames ['.var_export($classNameArray, true).']');
150: return $classNameArray;
151: }
152:
153: 154: 155: 156: 157: 158:
159: public static function loadClassDef($classname) {
160: global $config;
161:
162: if(self::$logger == null) {
163: self::$logger = new Logger('AlphaCronManager');
164: self::$logger->setLogFile($config->get('app.file.store.dir').'logs/tasks.log');
165: }
166: self::$logger->debug('>>loadClassDef(classname=['.$classname.'])');
167:
168: if(file_exists($config->get('app.root').'tasks/'.$classname.'.inc'))
169: require_once $config->get('app.root').'tasks/'.$classname.'.inc';
170: elseif(file_exists($config->get('app.root').'alpha/tasks/'.$classname.'.inc'))
171: require_once $config->get('app.root').'alpha/tasks/'.$classname.'.inc';
172: else
173: throw new IllegalArguementException('The class ['.$classname.'] is not defined anywhere!');
174:
175: self::$logger->debug('<<loadClassDef');
176: }
177: }
178:
179:
180: $processor = new AlphaCronManager();
181:
182: ?>