Commit fa333a38791f546ac91b75cf3f8db3af9c32272e
1 parent
16654410
KTS-1554
"Implement a Scheduler" Added the script for running the scheduled tasks. Committed by: Megan Watson Reviewed by: Conrad Vermeulen git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7134 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
163 additions
and
0 deletions
bin/scheduler.php
0 → 100644
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * $Id: | |
| 4 | + * | |
| 5 | + * The contents of this file are subject to the KnowledgeTree Public | |
| 6 | + * License Version 1.1.2 ("License"); You may not use this file except in | |
| 7 | + * compliance with the License. You may obtain a copy of the License at | |
| 8 | + * http://www.knowledgetree.com/KPL | |
| 9 | + * | |
| 10 | + * Software distributed under the License is distributed on an "AS IS" | |
| 11 | + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. | |
| 12 | + * See the License for the specific language governing rights and | |
| 13 | + * limitations under the License. | |
| 14 | + * | |
| 15 | + * All copies of the Covered Code must include on each user interface screen: | |
| 16 | + * (i) the "Powered by KnowledgeTree" logo and | |
| 17 | + * (ii) the KnowledgeTree copyright notice | |
| 18 | + * in the same form as they appear in the distribution. See the License for | |
| 19 | + * requirements. | |
| 20 | + * | |
| 21 | + * The Original Code is: KnowledgeTree Open Source | |
| 22 | + * | |
| 23 | + * The Initial Developer of the Original Code is The Jam Warehouse Software | |
| 24 | + * (Pty) Ltd, trading as KnowledgeTree. | |
| 25 | + * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright | |
| 26 | + * (C) 2007 The Jam Warehouse Software (Pty) Ltd; | |
| 27 | + * All Rights Reserved. | |
| 28 | + * Contributor( s): ______________________________________ | |
| 29 | + * | |
| 30 | + */ | |
| 31 | + | |
| 32 | +require_once('../config/dmsDefaults.php'); | |
| 33 | +require_once(KT_LIB_DIR . '/database/dbutil.inc'); | |
| 34 | + | |
| 35 | +// Set the time limit to 0 to prevent the script timing out | |
| 36 | +set_time_limit(0); | |
| 37 | + | |
| 38 | + | |
| 39 | +/* ** Set up functions ** */ | |
| 40 | + | |
| 41 | +// Calculate the next run time based on the frequency of iteration and the given time | |
| 42 | +function calculateRunTime($sFreq, $iTime) { | |
| 43 | + | |
| 44 | + switch($sFreq){ | |
| 45 | + case 'monthly': | |
| 46 | + $iDays = date('t'); | |
| 47 | + $iDiff = (60*60)*24*$iDays; | |
| 48 | + break; | |
| 49 | + case 'weekly': | |
| 50 | + $iDiff = (60*60)*24*7; | |
| 51 | + break; | |
| 52 | + case 'daily': | |
| 53 | + $iDiff = (60*60)*24; | |
| 54 | + break; | |
| 55 | + case 'hourly': | |
| 56 | + $iDiff = (60*60); | |
| 57 | + break; | |
| 58 | + case 'half_hourly': | |
| 59 | + $iDiff = (60*30); | |
| 60 | + break; | |
| 61 | + case 'quarter_hourly': | |
| 62 | + $iDiff = (60*15); | |
| 63 | + break; | |
| 64 | + case '10mins': | |
| 65 | + $iDiff = (60*10); | |
| 66 | + break; | |
| 67 | + case '5mins': | |
| 68 | + $iDiff = (60*5); | |
| 69 | + break; | |
| 70 | + case 'once': | |
| 71 | + $iDiff = 0; | |
| 72 | + break; | |
| 73 | + } | |
| 74 | + $iNextTime = $iTime + $iDiff; | |
| 75 | + return $iNextTime; | |
| 76 | +} | |
| 77 | + | |
| 78 | +// Update the task information in the database | |
| 79 | +function updateTask($sTable, $aFieldValues, $iId) { | |
| 80 | + DBUtil::autoUpdate($sTable, $aFieldValues, $iId); | |
| 81 | +} | |
| 82 | + | |
| 83 | +// Get the list of tasks due to be run from the database | |
| 84 | +function getTaskList($sTable) { | |
| 85 | + $now = time(); | |
| 86 | + $query = "SELECT * FROM {$sTable} | |
| 87 | + WHERE is_complete = 0 AND run_time < {$now}"; | |
| 88 | + | |
| 89 | + $result = DBUtil::getResultArray($query); | |
| 90 | + | |
| 91 | + if (PEAR::isError($result)){ | |
| 92 | + exit(); | |
| 93 | + } | |
| 94 | + return $result; | |
| 95 | +} | |
| 96 | + | |
| 97 | + | |
| 98 | +/* ** Scheduler script ** */ | |
| 99 | + | |
| 100 | +$sTable = 'scheduler_tasks'; | |
| 101 | + | |
| 102 | +// Get task list | |
| 103 | +$aList = getTaskList($sTable); | |
| 104 | + | |
| 105 | +// Loop through tasks and run | |
| 106 | +if(!empty($aList)){ | |
| 107 | + foreach($aList as $item){ | |
| 108 | + $aUpdate = array(); | |
| 109 | + $iEnd = 0; $iStart = 0; $iDuration = 0; | |
| 110 | + $sFreq = ''; $sParameters = ''; | |
| 111 | + | |
| 112 | + // Set up start variables | |
| 113 | + $sTask = $item['task']; | |
| 114 | + $sTaskUrl = $item['script_url']; | |
| 115 | + $iDuration = $item['run_duration']; | |
| 116 | + $sFreq = $item['frequency']; | |
| 117 | + $sParameters = $item['script_params']; | |
| 118 | + | |
| 119 | + $iTime = time(); | |
| 120 | + $iStart = explode(' ', microtime()); | |
| 121 | + | |
| 122 | + // Set up parameters for use by the script | |
| 123 | + $aParams = explode('|', $sParameters); | |
| 124 | + | |
| 125 | + foreach($aParams as $param){ | |
| 126 | + $aParam = explode('=', $param); | |
| 127 | + if(!empty($aParam)){ | |
| 128 | + $$aParam[0] = $aParam[1]; | |
| 129 | + } | |
| 130 | + } | |
| 131 | + | |
| 132 | + // Run the script | |
| 133 | + include(KT_DIR . $sTaskUrl); | |
| 134 | + | |
| 135 | + // On completion - reset run time | |
| 136 | + $iEnd = explode(' ', microtime()); | |
| 137 | + $iDuration = ($iEnd[1] + $iEnd[0]) - ($iStart[1] + $iStart[0]); | |
| 138 | + $iDuration = round($iDuration, 3); | |
| 139 | + | |
| 140 | + if($sFreq == 'once' || empty($sFreq)){ | |
| 141 | + // Set is_complete to true | |
| 142 | + $aUpdate['is_complete'] = '1'; | |
| 143 | + }else{ | |
| 144 | + $iNextTime = calculateRunTime($sFreq, $iTime); | |
| 145 | + $aUpdate['run_time'] = $iNextTime; | |
| 146 | + } | |
| 147 | + $aUpdate['previous_run_time'] = $iTime; | |
| 148 | + $aUpdate['run_duration'] = $iDuration; | |
| 149 | + | |
| 150 | + updateTask($sTable, $aUpdate, $item['id']); | |
| 151 | + | |
| 152 | + // clear parameters | |
| 153 | + if(!empty($aParams)){ | |
| 154 | + foreach($aParams as $param){ | |
| 155 | + $aParam = explode('=', $param); | |
| 156 | + $$aParam[0] = ''; | |
| 157 | + } | |
| 158 | + $aParam = array(); | |
| 159 | + $aParams = array(); | |
| 160 | + } | |
| 161 | + } | |
| 162 | +} | |
| 163 | +?> | |
| 0 | 164 | \ No newline at end of file | ... | ... |