source: trunk/cleanup.php@ 9

Last change on this file since 9 was 9, checked in by gegorbet, 10 years ago

fix to handle ~us3/bin on different hosts

File size: 23.9 KB
Line 
1<?php
2/*
3 * cleanup.php
4 *
5 * functions relating to copying results and cleaning up the gfac DB
6 *
7 */
8
9$us3bin = exec( "ls -d ~us3/bin" );
10include_once "$us3bin/listen-config.php";
11$me = 'cleanup.php';
12$email_address = '';
13$queuestatus = '';
14$jobtype = '';
15$db = '';
16$editXMLFilename = '';
17$status = '';
18
19function gfac_cleanup( $us3_db, $reqID, $gfac_link )
20{
21 global $dbhost;
22 global $user;
23 global $passwd;
24 global $db;
25 global $guser;
26 global $gpasswd;
27 global $gDB;
28 global $me;
29 global $work;
30 global $email_address;
31 global $queuestatus;
32 global $jobtype;
33 global $editXMLFilename;
34 global $submittime;
35 global $status;
36 global $stdout;
37 global $requestID;
38
39 $requestID = $reqID;
40 $db = $us3_db;
41 write_log( "$me: debug db=$db; requestID=$requestID" );
42
43 $us3_link = mysql_connect( $dbhost, $user, $passwd );
44
45 if ( ! $us3_link )
46 {
47 write_log( "$me: could not connect: $dbhost, $user, $passwd" );
48 mail_to_user( "fail", "Internal Error $requestID\nCould not connect to DB" );
49 return( -1 );
50 }
51
52 $result = mysql_select_db( $db, $us3_link );
53
54 if ( ! $result )
55 {
56 write_log( "$me: could not select DB $db" );
57 mail_to_user( "fail", "Internal Error $requestID\n$could not select DB $db" );
58 return( -1 );
59 }
60
61 // First get basic info for email messages
62 $query = "SELECT email, investigatorGUID, editXMLFilename FROM HPCAnalysisRequest " .
63 "WHERE HPCAnalysisRequestID=$requestID";
64 $result = mysql_query( $query, $us3_link );
65
66 if ( ! $result )
67 {
68 write_log( "$me: Bad query: $query" );
69 mail_to_user( "fail", "Internal Error $requestID\n$query\n" . mysql_error( $us3_link ) );
70 return( -1 );
71 }
72
73 list( $email_address, $investigatorGUID, $editXMLFilename ) = mysql_fetch_array( $result );
74
75 $query = "SELECT personID FROM people " .
76 "WHERE personGUID='$investigatorGUID'";
77 $result = mysql_query( $query, $us3_link );
78
79 list( $personID ) = mysql_fetch_array( $result );
80
81 /*
82 $query = "SELECT clusterName, submitTime, queueStatus, method " .
83 "FROM HPCAnalysisRequest h LEFT JOIN HPCAnalysisResult " .
84 "ON h.HPCAnalysisRequestID=HPCAnalysisResult.HPCAnalysisRequestID " .
85 "WHERE h.HPCAnalysisRequestID=$requestID";
86 */
87 $query = "SELECT clusterName, submitTime, queueStatus, method " .
88 "FROM HPCAnalysisRequest h, HPCAnalysisResult r " .
89 "WHERE h.HPCAnalysisRequestID=$requestID " .
90 "AND h.HPCAnalysisRequestID=r.HPCAnalysisRequestID";
91
92 $result = mysql_query( $query, $us3_link );
93
94 if ( ! $result )
95 {
96 write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
97 return( -1 );
98 }
99
100 if ( mysql_num_rows( $result ) == 0 )
101 {
102 write_log( "$me: US3 Table error - No records for requestID: $requestID" );
103 return( -1 );
104 }
105
106 list( $cluster, $submittime, $queuestatus, $jobtype ) = mysql_fetch_array( $result );
107
108 // Get the GFAC ID
109 $query = "SELECT HPCAnalysisResultID, gfacID FROM HPCAnalysisResult " .
110 "WHERE HPCAnalysisRequestID=$requestID";
111
112 $result = mysql_query( $query, $us3_link );
113
114 if ( ! $result )
115 {
116 write_log( "$me: Bad query: $query" );
117 mail_to_user( "fail", "Internal Error $requestID\n$query\n" . mysql_error( $us3_link ) );
118 return( -1 );
119 }
120
121 list( $HPCAnalysisResultID, $gfacID ) = mysql_fetch_array( $result );
122
123 ////////
124 // Get data from global GFAC DB and insert it into US3 DB
125 // $gfac_link = mysql_connect( $dbhost, $guser, $gpasswd );
126
127 $result = mysql_select_db( $gDB, $gfac_link );
128
129 if ( ! $result )
130 {
131 write_log( "$me: Could not connect to DB $gDB" );
132 mail_to_user( "fail", "Internal Error $requestID\nCould not connect to DB $gDB" );
133 return( -1 );
134 }
135
136 $query = "SELECT status, cluster, id FROM analysis " .
137 "WHERE gfacID='$gfacID'";
138
139 $result = mysql_query( $query, $gfac_link );
140 if ( ! $result )
141 {
142 write_log( "$me: Could not select GFAC status for $gfacID" );
143 mail_to_user( "fail", "Could not select GFAC status for $gfacID" );
144 return( -1 );
145 }
146
147 list( $status, $cluster, $id ) = mysql_fetch_array( $result );
148
149 if ( $cluster == 'bcf-local' || $cluster == 'alamo-local' )
150 {
151 $clushost = $cluster;
152 $clushost = preg_replace( "/\-local/", "", $clushost );
153 get_local_files( $gfac_link, $clushost, $requestID, $id, $gfacID );
154 }
155
156 $query = "SELECT id, stderr, stdout, tarfile FROM analysis " .
157 "WHERE gfacID='$gfacID'";
158
159 $result = mysql_query( $query, $gfac_link );
160
161 if ( ! $result )
162 {
163 write_log( "$me: Bad query:\n$query\n" . mysql_error( $gfac_link ) );
164 mail_to_user( "fail", "Internal error " . mysql_error( $gfac_link ) );
165 return( -1 );
166 }
167
168 list( $analysisID, $stderr, $stdout, $tarfile ) = mysql_fetch_array( $result );
169
170 // Save queue messages for post-mortem analysis
171 $query = "SELECT message, time FROM queue_messages " .
172 "WHERE analysisID = $analysisID " .
173 "ORDER BY time ";
174 $result = mysql_query( $query, $gfac_link );
175
176 if ( ! $result )
177 {
178 // Just log it and continue
179 write_log( "$me: Bad query:\n$query\n" . mysql_error( $gfac_link ) );
180 }
181
182 $now = date( 'Y-m-d H:i:s' );
183 $message_log = "US3 DB: $db\n" .
184 "RequestID: $requestID\n" .
185 "GFAC ID: $gfacID\n" .
186 "Processed: $now\n\n" .
187 "Queue Messages\n\n" ;
188 if ( mysql_num_rows( $result ) > 0 )
189 {
190 while ( list( $message, $time ) = mysql_fetch_array( $result ) )
191 $message_log .= "$time $message\n";
192 }
193
194 $query = "DELETE FROM queue_messages " .
195 "WHERE analysisID = $analysisID ";
196
197 $result = mysql_query( $query, $gfac_link );
198
199 if ( ! $result )
200 {
201 // Just log it and continue
202 write_log( "$me: Bad query:\n$query\n" . mysql_error( $gfac_link ) );
203 }
204
205 // Save stdout, stderr, etc. for message log
206 $query = "SELECT stdout, stderr, status, queue_msg FROM analysis " .
207 "WHERE gfacID='$gfacID' ";
208 $result = mysql_query( $query, $gfac_link );
209 try
210 {
211 // What if this is too large?
212 list( $stdout, $stderr, $status, $queue_msg ) = mysql_fetch_array( $result );
213 }
214 catch ( Exception $e )
215 {
216 write_log( "$me: stdout + stderr larger than 128M - $gfacID\n" . mysql_error( $gfac_link ) );
217 // Just go ahead and clean up
218 }
219
220 // But let's allow for investigation of other large stdout and/or stderr
221 if ( strlen( $stdout ) > 20480000 ||
222 strlen( $stderr ) > 20480000 )
223 write_log( "$me: stdout + stderr larger than 20M - $gfacID\n" );
224
225 $message_log .= "\n\n\nStdout Contents\n\n" .
226 $stdout .
227 "\n\n\nStderr Contents\n\n" .
228 $stderr .
229 "\n\n\nGFAC Status: $status\n" .
230 "GFAC message field: $queue_msg\n";
231
232 // Delete data from GFAC DB
233 $query = "DELETE from analysis WHERE gfacID='$gfacID'";
234
235 $result = mysql_query( $query, $gfac_link );
236
237 if ( ! $result )
238 {
239 // Just log it and continue
240 write_log( "$me: Bad query:\n$query\n" . mysql_error( $gfac_link ) );
241 }
242
243 // Copy queue messages to LIMS submit directory (files there are deleted after 7 days)
244 global $submit_dir;
245
246 // Get the request guid (LIMS submit dir name)
247 $query = "SELECT HPCAnalysisRequestGUID FROM HPCAnalysisRequest " .
248 "WHERE HPCAnalysisRequestID = $requestID ";
249 $result = mysql_query( $query, $us3_link );
250
251 if ( ! $result )
252 {
253 write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
254 }
255
256 list( $requestGUID ) = mysql_fetch_array( $result );
257 $output_dir = "$submit_dir/$requestGUID";
258
259 // Try to create it if necessary, and write the file
260 // Let's use FILE_APPEND, in case this is the second time around and the
261 // GFAC job status was INSERTed, rather than UPDATEd
262 if ( ! is_dir( $output_dir ) )
263 mkdir( $output_dir, 0775, true );
264 $message_filename = "$output_dir/$db-$requestID-messages.txt";
265 file_put_contents( $message_filename, $message_log, FILE_APPEND );
266 // mysql_close( $gfac_link );
267
268 /////////
269 // Insert data into HPCAnalysis
270
271 $query = "UPDATE HPCAnalysisResult SET " .
272 "stderr='" . mysql_real_escape_string( $stderr, $us3_link ) . "', " .
273 "stdout='" . mysql_real_escape_string( $stdout, $us3_link ) . "' " .
274 "WHERE HPCAnalysisResultID=$HPCAnalysisResultID";
275
276 $result = mysql_query( $query, $us3_link );
277
278 if ( ! $result )
279 {
280 write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
281 mail_to_user( "fail", "Bad query:\n$query\n" . mysql_error( $us3_link ) );
282 return( -1 );
283 }
284
285 // Save the tarfile and expand it
286
287 if ( strlen( $tarfile ) == 0 )
288 {
289 write_log( "$me: No tarfile" );
290 mail_to_user( "fail", "No results" );
291 return( -1 );
292 }
293
294 // Shouldn't happen
295 if ( ! is_dir( "$work" ) )
296 {
297 write_log( "$me: $work directory does not exist" );
298 mail_to_user( "fail", "$work directory does not exist" );
299 return( -1 );
300 }
301
302 if ( ! is_dir( "$work/$gfacID" ) ) mkdir( "$work/$gfacID", 0770 );
303 chdir( "$work/$gfacID" );
304
305 $f = fopen( "analysis.tar", "w" );
306 fwrite( $f, $tarfile );
307 fclose( $f );
308
309 $tar_out = array();
310 exec( "tar -xf analysis.tar 2>&1", $tar_out, $err );
311
312 if ( $err != 0 )
313 {
314 chdir( $work );
315 exec( "rm -r $gfacID" );
316 $output = implode( "\n", $tar_out );
317
318 write_log( "$me: Bad output tarfile: $output" );
319 mail_to_user( "fail", "Bad output file" );
320 return( -1 );
321 }
322
323 // Insert the model files and noise files
324 $files = file( "analysis_files.txt", FILE_IGNORE_NEW_LINES );
325 $noiseIDs = array();
326 $modelGUIDs = array();
327
328 foreach ( $files as $file )
329 {
330 $split = explode( ";", $file );
331
332 if ( count( $split ) > 1 )
333 {
334 list( $fn, $meniscus, $mc_iteration, $variance ) = explode( ";", $file );
335
336 list( $other, $mc_iteration ) = explode( "=", $mc_iteration );
337 list( $other, $variance ) = explode( "=", $variance );
338 list( $other, $meniscus ) = explode( "=", $meniscus );
339 }
340 else
341 $fn = $file;
342
343 if ( filesize( $fn ) < 100 )
344 {
345 write_log( "$me:fn is invalid $fn" );
346 mail_to_user( "fail", "Internal error\n$fn is invalid" );
347 return( -1 );
348 }
349
350 if ( preg_match( "/^job_statistics\.xml$/", $fn ) ) // Job statistics file
351 {
352 $xml = file_get_contents( $fn );
353 $statistics = parse_xml( $xml, 'statistics' );
354 $otherdata = parse_xml( $xml, 'id' );
355
356 $query = "UPDATE HPCAnalysisResult SET " .
357 "wallTime = {$statistics['walltime']}, " .
358 "CPUTime = {$statistics['cputime']}, " .
359 "CPUCount = {$statistics['cpucount']}, " .
360 "max_rss = {$statistics['maxmemory']}, " .
361 "startTime = '{$otherdata['starttime']}', " .
362 "endTime = '{$otherdata['endtime']}', " .
363 "mgroupcount = {$otherdata['groupcount']} " .
364 "WHERE HPCAnalysisResultID=$HPCAnalysisResultID";
365 $result = mysql_query( $query, $us3_link );
366
367 if ( ! $result )
368 {
369 write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
370 }
371
372 file_put_contents( "$output_dir/$fn", $xml ); // Copy to submit dir
373
374 }
375
376 else if ( preg_match( "/\.noise/", $fn ) > 0 ) // It's a noise file
377 {
378 $xml = file_get_contents( $fn );
379 $noise_data = parse_xml( $xml, "noise" );
380 $type = ( $noise_data[ 'type' ] == "ri" ) ? "ri_noise" : "ti_noise";
381 $desc = $noise_data[ 'description' ];
382 $modelGUID = $noise_data[ 'modelGUID' ];
383 $noiseGUID = $noise_data[ 'noiseGUID' ];
384
385 $query = "INSERT INTO noise SET " .
386 "noiseGUID='$noiseGUID'," .
387 "modelGUID='$modelGUID'," .
388 "editedDataID=1, " .
389 "modelID=1, " .
390 "noiseType='$type'," .
391 "description='$desc'," .
392 "xml='" . mysql_real_escape_string( $xml, $us3_link ) . "'";
393
394 // Add later after all files are processed: editDataID, modelID
395
396 $result = mysql_query( $query, $us3_link );
397
398 if ( ! $result )
399 {
400 write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
401 mail_to_user( "fail", "Internal error\n$query\n" . mysql_error( $us3_link ) );
402 return( -1 );
403 }
404
405 $id = mysql_insert_id( $us3_link );
406 $file_type = "noise";
407 $noiseIDs[] = $id;
408
409 // Keep track of modelGUIDs for later, when we replace them
410 $modelGUIDs[ $id ] = $modelGUID;
411
412 }
413 else // It's a model file
414 {
415 $xml = file_get_contents( $fn );
416 $model_data = parse_xml( $xml, "model" );
417 $description = $model_data[ 'description' ];
418 $modelGUID = $model_data[ 'modelGUID' ];
419 $editGUID = $model_data[ 'editGUID' ];
420
421 $query = "INSERT INTO model SET " .
422 "modelGUID='$modelGUID'," .
423 "editedDataID=" .
424 "(SELECT editedDataID FROM editedData WHERE editGUID='$editGUID')," .
425 "description='$description'," .
426 "MCIteration='$mc_iteration'," .
427 "meniscus='$meniscus'," .
428 "variance='$variance'," .
429 "xml='" . mysql_real_escape_string( $xml, $us3_link ) . "'";
430
431 $result = mysql_query( $query, $us3_link );
432
433 if ( ! $result )
434 {
435 write_log( "$me: Bad query:\n$query " . mysql_error( $us3_link ) );
436 mail_to_user( "fail", "Internal error\n$query\n" . mysql_error( $us3_link ) );
437 return( -1 );
438 }
439
440 $modelID = mysql_insert_id( $us3_link );
441 $id = $modelID;
442 $file_type = "model";
443
444 $query = "INSERT INTO modelPerson SET " .
445 "modelID=$modelID, personID=$personID";
446 $result = mysql_query( $query, $us3_link );
447 }
448
449 $query = "INSERT INTO HPCAnalysisResultData SET " .
450 "HPCAnalysisResultID='$HPCAnalysisResultID', " .
451 "HPCAnalysisResultType='$file_type', " .
452 "resultID=$id";
453
454 $result = mysql_query( $query, $us3_link );
455
456 if ( ! $result )
457 {
458 write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
459 mail_to_user( "fail", "Internal error\n$query\n" . mysql_error( $us3_link ) );
460 return( -1 );
461 }
462 }
463
464 // Now fix up noise entries
465 // For noise files, there is, at most two: ti_noise and ri_noise
466 // In this case there will only be one modelID
467
468 foreach ( $noiseIDs as $noiseID )
469 {
470 $modelGUID = $modelGUIDs[ $noiseID ];
471 $query = "UPDATE noise SET " .
472 "editedDataID=" .
473 "(SELECT editedDataID FROM model WHERE modelGUID='$modelGUID')," .
474 "modelID=" .
475 "(SELECT modelID FROM model WHERE modelGUID='$modelGUID')" .
476 "WHERE noiseID=$noiseID";
477
478 $result = mysql_query( $query, $us3_link );
479
480 if ( ! $result )
481 {
482 write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
483 mail_to_user( "fail", "Bad query\n$query\n" . mysql_error( $us3_link ) );
484 return( -1 );
485 }
486 }
487
488 // Copy results to LIMS submit directory (files there are deleted after 7 days)
489 global $submit_dir; // LIMS submit files dir
490
491 // Get the request guid (LIMS submit dir name)
492 $query = "SELECT HPCAnalysisRequestGUID FROM HPCAnalysisRequest " .
493 "WHERE HPCAnalysisRequestID = $requestID ";
494 $result = mysql_query( $query, $us3_link );
495
496 if ( ! $result )
497 {
498 write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
499 }
500
501 list( $requestGUID ) = mysql_fetch_array( $result );
502
503 chdir( "$submit_dir/$requestGUID" );
504 $f = fopen( "analysis.tar", "w" );
505 fwrite( $f, $tarfile );
506 fclose( $f );
507
508 // Clean up
509 chdir ( $work );
510 // exec( "rm -rf $gfacID" );
511
512 mysql_close( $us3_link );
513
514 /////////
515 // Send email
516
517 mail_to_user( "success", "" );
518}
519
520function mail_to_user( $type, $msg )
521{
522 // Note to me. Just changed subject line to include a modified $status instead
523 // of the $type variable passed. More informative than just "fail" or "success."
524 // See how it works for awhile and then consider removing $type parameter from
525 // function.
526 global $email_address;
527 global $submittime;
528 global $queuestatus;
529 global $status;
530 global $cluster;
531 global $jobtype;
532 global $org_name;
533 global $admin_email;
534 global $db;
535 global $dbhost;
536 global $requestID;
537 global $gfacID;
538 global $editXMLFilename;
539 global $stdout;
540
541global $me;
542write_log( "$me mail_to_user(): sending email to $email_address for $gfacID" );
543
544 // Get GFAC status and message
545 // function get_gfac_message() also sets global $status
546 $gfac_message = get_gfac_message( $gfacID );
547 if ( $gfac_message === false ) $gfac_message = "";
548
549 // Create a status to put in the subject line
550 switch ( $status )
551 {
552 case "COMPLETE":
553 $subj_status = 'completed';
554 break;
555
556 case "CANCELLED":
557 case "CANCELED":
558 $subj_status = 'canceled';
559 break;
560
561 case "FAILED":
562 $subj_status = 'failed';
563 break;
564
565 case "ERROR":
566 $subj_status = 'unknown error';
567 break;
568
569 default:
570 $subj_status = $status; // For now
571 break;
572
573 }
574
575 // Parse the editXMLFilename
576 list( $runID, $editID, $dataType, $cell, $channel, $wl, $ext ) =
577 explode( ".", $editXMLFilename );
578
579 $headers = "From: $org_name Admin<$admin_email>" . "\n";
580 $headers .= "Cc: $org_name Admin<$admin_email>" . "\n";
581
582 // Set the reply address
583 $headers .= "Reply-To: $org_name<$admin_email>" . "\n";
584 $headers .= "Return-Path: $org_name<$admin_email>" . "\n";
585
586 // Try to avoid spam filters
587 $now = time();
588 $headers .= "Message-ID: <" . $now . "cleanup@$dbhost>\n";
589 $headers .= "X-Mailer: PHP v" . phpversion() . "\n";
590 $headers .= "MIME-Version: 1.0" . "\n";
591 $headers .= "Content-Transfer-Encoding: 8bit" . "\n";
592
593 $subject = "UltraScan Job Notification - $subj_status - " . substr( $gfacID, 0, 16 );
594 $message = "
595 Your UltraScan job is complete:
596
597 Submission Time : $submittime
598 Analysis ID : $gfacID
599 Request ID : $requestID ( $db )
600 RunID : $runID
601 EditID : $editID
602 Data Type : $dataType
603 Cell/Channel/Wl : $cell / $channel / $wl
604 Status : $queuestatus
605 Cluster : $cluster
606 Job Type : $jobtype
607 GFAC Status : $status
608 GFAC Message : $gfac_message
609 Stdout : $stdout
610 ";
611
612 if ( $type != "success" ) $message .= "Grid Ctrl Error : $msg\n";
613
614 // Handle the error case where an error occurs before fetching the
615 // user's email address
616 if ( $email_address == "" ) $email_address = $admin_email;
617
618 mail( $email_address, $subject, $message, $headers );
619}
620
621function parse_xml( $xml, $type )
622{
623 $parser = new XMLReader();
624 $parser->xml( $xml );
625
626 $results = array();
627
628 while ( $parser->read() )
629 {
630 if ( $parser->name == $type )
631 {
632 while ( $parser->moveToNextAttribute() )
633 {
634 $results[ $parser->name ] = $parser->value;
635 }
636
637 break;
638 }
639 }
640
641 $parser->close();
642 return $results;
643}
644
645// Function to get information about the current job GFAC
646function get_gfac_message( $gfacID )
647{
648 global $serviceURL;
649 global $me;
650
651 $hex = "[0-9a-fA-F]";
652 if ( ! preg_match( "/^US3-Experiment/i", $gfacID ) &&
653 ! preg_match( "/^US3-$hex{8}-$hex{4}-$hex{4}-$hex{4}-$hex{12}$/", $gfacID ) )
654 {
655 // Then it's not a GFAC job
656 return false;
657 }
658
659 $url = "$serviceURL/jobstatus/$gfacID";
660 try
661 {
662 $post = new HttpRequest( $url, HttpRequest::METH_GET );
663 $http = $post->send();
664 $xml = $post->getResponseBody();
665 }
666 catch ( HttpException $e )
667 {
668 write_log( "$me: Job status not available - $gfacID" );
669 return false;
670 }
671
672 // Parse the result
673 $gfac_message = parse_message( $xml );
674
675 return $gfac_message;
676}
677
678function parse_message( $xml )
679{
680 global $status;
681 $status = "";
682 $gfac_message = "";
683
684 $parser = new XMLReader();
685 $parser->xml( $xml );
686
687 $results = array();
688
689 while( $parser->read() )
690 {
691 $type = $parser->nodeType;
692
693 if ( $type == XMLReader::ELEMENT )
694 $name = $parser->name;
695
696 else if ( $type == XMLReader::TEXT )
697 {
698 if ( $name == "status" )
699 $status = $parser->value;
700 else
701 $gfac_message = $parser->value;
702 }
703 }
704
705 $parser->close();
706 return $gfac_message;
707}
708
709function get_local_files( $gfac_link, $cluster, $requestID, $id, $gfacID )
710{
711 global $work;
712 global $work_remote;
713 global $me;
714 global $db;
715 global $status;
716
717 // Figure out local working directory
718 if ( ! is_dir( "$work/$gfacID" ) ) mkdir( "$work/$gfacID", 0770 );
719 $pwd = chdir( "$work/$gfacID" );
720
721 // Figure out remote directory
722 $remoteDir = sprintf( "$work_remote/$db-%06d", $requestID );
723
724 // Get stdout, stderr, output/analysis-results.tar
725 $output = array();
726// $cmd = "scp us3@$cluster.uthscsa.edu:$remoteDir/stdout . 2>&1";
727//
728// exec( $cmd, $output, $stat );
729// if ( $stat != 0 )
730// write_log( "$me: Bad exec:\n$cmd\n" . implode( "\n", $output ) );
731//
732// $cmd = "scp us3@$cluster.uthscsa.edu:$remoteDir/stderr . 2>&1";
733// exec( $cmd, $output, $stat );
734// if ( $stat != 0 )
735// write_log( "$me: Bad exec:\n$cmd\n" . implode( "\n", $output ) );
736
737 $cmd = "scp us3@$cluster.uthscsa.edu:$remoteDir/output/analysis-results.tar . 2>&1";
738 exec( $cmd, $output, $stat );
739 if ( $stat != 0 )
740 write_log( "$me: Bad exec:\n$cmd\n" . implode( "\n", $output ) );
741
742 $cmd = "scp us3@$cluster.uthscsa.edu:$remoteDir/stdout . 2>&1";
743 exec( $cmd, $output, $stat );
744 if ( $stat != 0 )
745 {
746 write_log( "$me: Bad exec:\n$cmd\n" . implode( "\n", $output ) );
747 sleep( 10 );
748 write_log( "$me: RETRY" );
749 exec( $cmd, $output, $stat );
750 if ( $stat != 0 )
751 write_log( "$me: Bad exec:\n$cmd\n" . implode( "\n", $output ) );
752 }
753
754 $cmd = "scp us3@$cluster.uthscsa.edu:$remoteDir/stderr . 2>&1";
755 exec( $cmd, $output, $stat );
756 if ( $stat != 0 )
757 {
758 write_log( "$me: Bad exec:\n$cmd\n" . implode( "\n", $output ) );
759 sleep( 10 );
760 write_log( "$me: RETRY" );
761 exec( $cmd, $output, $stat );
762 if ( $stat != 0 )
763 write_log( "$me: Bad exec:\n$cmd\n" . implode( "\n", $output ) );
764 }
765
766 // Write the files to gfacDB
767
768 if ( file_exists( "stderr" ) ) $stderr = file_get_contents( "stderr" );
769 if ( file_exists( "stdout" ) ) $stdout = file_get_contents( "stdout" );
770 if ( file_exists( "analysis-results.tar" ) )
771 $tarfile = file_get_contents( "analysis-results.tar" );
772
773 $query = "UPDATE analysis SET " .
774 "stderr='" . mysql_real_escape_string( $stderr, $gfac_link ) . "'," .
775 "stdout='" . mysql_real_escape_string( $stdout, $gfac_link ) . "'," .
776 "tarfile='" . mysql_real_escape_string( $tarfile, $gfac_link ) . "'";
777
778 $result = mysql_query( $query, $gfac_link );
779
780 if ( ! $result )
781 {
782 write_log( "$me: Bad query:\n$query\n" . mysql_error( $gfac_link ) );
783 echo "Bad query\n";
784 return( -1 );
785 }
786}
787?>
Note: See TracBrowser for help on using the repository browser.