Commit 018ef051e405eb953a6f2be05a9d36bf83899341
1 parent
bf2e3602
KTS-2643
"basic tasks called by scheduler need to be in the source install" Updated. Committed By: Conrad Vermeulen Reviewed By: Megan Watson git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7668 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
90 additions
and
75 deletions
bin/scheduler.php
| ... | ... | @@ -88,20 +88,20 @@ function calculateRunTime($sFreq, $iTime) { |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | // Update the task information in the database |
| 91 | -function updateTask($sTable, $aFieldValues, $iId) { | |
| 92 | - DBUtil::autoUpdate($sTable, $aFieldValues, $iId); | |
| 91 | +function updateTask($aFieldValues, $iId) { | |
| 92 | + DBUtil::autoUpdate('scheduler_tasks', $aFieldValues, $iId); | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | // Get the list of tasks due to be run from the database |
| 96 | -function getTaskList($sTable) { | |
| 96 | +function getTaskList() { | |
| 97 | 97 | $now = date('Y-m-d H:i:s'); //time(); |
| 98 | - $query = "SELECT * FROM {$sTable} | |
| 99 | - WHERE is_complete = 0 AND run_time < '{$now}'"; | |
| 98 | + | |
| 99 | + $query = "SELECT * FROM scheduler_tasks WHERE is_complete = 0 AND run_time < '{$now}'"; | |
| 100 | 100 | |
| 101 | 101 | $result = DBUtil::getResultArray($query); |
| 102 | 102 | |
| 103 | 103 | if (PEAR::isError($result)){ |
| 104 | - exit(); | |
| 104 | + return false; | |
| 105 | 105 | } |
| 106 | 106 | return $result; |
| 107 | 107 | } |
| ... | ... | @@ -109,16 +109,22 @@ function getTaskList($sTable) { |
| 109 | 109 | |
| 110 | 110 | /* ** Scheduler script ** */ |
| 111 | 111 | |
| 112 | -$sTable = 'scheduler_tasks'; | |
| 112 | +global $default; | |
| 113 | 113 | |
| 114 | -// Get task list | |
| 115 | -$aList = getTaskList($sTable); | |
| 114 | +$default->log->debug('Scheduler: starting'); | |
| 116 | 115 | |
| 117 | -global $default; | |
| 116 | +// Get task list | |
| 117 | +$aList = getTaskList(); | |
| 118 | +if (empty($aList)) | |
| 119 | +{ | |
| 120 | + $default->log->debug('Scheduler: stopping - nothing to do'); | |
| 121 | + return; | |
| 122 | +} | |
| 118 | 123 | |
| 119 | 124 | // Loop through tasks and run |
| 120 | -if(!empty($aList)){ | |
| 121 | - foreach($aList as $item){ | |
| 125 | + | |
| 126 | + foreach($aList as $item) | |
| 127 | + { | |
| 122 | 128 | $aUpdate = array(); |
| 123 | 129 | $iEnd = 0; $iStart = 0; $iDuration = 0; |
| 124 | 130 | $sFreq = ''; $sParameters = ''; |
| ... | ... | @@ -132,64 +138,84 @@ if(!empty($aList)){ |
| 132 | 138 | $sParameters = $item['script_params']; |
| 133 | 139 | |
| 134 | 140 | // Check if script is windows or *nix compatible |
| 135 | - $extArr = explode('.', $sTaskUrl); | |
| 136 | - $ext = array_pop($extArr); | |
| 137 | - $script = implode('.', $extArr); | |
| 138 | - if(OS_WINDOWS){ | |
| 139 | - switch($ext){ | |
| 140 | - case 'sh': | |
| 141 | - $sTaskUrl = $script.'.bat'; | |
| 142 | - break; | |
| 143 | - case 'bin': | |
| 144 | - $sTaskUrl = $script.'.exe'; | |
| 145 | - break; | |
| 146 | - } | |
| 147 | - }else{ | |
| 148 | - switch($ext){ | |
| 149 | - case 'bat': | |
| 150 | - if(file_exists(KT_DIR . $script.'.sh')){ | |
| 151 | - $sTaskUrl = $script.'.sh'; | |
| 152 | - break; | |
| 153 | - } | |
| 154 | - // File doesn't exist - log error | |
| 155 | - $default->log->error("Scheduler: Task script can't be found at ".KT_DIR."{$script}.sh"); | |
| 156 | - continue; | |
| 157 | - break; | |
| 158 | - case 'exe': | |
| 159 | - if(file_exists(KT_DIR . $script)){ | |
| 160 | - $sTaskUrl = $script; | |
| 161 | - break; | |
| 162 | - } | |
| 163 | - if(file_exists(KT_DIR . $script.'.bin')){ | |
| 164 | - $sTaskUrl = $script.'.bin'; | |
| 165 | - break; | |
| 166 | - } | |
| 167 | - // File doesn't exist - log error | |
| 168 | - $default->log->error("Scheduler: Task script can't be found at ".KT_DIR."{$script} or ".KT_DIR."{$script}.bin"); | |
| 169 | - continue; | |
| 170 | - break; | |
| 171 | - } | |
| 141 | + $ext = pathinfo($sTaskUrl, PATHINFO_EXTENSION); | |
| 142 | + $script = substr($sTaskUrl,0,-strlen($ext)-1); | |
| 143 | + | |
| 144 | + if(OS_WINDOWS) | |
| 145 | + { | |
| 146 | + $mapping = array('sh'=>'bin','bat'=>'exe'); | |
| 147 | + if (array_key_exists($ext, $mapping)) | |
| 148 | + { | |
| 149 | + $sTaskUrl = $script . '.' . $mapping[$ext]; | |
| 150 | + } | |
| 151 | + } | |
| 152 | + else | |
| 153 | + { | |
| 154 | + $mapping = array('bat'=>'sh', 'exe'=>'bin'); | |
| 155 | + | |
| 156 | + if (array_key_exists($ext, $mapping)) | |
| 157 | + { | |
| 158 | + switch ($ext) | |
| 159 | + { | |
| 160 | + case 'exe': | |
| 161 | + if (is_executable(KT_DIR . '/' . $script)) | |
| 162 | + { | |
| 163 | + $sTaskUrl = $script; | |
| 164 | + break; | |
| 165 | + } | |
| 166 | + default: | |
| 167 | + $sTaskUrl = $script . '.' . $mapping[$ext]; | |
| 168 | + } | |
| 169 | + } | |
| 170 | + | |
| 171 | + if (!is_executable(KT_DIR . '/' . $script) && $ext != 'php') | |
| 172 | + { | |
| 173 | + $default->log->error("Scheduler: The script '{$sTaskUrl}' is not executable."); | |
| 174 | + continue; | |
| 175 | + } | |
| 176 | + } | |
| 177 | + | |
| 178 | + $file = realpath(KT_DIR . '/' . $sTaskUrl); | |
| 179 | + | |
| 180 | + if ($file === false) | |
| 181 | + { | |
| 182 | + $default->log->error("Scheduler: The script '{$sTaskUrl}' cannot be resolved."); | |
| 183 | + continue; | |
| 172 | 184 | } |
| 173 | 185 | |
| 174 | 186 | $iTime = time(); |
| 175 | - $iStart = explode(' ', microtime()); | |
| 187 | + $iStart = KTUtil::getBenchmarkTime(); | |
| 176 | 188 | |
| 177 | 189 | // Run the script |
| 178 | - $file = realpath(KT_DIR . '/' . $sTaskUrl); | |
| 179 | 190 | |
| 180 | 191 | $cmd = "\"$file\" {$sParameters}"; |
| 181 | 192 | |
| 182 | - $start = KTUtil::getBenchmarkTime(); | |
| 193 | + if ($ext == 'php') | |
| 194 | + { | |
| 195 | + $config = KTConfig::getSingleton(); | |
| 196 | + $phpPath = $config->get('externalBinary/php'); | |
| 197 | + | |
| 198 | + // being protective as some scripts work on relative paths | |
| 199 | + $dirname = dirname($file); | |
| 200 | + chdir($dirname); | |
| 201 | + | |
| 202 | + $cmd = "$phpPath $cmd"; | |
| 203 | + } | |
| 204 | + | |
| 183 | 205 | if (OS_WINDOWS) |
| 184 | 206 | { |
| 185 | 207 | $cmd = str_replace( '/','\\',$cmd); |
| 186 | - $res = `"$cmd" 2>&1`; | |
| 208 | + $res = `$cmd`; | |
| 187 | 209 | } |
| 188 | 210 | else |
| 189 | 211 | { |
| 190 | 212 | $res = shell_exec($cmd." 2>&1"); |
| 191 | 213 | } |
| 192 | 214 | |
| 215 | + // On completion - reset run time | |
| 216 | + $iEnd = KTUtil::getBenchmarkTime(); | |
| 217 | + $iDuration = number_format($iEnd - $iStart,2); | |
| 218 | + | |
| 193 | 219 | if (!empty($res)) |
| 194 | 220 | { |
| 195 | 221 | $default->log->info("Scheduler - Task: $sTask"); |
| ... | ... | @@ -199,37 +225,26 @@ if(!empty($aList)){ |
| 199 | 225 | } |
| 200 | 226 | else |
| 201 | 227 | { |
| 202 | - $time = number_format(KTUtil::getBenchmarkTime() - $start,2,'.',','); | |
| 203 | - $default->log->debug("Scheduler - Task: {$sTask} completed in {$diff}s."); | |
| 228 | + $default->log->debug("Scheduler - Task: {$sTask} completed in {$iDuration}s."); | |
| 204 | 229 | } |
| 205 | 230 | |
| 206 | - | |
| 207 | - // On completion - reset run time | |
| 208 | - $iEnd = explode(' ', microtime()); | |
| 209 | - $iDuration = ($iEnd[1] + $iEnd[0]) - ($iStart[1] + $iStart[0]); | |
| 210 | - $iDuration = round($iDuration, 3); | |
| 211 | - | |
| 212 | - if(($sFreq == 'once' || empty($sFreq)) && $retval !== FALSE){ | |
| 231 | + if(($sFreq == 'once' || empty($sFreq)) && $retval !== FALSE) | |
| 232 | + { | |
| 213 | 233 | // Set is_complete to true |
| 214 | 234 | $aUpdate['is_complete'] = '1'; |
| 215 | - }else{ | |
| 235 | + } | |
| 236 | + else | |
| 237 | + { | |
| 216 | 238 | $iNextTime = calculateRunTime($sFreq, $iTime); |
| 217 | 239 | $aUpdate['run_time'] = date('Y-m-d H:i:s', $iNextTime); |
| 218 | 240 | } |
| 241 | + | |
| 219 | 242 | $aUpdate['previous_run_time'] = date('Y-m-d H:i:s', $iTime); |
| 220 | 243 | $aUpdate['run_duration'] = $iDuration; |
| 221 | 244 | |
| 222 | - updateTask($sTable, $aUpdate, $item['id']); | |
| 223 | - | |
| 224 | - // clear parameters | |
| 225 | - if(!empty($aParams)){ | |
| 226 | - foreach($aParams as $param){ | |
| 227 | - $aParam = explode('=', $param); | |
| 228 | - $$aParam[0] = ''; | |
| 229 | - } | |
| 230 | - $aParam = array(); | |
| 231 | - $aParams = array(); | |
| 232 | - } | |
| 245 | + updateTask($aUpdate, $item['id']); | |
| 233 | 246 | } |
| 234 | -} | |
| 247 | + | |
| 248 | +$default->log->debug('Scheduler: stopping'); | |
| 249 | + | |
| 235 | 250 | ?> |
| 236 | 251 | \ No newline at end of file | ... | ... |