/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1 by Suren A. Chilingaryan
Initial import
1
<?php
2
/*
3
function signal_handler($signal) {
4
    profilerDisplay();
5
    exit;
6
}
7
declare(ticks = 1);
8
pcntl_signal(SIGINT, "signal_handler");
9
*/
10
224 by Suren A. Chilingaryan
Support parallel caching
11
$curdir = getcwd();
1 by Suren A. Chilingaryan
Initial import
12
13
if (preg_match("/(.*)cache.php$/", $_SERVER['SCRIPT_FILENAME'], $m)) @chdir($m[1]);
14
15
require("../adei.php");
24 by Suren A. Chilingaryan
Logger subsystem
16
#adeiLogOutput();
17
224 by Suren A. Chilingaryan
Support parallel caching
18
$verbose = 0;
465 by Suren A. Chilingaryan
Initial release of scheduling service
19
$archives = 0;
419 by Suren A. Chilingaryan
Support caching archived databases (we should do it in the manager script)
20
$list_flags = 0;
473 by Suren A. Chilingaryan
Caching scripts reworked
21
$execute = false;
22
$partype = false;
23
$multihost = false;
24
$user_req = new REQUEST();
224 by Suren A. Chilingaryan
Support parallel caching
25
$source_param = "";
26
27
$params = sizeof($_SERVER['argv']);
28
if ($params>1) {
29
    $pos = array_search("-verbose", $_SERVER['argv']);
30
    if ($pos) $verbose = 1;
473 by Suren A. Chilingaryan
Caching scripts reworked
31
    
32
    $pos = array_search("-execute", $_SERVER['argv']);
33
    if ($pos) $execute = true;
34
35
    $pos = array_search("-archives", $_SERVER['argv']);
36
    if ($pos) $archives = true;
37
    
38
    $pos = array_search("-multihost", $_SERVER['argv']);
39
    if ($pos) $multihost = true;
40
    
224 by Suren A. Chilingaryan
Support parallel caching
41
    $pos = array_search("-source", $_SERVER['argv']);
473 by Suren A. Chilingaryan
Caching scripts reworked
42
    if ($pos) {
43
	unset($_SERVER['argv'][$pos]);
224 by Suren A. Chilingaryan
Support parallel caching
44
45
	if (($pos + 1) < $params) {
46
	    $source_param = $_SERVER['argv'][$pos + 1];
47
	    $user_req = new REQUEST($source_param);
473 by Suren A. Chilingaryan
Caching scripts reworked
48
49
	    unset($_SERVER['argv'][$pos + 1]);
224 by Suren A. Chilingaryan
Support parallel caching
50
	} else {
51
	    throw new ADEIException(translate("The source is not specified"));
52
	}
53
    }
54
55
    $pos = array_search("-parallel", $_SERVER['argv']);
473 by Suren A. Chilingaryan
Caching scripts reworked
56
    if ($pos) {
224 by Suren A. Chilingaryan
Support parallel caching
57
	unset($_SERVER['argv'][$pos]);
473 by Suren A. Chilingaryan
Caching scripts reworked
58
224 by Suren A. Chilingaryan
Support parallel caching
59
	$partype = "sources";
60
	if ((($pos + 1) < $params)&&(substr($_SERVER['argv'][$pos + 1], 0, 1) != "-")) {
61
	    $partype = $_SERVER['argv'][$pos + 1];
62
	    unset($_SERVER['argv'][$pos + 1]);
63
	}
64
    }
65
} else $SETUP_MULTI_MODE = 0;
66
473 by Suren A. Chilingaryan
Caching scripts reworked
67
if ($archives) $list_flags = REQUEST::LIST_WILDCARDED;
224 by Suren A. Chilingaryan
Support parallel caching
68
69
$syslock = new LOCK("system-cache");
1 by Suren A. Chilingaryan
Initial import
70
71
function ROOTCache(&$cache, $rootdb, $app, $inq_app, $rootdb_name) {
72
    try {
73
74
	$desc = array(
75
	    0 => array("pipe", "r"),
76
	    1 => array("pipe", "w"),
77
	    2 => array("pipe", "w")
78
	);
79
80
	$cmd = proc_open($inq_app, $desc, $pipes);
81
	if ($cmd) {
82
	    if ($rootdb) {
83
	        fwrite($pipes[0], $rootdb['user'] . "\n");
84
		fwrite($pipes[0], $rootdb['password'] . "\n");
85
	    }
86
	    fclose($pipes[0]);
87
88
	    $start = stream_get_contents($pipes[1]);
89
	    fclose($pipes[1]);
90
	    
91
	    $errors = stream_get_contents($pipes[2]);
92
	    $ret = proc_close($cmd);
93
	    if ($ret) throw new ADEIException("Execution of csv2root is finished with error code $ret");
94
95
	} else throw new ADEIException("Execution of csv2root is failed");
96
	
97
	if ($start < 0) $start = 0;
98
99
	$ivl = $cache->CreateInterval();
100
	$ivl->Limit($start);
101
102
	$cmd = popen($app, "w");
103
	if ($rootdb) {
104
	    fwrite($cmd, $rootdb['user'] . "\n");
105
	    fwrite($cmd, $rootdb['password'] . "\n");
106
	}
19 by Suren A. Chilingaryan
Continious ROOT files generation is fixed
107
	$cache->ExportCSV(new STREAMOutput($cmd), $mask = NULL, $ivl);
1 by Suren A. Chilingaryan
Initial import
108
	if ($err = pclose($cmd)) throw new ADEIException("csv2root is finished with error: $err");
109
    } catch(ADEIException $e) {
24 by Suren A. Chilingaryan
Logger subsystem
110
	$e->logInfo(translate("Problem processing ROOT database \"%s\"", $rootdb_name), $cache);
111
	echo "Problem processing ROOT database \"" . $rootdb_name . "\": " .  $e->getInfo() . "\n";
1 by Suren A. Chilingaryan
Initial import
112
    }
113
}
114
115
function DoCache(&$req) {
224 by Suren A. Chilingaryan
Support parallel caching
116
    global $user_req;
117
    global $verbose;
1 by Suren A. Chilingaryan
Initial import
118
    global $ROOT_DB;
224 by Suren A. Chilingaryan
Support parallel caching
119
    global $syslock;
1 by Suren A. Chilingaryan
Initial import
120
121
    try {
122
	$reader = $req->CreateReader(REQUEST::READER_FORBID_CACHEREADER);
123
    } catch(ADEIException $e) {
17 by Suren A. Chilingaryan
Prevent caching code from complaining on disconnected sources
124
	if ($e->getCode() == ADEIException::DISCONNECTED) return;
24 by Suren A. Chilingaryan
Logger subsystem
125
126
	$e->logInfo(translate("READER constructor is failed"), $req);
127
        return $req->GetLocationString() . ", Error: " . $e->getInfo();
1 by Suren A. Chilingaryan
Initial import
128
    }
129
130
    try {
131
        $opts = &$req->GetOptions();
132
	$rootdb = $opts->Get('root_database');
133
35 by Suren A. Chilingaryan
Minor refactoring: REQUEST/READER/CACHE listing flags, REQUEST listing functions, eliminating FETCH_BOTH fetches in DATABASE, support for partial menu generation
134
	$list = $req->GetGroups($reader);
1 by Suren A. Chilingaryan
Initial import
135
    } catch(ADEIException $e) {
24 by Suren A. Chilingaryan
Logger subsystem
136
	$e->logInfo(translate("Problem processing options"), $reader);
137
        return $req->GetLocationString() . ", Error: " . $e->getInfo();
1 by Suren A. Chilingaryan
Initial import
138
    }
139
140
    if ($rootdb) {
141
	if (strchr($rootdb, "/")) {
142
	    $db_info = false;
143
	    $inq_app = adei_app("csv2root", "--file " . $rootdb . " --inquiry-latest-data", true);
144
	    $app = adei_app("csv2root", "--file " . $rootdb, true);
145
	} else {
146
	    try {
147
		$db_info = $ROOT_DB;
148
		$db_info['database'] = $rootdb;
149
		$database = new DATABASE($db_info);
150
	    } catch (ADEIException $e) {
151
		unset($db_info['database']);
152
		try {
153
		    $database = new DATABASE($ROOT_DB);
154
		    $database->CreateDatabase($rootdb);
155
		} catch(ADEIException $e) {
24 by Suren A. Chilingaryan
Logger subsystem
156
		    $e->logInfo(translate("Problem accessing ROOT database \"%s\"", $rootdb), $req);
157
		    echo translate("Problem accessing ROOT database \"%s\": %s\n", $rootdb, $e->getInfo());
1 by Suren A. Chilingaryan
Initial import
158
		    $rootdb = false;
159
		}
160
	    }
161
162
	    if ($rootdb) {
163
		$conprm = $db_info['driver'] . "://" . $db_info['host'] . ($db_info['port']?(":" . $db_info['port']):"") . "/" . $rootdb;
164
		$inq_app = adei_app("csv2root", "--db " . $conprm . " --inquiry-latest-data", true);
165
		$app = adei_app("csv2root", "--db " . $conprm, true);
166
	    }
167
	}
168
	if ($rootdb) {
169
	    if ((!$app)||(!$inq_app)) {
170
		echo("csv2root is not present");
171
		$rootdb = false;
172
	    } else {
35 by Suren A. Chilingaryan
Minor refactoring: REQUEST/READER/CACHE listing flags, REQUEST listing functions, eliminating FETCH_BOTH fetches in DATABASE, support for partial menu generation
173
	        $groups = $reader->GetGroupList(REQUEST::NEED_INFO);
1 by Suren A. Chilingaryan
Initial import
174
	    }
175
	}
176
    }
177
178
    foreach ($list as $gid => $greq) {
291 by Suren A. Chilingaryan
In the parallel caching mode, properly handle groups with spaces and zeroes in the gid
179
        if (isset($user_req->props['db_group'])&&($user_req->props['db_group'] != $gid)) continue;
473 by Suren A. Chilingaryan
Caching scripts reworked
180
	    
224 by Suren A. Chilingaryan
Support parallel caching
181
	if ($verbose) {
182
	    echo "{$greq->props['db_server']} -- {$greq->props['db_name']} -- $gid:";
183
	}
184
185
        $syslocked = false;
186
        $prelocked = false;
187
1 by Suren A. Chilingaryan
Initial import
188
	try {
224 by Suren A. Chilingaryan
Support parallel caching
189
	    
1 by Suren A. Chilingaryan
Initial import
190
	    $cache = $greq->CreateCacheUpdater($reader);
224 by Suren A. Chilingaryan
Support parallel caching
191
192
	    $syslock->Lock(LOCK::BLOCK);
193
	    $syslocked = true;
194
	    if (!$cache->PreLock()) {
195
		if ($verbose) echo " busy\n";
196
		continue;
197
	    }
198
	    $prelocked = true;
199
	    $syslocked = false;
200
	    $syslock->UnLock();
201
	    
202
	    $cache->Update($ivl=NULL, CACHE::DIM_AUTO, verbose?CACHE::VERBOSE:0);
203
	    
204
	    $prelocked = false;
205
	    $cache->UnLock();
53 by Suren A. Chilingaryan
fixups and some search hooks (incomplete)
206
	    
1 by Suren A. Chilingaryan
Initial import
207
	    if ($rootdb) {
19 by Suren A. Chilingaryan
Continious ROOT files generation is fixed
208
		$grname = " --group \"" . $groups[$gid]["name"] . "\"";
209
		ROOTCache($cache, $db_info, $app . $grname, $inq_app . $grname, $conprm?$conprm:$rootdb);
1 by Suren A. Chilingaryan
Initial import
210
	    }
35 by Suren A. Chilingaryan
Minor refactoring: REQUEST/READER/CACHE listing flags, REQUEST listing functions, eliminating FETCH_BOTH fetches in DATABASE, support for partial menu generation
211
	    unset($cache);
1 by Suren A. Chilingaryan
Initial import
212
	} catch(ADEIException $e) {
224 by Suren A. Chilingaryan
Support parallel caching
213
	    if ($prelocked) $cache->UnLock();
214
	    if ($syslocked) $syslock->UnLock();
215
	    
71 by Suren A. Chilingaryan
Fast data support in TESTReader
216
	    if ($e->getCode() !=  ADEIException::DISABLED) {
217
		$e->logInfo(translate("Failed to update CACHE"), $cache?$cache:$greq);
224 by Suren A. Chilingaryan
Support parallel caching
218
		if ($verbose) echo " failed\n";
71 by Suren A. Chilingaryan
Fast data support in TESTReader
219
		return $greq->GetLocationString() . ", Error: " . $e->getInfo();
220
	    }
1 by Suren A. Chilingaryan
Initial import
221
	}
224 by Suren A. Chilingaryan
Support parallel caching
222
	if ($verbose) echo " done\n";
1 by Suren A. Chilingaryan
Initial import
223
    }
224
    
225
    return 0;
226
}
227
473 by Suren A. Chilingaryan
Caching scripts reworked
228
229
if (($ADEI_CACHE_SCHEDULER)&&(!$execute)) {
230
    $flags = 0;
231
    if ($archives) {
232
        $mode = SCHEDULER::CACHE_ARCHIVE;
233
    } else {
234
        $mode = SCHEDULER::CACHE_CURRENT;
235
        if ($ADEI_CONTINUOUS_CACHING) $flags |= SCHEDULER::AUTO_RESTART;
236
    }
237
    $sched = new SCHEDULER($user_req, "cache", $mode, $flags);
238
    $sched->ScheduleRequest($user_req, $ADEI_CACHE_SCHEDULER);
239
} elseif ($partype) {
240
    switch($partype) {
241
     case "servers":
242
	$list = $user_req->GetServerList();
243
	break;
244
     case "sources":
245
	$list = $user_req->GetSourceList($list_flags);
246
	break;
247
     case "groups":
248
	$list = $user_req->GetGroupList($list_flags);
249
	break;
250
     default:
251
	throw new ADEIException(translate("Unsupported type of parallelization (%s)", $partype));
252
    }
253
	
254
    $params = implode(" ", $_SERVER['argv']);
255
    chdir($curdir);
256
257
    $procs = array();	
258
    foreach ($list as $item) {
259
	$source = "db_server={$item['db_server']}";
260
	if (isset($item['db_name'])) $source .= "&db_name={$item['db_name']}";
261
	if (isset($item['db_group'])) $source .= "&db_group={$item['db_group']}";
262
	$run = "php $params -source \"$source\"";
263
#	echo "===> $run\n";
264
	array_push($procs, popen($run, "r"));
265
    }
266
	
267
    foreach ($procs as $proc) {
268
	while (!feof($proc)) {
269
	    echo fgets($proc);
270
        }
271
	pclose($proc);
272
    }
465 by Suren A. Chilingaryan
Initial release of scheduling service
273
} else {
508 by Suren A. Chilingaryan
Fix checking if is off in system/cache.php
274
    if ($multihost === false) {
473 by Suren A. Chilingaryan
Caching scripts reworked
275
        if ($source_param) {
276
            $res = exec('ps xa | grep "cache.php" | grep -v "grep cache.php" | grep -v "sh -c" | grep -E "' . $source_param . '\s*([\'\\-\\\\\\"]|$)" | wc -l');
277
            if ($res > 1) {
278
	        if ($verbose) echo "Cache script is already running for source \"$source_param\"\n";
279
	        exit;
280
            }
281
        } else {
282
            $res = exec('ps xa | grep "cache.php" | grep -v "grep cache.php" | grep -v "sh -c" | grep -v "\-source" | wc -l');
283
            if ($res > 1) {
284
	        if ($verbose) echo "Cache script is already running\n";
285
	        exit;
286
            }
287
        }
288
    }
289
    
290
    $list = $user_req->GetSources($list_flags);
465 by Suren A. Chilingaryan
Initial release of scheduling service
291
    foreach ($list as $sreq) {
292
        if (isset($user_req->props['db_server'])&&($user_req->props['db_server'] != $sreq->props['db_server'])) continue;
293
        if (isset($user_req->props['db_name'])&&($user_req->props['db_name'] != $sreq->props['db_name'])) continue;
473 by Suren A. Chilingaryan
Caching scripts reworked
294
        
295
        if (isset($user_req->props['db_group'])) {
296
            $sreq = new GROUPRequest(array_merge($sreq->props, array("db_group" => $user_req->props['db_group'])));
297
        }
465 by Suren A. Chilingaryan
Initial release of scheduling service
298
299
        $err = DoCache($sreq);
300
        if ($err) echo "$err\n\n\n";
301
    }
1 by Suren A. Chilingaryan
Initial import
302
}
465 by Suren A. Chilingaryan
Initial release of scheduling service
303
1 by Suren A. Chilingaryan
Initial import
304
?>