Changeset 364 for dev/mods/ajcron
- Timestamp:
- 12/16/08 23:26:06 (14 years ago)
- Location:
- dev/mods/ajcron
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
dev/mods/ajcron/auto_settings.php
r363 r364 8 8 options::fadd('Reset next scheduled run to now', 'ajcron_resetNextRun', 'checkbox'); 9 9 options::fadd('Reset running jobs', 'ajcron_resetRunning', 'checkbox'); 10 options::fadd('Blocking Cronjobs', 'ajcron_blocking', 'checkbox'); 10 11 options::fadd('Next scheduled run', 'none', 'custom', array('ajcron', 'getNextRunDisplay'), array('ajcron', 'resetNextRunCheckbox')); 11 options::fadd('Jobs', 'ajcron_jobs', 'textarea:cols:60'); 12 options::fadd('Jobs', 'ajcron_jobs', 'textarea:cols:60:rows:10'); 13 options::fadd('Runtable', 'anone', 'custom', array('ajcron', 'getRuntable')); -
dev/mods/ajcron/init.php
r363 r364 31 31 { 32 32 $page->addBody('<script type="text/javascript" charset="UTF-8"> 33 var myLocalCallback = xajax.callback.create(400 , 1000);33 var myLocalCallback = xajax.callback.create(4000, 10000); 34 34 myLocalCallback.onExpiration = function(req) { xajax.abortRequest(req); } 35 35 xajax.call(\'xajax_req\', {callback: myLocalCallback});</script>'); … … 105 105 foreach ($arr as $job) 106 106 { 107 if (!trim($job)) 108 { 109 continue; 110 } 107 111 $tmp = preg_split("/".chr(32)."/", trim($job), -1, PREG_SPLIT_NO_EMPTY); 108 $nextrun = date('Y-m-d H:i:s' ,ajcron::getNextRun($tmp[0]));112 $nextrun = ajcron::getNextRun($tmp[0]); 109 113 $jobs[$i++] = array('id' => md5(trim($job)), 'nextrun' => $nextrun, 'name' => $tmp[2], 'url' => $tmp[1]); 110 114 } 111 115 112 return nl2br(var_export($jobs, true)); 116 return $jobs; 117 } 118 119 function getRuntable() 120 { 121 $output = ''; 122 // load up our crontasks 123 $jobs = ajcron::parseJobs(); 124 $state = config::get('ajcron_running'); 125 126 foreach ($jobs as $job) 127 { 128 if (isset($state[$job['id']])) 129 { 130 $running = 'running'; 131 } 132 else 133 { 134 $running = 'not running'; 135 } 136 137 if (empty($job['name'])) 138 { 139 $name = '-None found-'; 140 } 141 else 142 { 143 $name = $job['name']; 144 } 145 $output .= '<b>Name:</b> '.$name.' <b>URL:</b> '.$job['url'].' <b>Nextrun:</b> '.date('Y-m-d H:i:s', $job['nextrun']).'<br/>'; 146 $output .= ' <b>State:</b> '.$running.' <b>ID: </b>'.$job['id'].'<br/><br/>'; 147 } 148 149 return $output; 150 } 151 152 function getNextRuntime() 153 { 154 // calculate and set new runtime 155 $jobs = ajcron::parseJobs(); 156 $sorttable = array(); 157 // see which one should be started now 158 foreach ($jobs as $job) 159 { 160 $sorttable[$job['id']] = $job['nextrun']; 161 } 162 asort($sorttable); 163 config::set('ajcron_nextrun', $sorttable[key($sorttable)]); 113 164 } 114 165 115 166 function xajax_req() 116 167 { 168 // if the xajax call gets aborted, ignore that 117 169 @ignore_user_abort(true); 118 170 @set_time_limit(0); 119 // actual code goes here 171 172 $state = config::get('ajcron_running'); 173 if (!is_array($state)) 174 { 175 $state = array(); 176 config::set('ajcron_running', $state); 177 } 178 179 // check for blocking 180 if (config::get('ajcron_blocking')) 181 { 182 // if there is already something running, give up 183 if (count($state) >= 1) 184 { 185 return; 186 } 187 } 120 188 121 189 // load up our crontasks 122 190 $jobs = ajcron::parseJobs(); 123 191 192 // if there are no jobs just quit 124 193 if (!count($jobs)) 125 194 { 126 195 return; 127 196 } 128 $nextrun = $jobs[0]['nextrun']; 129 $id = null; 197 130 198 // see which one should be started now 131 foreach ($jobs as $job) 132 { 133 if ($nextrun > $job['nextrun']) 134 { 135 $nextrun = $job['nextrun']; 136 $id = $job['id']; 137 } 138 } 139 140 // start 141 #include_once('common/includes/class.http.php'); 142 #$http = new http_request($fetchurl); 143 #$http->set_timeout(120); 144 #$data = $http->get_content(); 145 146 // calculate and set new runtime 147 #config::set('ajcron_nextrun', time()); 199 $sorttable = array(); 200 foreach ($jobs as $job) 201 { 202 $sorttable[$job['id']] = $job['nextrun']; 203 } 204 asort($sorttable); 205 206 foreach ($sorttable as $id => $nextrun) 207 { 208 // this bypasses already running jobs 209 if (isset($state[$id])) 210 { 211 continue; 212 } 213 break; 214 } 215 216 if (!$id) 217 { 218 // no id found we could run as all are running 219 return; 220 } 221 222 // set current id to running 223 $state[$id] = 'running'; 224 $currentJob = null; 225 config::set('ajcron_running', $state); 226 foreach ($jobs as $job) 227 { 228 if ($job['id'] == $id) 229 { 230 $currentJob = $job; 231 } 232 } 233 234 // run the job (finally) 235 include_once('common/includes/class.http.php'); 236 $http = new http_request($currentJob['url']); 237 $http->set_timeout(120); 238 $data = $http->get_content(); 239 240 // job done, clean up 241 // we need to refresh our variable to prevent overwriting of 242 // other running jobs 243 $db = new DBQuery(true); 244 $db->execute('select * from kb3_config where cfg_site=\''.KB_SITE.'\' and cfg_key=\'ajcron_running\''); 245 $row = $db->getRow(); 246 $state = unserialize($row['cfg_value']); 247 unset($state[$id]); 248 config::set('ajcron_running', $state); 249 250 // calculate when next to insert ajax 251 ajcron::getNextRuntime(); 148 252 149 253 // testfun! 150 254 $objResponse = new xajaxResponse(); 151 $objResponse->Assign("header", "innerHTML", ajcron::parseJobs());255 #$objResponse->Assign("header", "innerHTML", nl2br(var_export($sorttable[key($sorttable)], true))); 152 256 #sleep(15); 153 257 return $objResponse;