JobsMgr.php
18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/**
* @class JobsMgr
* @brief Manage all batch jobs
* @version $Id: JobsMgr.php 2757 2015-02-19 12:15:41Z elena $
*
*/
class JobsMgr {
protected $jobXml, $jobXmlName;
protected $bkgRootNode = array('condition' => 'bkgSearch-treeRootNode',
'request' => 'bkgPlot-treeRootNode',
'download' => 'bkgDown-treeRootNode');
protected $resRootNode = array('condition' => 'resSearch-treeRootNode',
'request' => 'resPlot-treeRootNode',
'download' => 'resDown-treeRootNode');
protected $jobPrefix = array( 'condition' => 'bkgrd_cond_',
'request' => 'bkgrd_req_',
'download' => 'bkgrd_down_');
public $success = true;
function __construct() {
$this->jobXmlName = USERJOBDIR."jobs.xml";
$this->jobXml = new DomDocument("1.0");
if (!file_exists($this->jobXmlName)){
$status = $this->generateXml();
if (!$status) {
error_log('Cannot create jobs.xml: Fatal Error',1,email);
$this->success = false;
}
}
else {
$status = $this->jobXml->load($this->jobXmlName);
if (!$status) {
$status = $this->generateXml();
$msg = $status ? 'Cannot load jobs.xml. New jobs.xml was created' :
'Cannot load jobs.xml. Cannot create jobs.xml: Fatal Error';
error_log($msg,1,email);
if (!$status) $this->success = false;
}
}
}
/*
* Create jobs.xml if it doesn't exist or in case of loading problems
*/
private function generateXml() {
$rootElement = $this->jobXml->createElement('jobs');
$jobsInProgress = $this->jobXml->createElement('jobsInProgress');
foreach ($this->bkgRootNode as $key => $value) {
$element = $this->jobXml->createElement("$key");
$element->setAttribute('xml:id',"$value");
$jobsInProgress->appendChild($element);
}
$jobsFinished = $this->jobXml->createElement('jobsFinished');
foreach ($this->resRootNode as $key => $value) {
$element = $this->jobXml->createElement("$key");
$element->setAttribute('xml:id',"$value");
$jobsFinished->appendChild($element);
}
$rootElement->appendChild($jobsInProgress);
$rootElement->appendChild($jobsFinished);
$this->jobXml->appendChild($rootElement);
$status = $this->jobXml->save($this->jobXmlName);
return $status;
}
//TODO procedure to estimate STATUS
protected function getStatus($pid) {
return '20';
}
/*
*
*/
protected function updateJobStatus($pid) {
if ($this->isFinished($pid)) return 'done';
$status = $this -> getStatus($pid);
return $status;
}
/*
*
*/
protected function saveResource($obj, $name) {
$file = fopen(USERJOBDIR.$name, 'w');
//TODO change id and name to job ones?
fwrite($file, json_encode($obj));
fclose($file);
}
// $obj => XML tag
protected function deleteResource($obj) {
// Delete request
$request_name = $obj->getAttribute('xml:id');
if (file_exists(USERJOBDIR.$request_name)) unlink(USERJOBDIR.$request_name);
// Delete result Time Table
if ($obj->getAttribute('jobType') == 'condition') {
foreach (glob(USERTTDIR.'*'.$obj->getAttribute('name').'*.xml') as $filename)
unlink($filename);
}
// Delete result PLOT : PDF and PS
if ($obj->getAttribute('jobType') == 'request' || $obj->getAttribute('jobType') == 'download') {
$resultDir = USERWORKINGDIR.$obj->getAttribute('rawname').'_/';
if (is_dir($resultDir)) {
foreach (glob($resultDir.'*') as $filename)
unlink($filename);
rmdir($resultDir);
}
}
}
// $obj => XML tag
protected function updateResultName($obj) {
switch ($obj->getAttribute('jobType'))
{
case 'condition':
//TODO temporary dir?
$old_tt = USERTTDIR.$obj->getAttribute('rawname').'.xml';
$new_tt = USERTTDIR.$obj->getAttribute('name').'.xml';
if (file_exists($old_tt)) rename($old_tt, $new_tt);
$old_tt = USERTTDIR.'Gaps_'.$obj->getAttribute('rawname').'.xml';
$new_tt = USERTTDIR.'Gaps_'.$obj->getAttribute('name').'.xml';
if (file_exists($old_tt)) rename($old_tt, $new_tt);
break;
case 'request' :
$resultDir = USERWORKINGDIR.$obj->getAttribute('rawname').'_/';
$reqFile = USERJOBDIR.$obj->getAttribute('xml:id');
if (file_exists($reqFile)) {
$object = json_decode(file_get_contents($reqFile));
$format = strtolower($object->format);
$newName = $obj->getAttribute('name');
switch ($format) {
case 'ps':
if (file_exists($resultDir.'idl.ps')){
rename($resultDir.'idl.ps',$resultDir.$newName.'.ps');
exec('gzip '.$resultDir.$newName.'.ps');
}
//PS for time table
else {
$filenames = glob($resultDir."*.ps.gz");
if ($filenames && count($filenames) == 1) {
rename($filenames[0],$resultDir.$newName.'.ps.gz');
}
}
break;
case 'pdf':
if (file_exists($resultDir.'idl.ps')){
exec('ps2pdf '.$resultDir.'idl.ps '.$resultDir.'idl.pdf');
unlink($resultDir.'idl.ps');
rename($resultDir.'idl.pdf',$resultDir.$newName.'.pdf');
}
// PDF for time tables
if (file_exists($resultDir.'idl.pdf')){
rename($resultDir.'idl.pdf',$resultDir.$newName.'.pdf');
}
break;
case 'png' :
rename($resultDir.$obj->getAttribute('rawname').'.png', $resultDir.$newName.'.png');
break;
default :
}
}
break;
case 'download' :
$rawId = $obj->getAttribute('rawname');
$name = $obj->getAttribute('name');
$reqMgr = new RequestMgr('download');
$resultDir = USERWORKINGDIR.$rawId.'_/';
$res = $reqMgr->initResDir($rawId);
if (!$res['success'])
return $res;
$resultDir = $res['resdir'];
//get options
$opts = $reqMgr->getPrintOptions($rawId);
// Start PostProcessing - lock dir from Client Requests
if (!file_exists($resultDir."/POSTPROCESSING"))
touch($resultDir."/POSTPROCESSING");
// Is still PostProcessing - return
else return true;
$reqMgr->postProcessing($rawId,$name);
if (file_exists($resultDir."/POSTPROCESSING"))
unlink($resultDir."/POSTPROCESSING");
break;
default :
}
return false;
}
/*****************************************************************
* PUBLIC FUNCTIONS
*****************************************************************/
public function isFinished($PID){
exec("ps $PID", $ProcessState);
if (count($ProcessState) >= 2 ) return false;
return true;
}
/*
*
*/
public function addJob($obj, $pid, $tmpname, $newname) {
/* [id] => cond_2
[name] => longtest
[sampling] => 60
[gap] => 5
[expression] => imf(0) > 0
[startDate] => 2000-09-27T13:50:51
[durationDay] => 0665
[durationHour] => 12
[durationMin] => 00
[durationSec] => 00
[leaf] => 1
[nodeType] => condition
*/
$newJob = $this->jobXml->createElement('job');
$newJob->setAttribute('jobType', $obj->nodeType);
$newJob->setAttribute('pid', $pid);
$newJob->setAttribute('start', date('d-m-Y H:i:s'));
if ($pid) {
$newJob->setAttribute('status', $this->getStatus($pid));
$newJob->setAttribute('name', 'job_'.$pid);
}
else {
$newJob->setAttribute('status', 'done');
$newJob->setAttribute('name', $newname);
}
$newJob->setAttribute('rawname', $tmpname);
switch ($obj->nodeType)
{
case 'condition':
$newJob->setAttribute('info', $obj->expression);
break;
case 'download' :
$info = '';
foreach ($obj->list as $param)
{
if ($obj->downloadSrc == '2') //fits image
$info = $info.' '.$param->url;
else
$info = $info.' '.$param; //data
}
$newJob->setAttribute('info', $info);
break;
case 'request' :
$info = '';
for ($i=0; $i < count($obj->children); $i++) {
for ($j=0; $j < count($obj->children[$i]->children); $j++) {
$info = $info.' '.$obj->children[$i]->children[$j]->name;
}
}
$newJob->setAttribute('info', $info);
break;
default:
}
if ($pid) {
$newJob->setAttribute('xml:id',$this->jobPrefix[$obj->nodeType].$pid);
$rootJobNode = $this->jobXml->getElementById($this->bkgRootNode[$obj->nodeType]);
if (!$rootJobNode) {
$key = $obj->nodeType;
$rootJobNode = $this->jobXml->createElement("$key");
$rootJobNode->setAttribute('xml:id', $this->bkgRootNode[$obj->nodeType]);
$jobsInProgress = $this->jobXml->getElementsByTagName('jobsInProgress')->item(0);
$jobsInProgress->appendChild($rootJobNode);
}
}
else {
$newJob->setAttribute('xml:id', $tmpname);
$rootJobNode = $this->jobXml->getElementById($this->resRootNode[$obj->nodeType]);
if (!$rootJobNode) {
$key = $obj->nodeType;
$rootJobNode = $this->jobXml->createElement("$key");
$rootJobNode->setAttribute('xml:id', $this->resRootNode[$obj->nodeType]);
$jobsInProgress = $this->jobXml->getElementsByTagName('jobsFinished')->item(0);
$jobsInProgress->appendChild($rootJobNode);
}
}
//TODO if status 'error'...
$rootJobNode->appendChild($newJob);
$ok = $this->jobXml->save($this->jobXmlName);
if ($pid)
$this->saveResource($obj, $newJob->getAttribute('xml:id'));
return $newJob->getAttribute('xml:id');
}
/*
* object : id, leaf, nodeType
*/
public function deleteObject($obj) {
$job = $this->jobXml->getElementById($obj->id);
if (!$job) return array('id' => $obj->id);
$pid = $job->getAttribute('pid');
// Check if it is temporary result
if ($pid == '0') {
$resultDir = USERWORKINGDIR.$obj->id.'_';
if (is_dir($resultDir)) {
foreach (glob($resultDir.'/*') as $filename)
unlink($filename);
rmdir($resultDir);
}
//TT temporary
foreach (glob(USERTTDIR.'*'.$obj->id.'*.xml') as $filename)
unlink($filename);
$job->parentNode->removeChild($job);
$this->jobXml->save($this->jobXmlName);
return array('id' => $obj->id);
}
if (!$this->isFinished($pid)) {
$cmd = 'kill -9 '.$pid;
exec($cmd);
}
$this->deleteResource($job);
$job->parentNode->removeChild($job);
$this->jobXml->save($this->jobXmlName);
return array('id' => $obj->id);
}
/*
*
*/
public function getObject($id) {
$job = $this->jobXml->getElementById($id);
$format = 'unknown';
$compression = 'unknown';
if($job) {
$rawname = $job->getAttribute('rawname');
if ($job->getAttribute('jobType') == 'request') {
if (file_exists(USERJOBDIR.$id)) {
$obj = json_decode(file_get_contents(USERJOBDIR.$id));
$format = strtolower($obj->format);
};
}
if ($job->getAttribute('jobType') == 'download') {
if (file_exists(USERJOBDIR.$id)) {
$obj = json_decode(file_get_contents(USERJOBDIR.$id));
$compression = strtolower($obj->compression);
};
}
}
return array('id' => $id, 'name' => $rawname, 'format' => $format, 'compression' => $compression);
}
/*
* Real Update
*/
public function getCurrentJobsStatus() {
$jobs = $this->jobXml->getElementsByTagName('job');
foreach ($jobs as $job) {
$pid = $job->getAttribute('pid');
$status = $job->getAttribute('status');
// Already in Result => do not process
if ($status == 'error' || $status == 'done') {
$stop = $job->getAttribute('stop');
$jobType = $job->getAttribute('jobType');
$status = 'old';
}
else {
$status = $this -> updateJobStatus($pid);
$stop = 'unknown';
$job->setAttribute('status',$status);
$jobType = $job->getAttribute('jobType');
if ($status == 'error' || $status == 'done') {
$stop = date('d-m-Y H:i:s');
$job->setAttribute('stop', $stop);
// new array !!! otherwise $jobs array decreased
if ($jobType != null) {
$jobsToMove[] = $job;
}
if ($status == 'done') {
// Could take long time - special truck to avoid interference of TaskMgr requests
// check if ResDir is locked
$postProccesing = $this->updateResultName($job);
if ($postProccesing) {
// is already PostProcessing -> return something not-used
return array('id' => 'postprocessing');
}
}
}
}
$allJobs[] = array('name' => $job->getAttribute('name'),
'id' => $job->getAttribute('xml:id'),
'nodeType' => 'bkgWorks', 'leaf' => true,
'pid' => $pid, 'status' => $status,
'start' => $job->getAttribute('start'),
'stop' => $stop, 'jobType' => $jobType);
}
foreach ($jobsToMove as $job)
$this->jobXml->getElementById($this->resRootNode[$job->getAttribute('jobType')])->appendChild($job);
$this->jobXml->save($this->jobXmlName);
return $allJobs;
}
/*
* Delete all temporary products
*/
public function deleteTmp()
{
// Clean up temporary directory
if (is_dir(USERTEMPDIR))
foreach ( glob(USERTEMPDIR.'*') as $filename ) unlink($filename);
// Clean up immediate jobs
$xp = new domxpath($this->jobXml);
$tmps = $xp->query("//job[@pid='0']");
if ($tmps->length == 0) return;
foreach ($tmps as $tmp) {
$this->deleteResource($tmp);
$tmp->parentNode->removeChild($tmp);
}
$this->jobXml->save($this->jobXmlName);
return;
}
}
?>