Commit 65411235b9d48ae3822a60818761c0d7a5ec5c1e

Authored by kevin_fourie
1 parent 441e91e3

Merged in from DEV trunk...

KTS-2525
"Create windows service to wrap around scheduler"
Updated. Fixed issues executing cross platform.

Committed By: Kevin Fourie
Reviewed By: Conrad Vermeulen

KTS-1554
"Implement a Scheduler"
Updated. Made script work cross platform

Committed by: Conrad Vermeulen
Reviewed by: Kevin Fourie

KTS-2571
"Upgrade wizard is not calling ini upgrade function"
Fixed. Added backup before writing config.ini

Committed By: Conrad Vermeulen
Reviewed By: Kevin Fourie

KTS-1554
"Implement a Scheduler"
Updated. add scheduled tasks to clean db

Committed by: Conrad Vermeulen
Reviewed by: Kevin Fourie

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/STABLE/trunk@7513 c91229c3-7414-0410-bfa2-8a42b809f60b
bin/scheduler.php
... ... @@ -5,36 +5,36 @@
5 5 * KnowledgeTree Open Source Edition
6 6 * Document Management Made Simple
7 7 * Copyright (C) 2004 - 2007 The Jam Warehouse Software (Pty) Limited
8   - *
  8 + *
9 9 * This program is free software; you can redistribute it and/or modify it under
10 10 * the terms of the GNU General Public License version 3 as published by the
11 11 * Free Software Foundation.
12   - *
  12 + *
13 13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 16 * details.
17   - *
  17 + *
18 18 * You should have received a copy of the GNU General Public License
19 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20   - *
  20 + *
21 21 * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place,
22 22 * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com.
23   - *
  23 + *
24 24 * The interactive user interfaces in modified source and object code versions
25 25 * of this program must display Appropriate Legal Notices, as required under
26 26 * Section 5 of the GNU General Public License version 3.
27   - *
  27 + *
28 28 * In accordance with Section 7(b) of the GNU General Public License version 3,
29 29 * these Appropriate Legal Notices must retain the display of the "Powered by
30   - * KnowledgeTree" logo and retain the original copyright notice. If the display of the
  30 + * KnowledgeTree" logo and retain the original copyright notice. If the display of the
31 31 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
32   - * must display the words "Powered by KnowledgeTree" and retain the original
33   - * copyright notice.
  32 + * must display the words "Powered by KnowledgeTree" and retain the original
  33 + * copyright notice.
34 34 * Contributor( s): ______________________________________
35 35 *
36 36 */
37   -
  37 +
38 38 require_once('../config/dmsDefaults.php');
39 39 require_once(KT_LIB_DIR . '/database/dbutil.inc');
40 40  
... ... @@ -73,6 +73,12 @@ function calculateRunTime($sFreq, $iTime) {
73 73 case '5mins':
74 74 $iDiff = (60*5);
75 75 break;
  76 + case '1min':
  77 + $iDiff = 60;
  78 + break;
  79 + case '30secs':
  80 + $iDiff = 30;
  81 + break;
76 82 case 'once':
77 83 $iDiff = 0;
78 84 break;
... ... @@ -89,11 +95,11 @@ function updateTask($sTable, $aFieldValues, $iId) {
89 95 // Get the list of tasks due to be run from the database
90 96 function getTaskList($sTable) {
91 97 $now = date('Y-m-d H:i:s'); //time();
92   - $query = "SELECT * FROM {$sTable}
  98 + $query = "SELECT * FROM {$sTable}
93 99 WHERE is_complete = 0 AND run_time < '{$now}'";
94   -
  100 +
95 101 $result = DBUtil::getResultArray($query);
96   -
  102 +
97 103 if (PEAR::isError($result)){
98 104 exit();
99 105 }
... ... @@ -108,21 +114,23 @@ $sTable = &#39;scheduler_tasks&#39;;
108 114 // Get task list
109 115 $aList = getTaskList($sTable);
110 116  
  117 +global $default;
  118 +
111 119 // Loop through tasks and run
112 120 if(!empty($aList)){
113 121 foreach($aList as $item){
114 122 $aUpdate = array();
115 123 $iEnd = 0; $iStart = 0; $iDuration = 0;
116   - $sFreq = ''; $sParameters = '';
  124 + $sFreq = ''; $sParameters = '';
117 125 $retval = TRUE;
118   -
  126 +
119 127 // Set up start variables
120 128 $sTask = $item['task'];
121 129 $sTaskUrl = $item['script_url'];
122 130 $iDuration = $item['run_duration'];
123 131 $sFreq = $item['frequency'];
124 132 $sParameters = $item['script_params'];
125   -
  133 +
126 134 // Check if script is windows or *nix compatible
127 135 $extArr = explode('.', $sTaskUrl);
128 136 $ext = array_pop($extArr);
... ... @@ -162,19 +170,39 @@ if(!empty($aList)){
162 170 break;
163 171 }
164 172 }
165   -
  173 +
166 174 $iTime = time();
167 175 $iStart = explode(' ', microtime());
168   -
  176 +
169 177 // Run the script
170   - $file = KT_DIR . escapeshellcmd($sTaskUrl);
171   - system("{$file} {$sParameters} >> /dev/null", $retval);
172   -
  178 + $file =escapeshellcmd($sTaskUrl);
  179 +
  180 + $cmd = "\"$file\" {$sParameters}";
  181 +
  182 + if (OS_WINDOWS)
  183 + {
  184 + $cmd = str_replace( '/','\\',$cmd);
  185 + $res = `"$cmd" 2>&1`;
  186 + }
  187 + else
  188 + {
  189 + $res = shell_exec($cmd." 2>&1");
  190 + }
  191 +
  192 + if (!empty($res))
  193 + {
  194 + $default->log->info("Scheduler - Task: $sTask");
  195 + $default->log->info("Scheduler - Command: $cmd");
  196 + $default->log->info("Scheduler - Output: $res");
  197 + $default->log->info("Scheduler - Background tasks should not produce output. Please review why this is producing output.");
  198 + }
  199 +
  200 +
173 201 // On completion - reset run time
174 202 $iEnd = explode(' ', microtime());
175 203 $iDuration = ($iEnd[1] + $iEnd[0]) - ($iStart[1] + $iStart[0]);
176 204 $iDuration = round($iDuration, 3);
177   -
  205 +
178 206 if(($sFreq == 'once' || empty($sFreq)) && $retval !== FALSE){
179 207 // Set is_complete to true
180 208 $aUpdate['is_complete'] = '1';
... ... @@ -184,9 +212,9 @@ if(!empty($aList)){
184 212 }
185 213 $aUpdate['previous_run_time'] = date('Y-m-d H:i:s', $iTime);
186 214 $aUpdate['run_duration'] = $iDuration;
187   -
  215 +
188 216 updateTask($sTable, $aUpdate, $item['id']);
189   -
  217 +
190 218 // clear parameters
191 219 if(!empty($aParams)){
192 220 foreach($aParams as $param){
... ...
bin/win32/schedulerService.php
... ... @@ -6,6 +6,20 @@ $myservicename = &#39;ktscheduler&#39;;
6 6 if (!win32_start_service_ctrl_dispatcher($myservicename)) die('Could not connect to service :'.$myservicename);
7 7 win32_set_service_status(WIN32_SERVICE_RUNNING);
8 8  
  9 +require_once('../../config/dmsDefaults.php');
  10 +
  11 +global $default;
  12 +
  13 +$config = KTConfig::getSingleton();
  14 +$schedulerInterval = $config->get('KnowledgeTree/schedulerInterval',10); // interval in seconds
  15 +$phpPath = $config->get('externalBinary/php','php');
  16 +
  17 +if (!is_file($phpPath))
  18 +{
  19 + $default->log->error("Scheduler: php not found: $phpPath");
  20 + exit;
  21 +}
  22 +
9 23 // Main Scheduler Service Loop
10 24 while (1) {
11 25  
... ... @@ -23,15 +37,20 @@ while (1) {
23 37  
24 38 // Setup php binary path
25 39 $phpPath = realpath('../../php/php.exe');
26   - if (!is_file($phpPath))
27   - {
28   - die('Cannot find php.exe');
29   - }
30   -
  40 +
  41 +
31 42 // Run the scheduler script
32   - system("$phpPath scheduler.php");
33 43  
34   - sleep(10); // Run every 10 seconds
  44 + $cmd = "\"$phpPath\" scheduler.php";
  45 +
  46 + $cmd = str_replace( '/','\\',$cmd);
  47 + $res = `"$cmd" 2>&1`;
  48 + if (!empty($res))
  49 + {
  50 + $default->log->error('Scheduler: unexpected output - ' .$res);
  51 + }
  52 +
  53 + sleep($schedulerInterval);
35 54  
36 55 }
37 56 win32_set_service_status(WIN32_SERVICE_STOPPED);
... ...
lib/config/config.inc.php
... ... @@ -5,32 +5,32 @@
5 5 * KnowledgeTree Open Source Edition
6 6 * Document Management Made Simple
7 7 * Copyright (C) 2004 - 2007 The Jam Warehouse Software (Pty) Limited
8   - *
  8 + *
9 9 * This program is free software; you can redistribute it and/or modify it under
10 10 * the terms of the GNU General Public License version 3 as published by the
11 11 * Free Software Foundation.
12   - *
  12 + *
13 13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 16 * details.
17   - *
  17 + *
18 18 * You should have received a copy of the GNU General Public License
19 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20   - *
  20 + *
21 21 * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place,
22 22 * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com.
23   - *
  23 + *
24 24 * The interactive user interfaces in modified source and object code versions
25 25 * of this program must display Appropriate Legal Notices, as required under
26 26 * Section 5 of the GNU General Public License version 3.
27   - *
  27 + *
28 28 * In accordance with Section 7(b) of the GNU General Public License version 3,
29 29 * these Appropriate Legal Notices must retain the display of the "Powered by
30   - * KnowledgeTree" logo and retain the original copyright notice. If the display of the
  30 + * KnowledgeTree" logo and retain the original copyright notice. If the display of the
31 31 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
32   - * must display the words "Powered by KnowledgeTree" and retain the original
33   - * copyright notice.
  32 + * must display the words "Powered by KnowledgeTree" and retain the original
  33 + * copyright notice.
34 34 * Contributor( s): ______________________________________
35 35 *
36 36 */
... ... @@ -150,6 +150,25 @@ class KTConfig {
150 150 return $oDefault;
151 151 }
152 152  
  153 + /**
  154 + * Return the location of the config.ini
  155 + *
  156 + * @return string
  157 + */
  158 + static function getConfigFilename()
  159 + {
  160 + $configPath = file_get_contents(KT_DIR . '/config/config-path');
  161 +
  162 + if (is_file($configPath))
  163 + {
  164 + return $configPath;
  165 + }
  166 + else
  167 + {
  168 + return KT_DIR . '/' . $configPath;
  169 + }
  170 + }
  171 +
153 172 static function &getSingleton() {
154 173 static $singleton = null;
155 174  
... ...
lib/upgrades/Ini.inc.php
... ... @@ -5,32 +5,32 @@
5 5 * KnowledgeTree Open Source Edition
6 6 * Document Management Made Simple
7 7 * Copyright (C) 2004 - 2007 The Jam Warehouse Software (Pty) Limited
8   - *
  8 + *
9 9 * This program is free software; you can redistribute it and/or modify it under
10 10 * the terms of the GNU General Public License version 3 as published by the
11 11 * Free Software Foundation.
12   - *
  12 + *
13 13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 16 * details.
17   - *
  17 + *
18 18 * You should have received a copy of the GNU General Public License
19 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20   - *
  20 + *
21 21 * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place,
22 22 * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com.
23   - *
  23 + *
24 24 * The interactive user interfaces in modified source and object code versions
25 25 * of this program must display Appropriate Legal Notices, as required under
26 26 * Section 5 of the GNU General Public License version 3.
27   - *
  27 + *
28 28 * In accordance with Section 7(b) of the GNU General Public License version 3,
29 29 * these Appropriate Legal Notices must retain the display of the "Powered by
30   - * KnowledgeTree" logo and retain the original copyright notice. If the display of the
  30 + * KnowledgeTree" logo and retain the original copyright notice. If the display of the
31 31 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
32   - * must display the words "Powered by KnowledgeTree" and retain the original
33   - * copyright notice.
  32 + * must display the words "Powered by KnowledgeTree" and retain the original
  33 + * copyright notice.
34 34 * Contributor( s): ______________________________________
35 35 *
36 36 */
... ... @@ -48,7 +48,27 @@ class Ini {
48 48  
49 49 function Ini($iniFile = '../../config.ini') {
50 50 $this->iniFile = $iniFile;
51   - $this->read($iniFile);
  51 + $this->backupIni($iniFile);
  52 + $this->read($iniFile);
  53 + }
  54 +
  55 + /**
  56 + * Create a backup with the date as an extension in the same location as the original config.ini
  57 + *
  58 + * @param string $iniFile
  59 + * @return boolean
  60 + */
  61 + function backupIni($iniFile)
  62 + {
  63 + $content = file_get_contents($iniFile);
  64 + if ($content === false)
  65 + {
  66 + return false;
  67 + }
  68 + $date = date('YmdHis');
  69 +
  70 + $backupFile = $iniFile . '.' .$date;
  71 + return file_put_contents($backupFile, $content) !== false;
52 72 }
53 73  
54 74 function read($iniFile) {
... ... @@ -85,7 +105,7 @@ class Ini {
85 105 $key = trim(substr($iniLine, 0, $equalsPos));
86 106 $value = trim(substr($iniLine, $equalsPos+1));
87 107 if (substr($value, 1, 1) == '"' && substr( $value, -1, 1) == '"') {
88   - $value = substr($value, 1, -1);
  108 + $value = substr($value, 1, -1);
89 109 }
90 110 $this->cleanArray[$section][$key] = stripcslashes($value);
91 111 } else {
... ... @@ -104,21 +124,21 @@ class Ini {
104 124 $fileHandle = fopen($iniFile, 'wb');
105 125 foreach ($this->cleanArray as $section => $items) {
106 126 if (substr($section, 0, strlen('_blankline_')) === '_blankline_' ) {
107   - fwrite ($fileHandle, "\r\n");
  127 + fwrite ($fileHandle, "\r\n");
108 128 continue;
109 129 }
110 130 if (substr($section, 0, strlen('_comment_')) === '_comment_' ) {
111   - fwrite ($fileHandle, "$items\r\n");
  131 + fwrite ($fileHandle, "$items\r\n");
112 132 continue;
113 133 }
114 134 fwrite ($fileHandle, "[".$section."]\r\n");
115 135 foreach ($items as $key => $value) {
116 136 if (substr($key, 0, strlen('_blankline_')) === '_blankline_' ) {
117   - fwrite ($fileHandle, "\r\n");
  137 + fwrite ($fileHandle, "\r\n");
118 138 continue;
119 139 }
120 140 if (substr($key, 0, strlen('_comment_')) === '_comment_' ) {
121   - fwrite ($fileHandle, "$value\r\n");
  141 + fwrite ($fileHandle, "$value\r\n");
122 142 continue;
123 143 }
124 144  
... ... @@ -145,7 +165,7 @@ class Ini {
145 165 }
146 166 return false;
147 167 }
148   -
  168 +
149 169 function addItem($addSection, $addItem, $value, $itemComment = '', $sectionComment = '') {
150 170  
151 171 if($this->itemExists($addSection, $addItem)) return false;
... ...
lib/upgrades/UpgradeFunctions.inc.php
... ... @@ -5,32 +5,32 @@
5 5 * KnowledgeTree Open Source Edition
6 6 * Document Management Made Simple
7 7 * Copyright (C) 2004 - 2007 The Jam Warehouse Software (Pty) Limited
8   - *
  8 + *
9 9 * This program is free software; you can redistribute it and/or modify it under
10 10 * the terms of the GNU General Public License version 3 as published by the
11 11 * Free Software Foundation.
12   - *
  12 + *
13 13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 16 * details.
17   - *
  17 + *
18 18 * You should have received a copy of the GNU General Public License
19 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20   - *
  20 + *
21 21 * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place,
22 22 * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com.
23   - *
  23 + *
24 24 * The interactive user interfaces in modified source and object code versions
25 25 * of this program must display Appropriate Legal Notices, as required under
26 26 * Section 5 of the GNU General Public License version 3.
27   - *
  27 + *
28 28 * In accordance with Section 7(b) of the GNU General Public License version 3,
29 29 * these Appropriate Legal Notices must retain the display of the "Powered by
30   - * KnowledgeTree" logo and retain the original copyright notice. If the display of the
  30 + * KnowledgeTree" logo and retain the original copyright notice. If the display of the
31 31 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
32   - * must display the words "Powered by KnowledgeTree" and retain the original
33   - * copyright notice.
  32 + * must display the words "Powered by KnowledgeTree" and retain the original
  33 + * copyright notice.
34 34 * Contributor( s): ______________________________________
35 35 *
36 36 */
... ... @@ -916,9 +916,12 @@ class UpgradeFunctions {
916 916 // {{{ updateConfigFile35
917 917 function updateConfigFile35()
918 918 {
919   - if(file_exists('../../config.ini')) {
  919 + $configPath = KTConfig::getConfigFilename();
  920 + $configPath = str_replace(array("\n","\r"), array('',''), $configPath);
  921 +
  922 + if(file_exists($configPath)) {
920 923  
921   - $ini = new Ini();
  924 + $ini = new Ini($configPath);
922 925  
923 926 // Webservices Section
924 927 $ini->addItem('webservice', 'uploadDirectory', '${varDirectory}/uploads');
... ... @@ -1008,7 +1011,6 @@ class UpgradeFunctions {
1008 1011 $ini->addItem('DiskUsage', 'urgentThreshold', '5', "When free space in a mount point is less than this percentage,\r\n; the disk usage dashlet will highlight the mount in RED");
1009 1012  
1010 1013 $ini->write();
1011   -
1012 1014 }
1013 1015 }
1014 1016 // }}}
... ... @@ -1031,7 +1033,7 @@ class UpgradeFunctions {
1031 1033  
1032 1034 $oScheduler = new Scheduler('Indexing');
1033 1035 $oScheduler->setScriptPath(KT_DIR . '/bin/indexingTask.' . $ext);
1034   - $oScheduler->setFrequency('5mins');
  1036 + $oScheduler->setFrequency('1min');
1035 1037 $oScheduler->setFirstRunTime(date('Y-m-d H:i',mktime($hour, $min, 0, $mon, $day, $year)));
1036 1038 $oScheduler->registerTask();
1037 1039  
... ...
sql/mysql/install/data.sql
No preview for this file type