1 | <?php
|
---|
2 | /*
|
---|
3 | * cleanup_gfac.php
|
---|
4 | *
|
---|
5 | * functions relating to copying results and cleaning up the gfac DB
|
---|
6 | *
|
---|
7 | */
|
---|
8 |
|
---|
9 | $us3bin = exec( "ls -d ~us3/bin" );
|
---|
10 | include_once "$us3bin/listen-config.php";
|
---|
11 | $me = 'cleanup_gfac.php';
|
---|
12 | $email_address = '';
|
---|
13 | $queuestatus = '';
|
---|
14 | $jobtype = '';
|
---|
15 | $db = '';
|
---|
16 | $editXMLFilename = '';
|
---|
17 | $status = '';
|
---|
18 |
|
---|
19 | function 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 | $num_rows = mysql_num_rows( $result );
|
---|
148 | if ( $num_rows == 0 )
|
---|
149 | {
|
---|
150 | write_log( "$me: Cleanup analysis query found 0 entries for $gfacID" );
|
---|
151 | return( 0 );
|
---|
152 | }
|
---|
153 | //else
|
---|
154 | //{
|
---|
155 | //write_log( "$me: db=$db; num_rows=$num_rows; queuestatus=$queuestatus" );
|
---|
156 | //}
|
---|
157 |
|
---|
158 | list( $status, $cluster, $id ) = mysql_fetch_array( $result );
|
---|
159 | //write_log( "$me: db=$db; requestID=$requestID; status=$status; cluster=$cluster" );
|
---|
160 |
|
---|
161 | if ( $cluster == 'bcf-local' || $cluster == 'alamo-local' )
|
---|
162 | {
|
---|
163 | $clushost = $cluster;
|
---|
164 | $clushost = preg_replace( "/\-local/", "", $clushost );
|
---|
165 | get_local_files( $gfac_link, $clushost, $requestID, $id, $gfacID );
|
---|
166 | }
|
---|
167 |
|
---|
168 | $query = "SELECT id, stderr, stdout, tarfile FROM analysis " .
|
---|
169 | "WHERE gfacID='$gfacID'";
|
---|
170 |
|
---|
171 | $result = mysql_query( $query, $gfac_link );
|
---|
172 |
|
---|
173 | if ( ! $result )
|
---|
174 | {
|
---|
175 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $gfac_link ) );
|
---|
176 | mail_to_user( "fail", "Internal error " . mysql_error( $gfac_link ) );
|
---|
177 | return( -1 );
|
---|
178 | }
|
---|
179 |
|
---|
180 | $num_rows = mysql_num_rows( $result );
|
---|
181 | if ( $num_rows == 0 )
|
---|
182 | {
|
---|
183 | write_log( "$me: Cleanup analysis query found 0 entries for $gfacID" );
|
---|
184 | return( 0 );
|
---|
185 | }
|
---|
186 |
|
---|
187 | list( $analysisID, $stderr, $stdout, $tarfile ) = mysql_fetch_array( $result );
|
---|
188 |
|
---|
189 | if ( strlen( $tarfile ) > 0 )
|
---|
190 | { // Log success at fetch attempt
|
---|
191 | write_log( "$me: Successful data fetch: $requestID $gfacID" );
|
---|
192 | }
|
---|
193 | else
|
---|
194 | { // Log failure at fetch attempt
|
---|
195 | write_log( "$me: Failed data fetch: $requestID $gfacID" );
|
---|
196 | if ( $analysisID == '' )
|
---|
197 | $analysisID = '0';
|
---|
198 | }
|
---|
199 |
|
---|
200 | // Save queue messages for post-mortem analysis
|
---|
201 | $query = "SELECT message, time FROM queue_messages " .
|
---|
202 | "WHERE analysisID = $analysisID " .
|
---|
203 | "ORDER BY time ";
|
---|
204 | $result = mysql_query( $query, $gfac_link );
|
---|
205 |
|
---|
206 | if ( ! $result )
|
---|
207 | {
|
---|
208 | // Just log it and continue
|
---|
209 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $gfac_link ) );
|
---|
210 | }
|
---|
211 |
|
---|
212 | $now = date( 'Y-m-d H:i:s' );
|
---|
213 | $message_log = "US3 DB: $db\n" .
|
---|
214 | "RequestID: $requestID\n" .
|
---|
215 | "GFAC ID: $gfacID\n" .
|
---|
216 | "Processed: $now\n\n" .
|
---|
217 | "Queue Messages\n\n" ;
|
---|
218 |
|
---|
219 | $need_finish = ( $status == 'COMPLETE' );
|
---|
220 |
|
---|
221 | if ( mysql_num_rows( $result ) > 0 )
|
---|
222 | {
|
---|
223 | $time_msg = time();
|
---|
224 | while ( list( $message, $time ) = mysql_fetch_array( $result ) )
|
---|
225 | {
|
---|
226 | //write_log( "$me: message=$message" );
|
---|
227 | $message_log .= "$time $message\n";
|
---|
228 | if ( preg_match( "/^Finished/i", $message ) )
|
---|
229 | $need_finish = false;
|
---|
230 | $time_msg = strtotime( $time );
|
---|
231 | }
|
---|
232 |
|
---|
233 | if ( $need_finish )
|
---|
234 | { // No 'Finished' yet: forget if too much time has passed
|
---|
235 | $time_now = time();
|
---|
236 | $tdelta = $time_now - $time_msg;
|
---|
237 | write_log( "$me: no-Finish time: tnow=$time_now, tmsg=$time_msg, tdelt=$tdelta" );
|
---|
238 | if ( $tdelta > 600 )
|
---|
239 | $need_finish = false;
|
---|
240 | }
|
---|
241 | //write_log( "$me: no-Finish time: tnow=$time_now, tmsg=$time_msg, tdelt=$tdelta" );
|
---|
242 | }
|
---|
243 | else
|
---|
244 | {
|
---|
245 | write_log( "$me: No messages for analysisID=$analysisID ." );
|
---|
246 | $need_finish = false;
|
---|
247 | }
|
---|
248 |
|
---|
249 | if ( $need_finish )
|
---|
250 | {
|
---|
251 | write_log( "$me: Cleanup has not yet found 'Finished' for $gfacID" );
|
---|
252 | return( 0 );
|
---|
253 | }
|
---|
254 |
|
---|
255 | $query = "DELETE FROM queue_messages " .
|
---|
256 | "WHERE analysisID = $analysisID ";
|
---|
257 |
|
---|
258 | $result = mysql_query( $query, $gfac_link );
|
---|
259 |
|
---|
260 | if ( ! $result )
|
---|
261 | {
|
---|
262 | // Just log it and continue
|
---|
263 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $gfac_link ) );
|
---|
264 | }
|
---|
265 |
|
---|
266 | // Save stdout, stderr, etc. for message log
|
---|
267 | $query = "SELECT stdout, stderr, status, queue_msg FROM analysis " .
|
---|
268 | "WHERE gfacID='$gfacID' ";
|
---|
269 | $result = mysql_query( $query, $gfac_link );
|
---|
270 | try
|
---|
271 | {
|
---|
272 | // What if this is too large?
|
---|
273 | list( $stdout, $stderr, $status, $queue_msg ) = mysql_fetch_array( $result );
|
---|
274 | }
|
---|
275 | catch ( Exception $e )
|
---|
276 | {
|
---|
277 | write_log( "$me: stdout + stderr larger than 128M - $gfacID\n" . mysql_error( $gfac_link ) );
|
---|
278 | // Just go ahead and clean up
|
---|
279 | }
|
---|
280 |
|
---|
281 | // But let's allow for investigation of other large stdout and/or stderr
|
---|
282 | if ( strlen( $stdout ) > 20480000 ||
|
---|
283 | strlen( $stderr ) > 20480000 )
|
---|
284 | write_log( "$me: stdout + stderr larger than 20M - $gfacID\n" );
|
---|
285 |
|
---|
286 | $message_log .= "\n\n\nStdout Contents\n\n" .
|
---|
287 | $stdout .
|
---|
288 | "\n\n\nStderr Contents\n\n" .
|
---|
289 | $stderr .
|
---|
290 | "\n\n\nGFAC Status: $status\n" .
|
---|
291 | "GFAC message field: $queue_msg\n";
|
---|
292 |
|
---|
293 | // Delete data from GFAC DB
|
---|
294 | $query = "DELETE from analysis WHERE gfacID='$gfacID'";
|
---|
295 |
|
---|
296 | $result = mysql_query( $query, $gfac_link );
|
---|
297 |
|
---|
298 | if ( ! $result )
|
---|
299 | {
|
---|
300 | // Just log it and continue
|
---|
301 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $gfac_link ) );
|
---|
302 | }
|
---|
303 | write_log( "$me: GFAC DB entry deleted" );
|
---|
304 |
|
---|
305 | // Copy queue messages to LIMS submit directory (files there are deleted after 7 days)
|
---|
306 | global $submit_dir;
|
---|
307 |
|
---|
308 | // Get the request guid (LIMS submit dir name)
|
---|
309 | $query = "SELECT HPCAnalysisRequestGUID FROM HPCAnalysisRequest " .
|
---|
310 | "WHERE HPCAnalysisRequestID = $requestID ";
|
---|
311 | $result = mysql_query( $query, $us3_link );
|
---|
312 |
|
---|
313 | if ( ! $result )
|
---|
314 | {
|
---|
315 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
|
---|
316 | }
|
---|
317 |
|
---|
318 | list( $requestGUID ) = mysql_fetch_array( $result );
|
---|
319 | $output_dir = "$submit_dir/$requestGUID";
|
---|
320 | write_log( "$me: Output dir determined: $output_dir" );
|
---|
321 |
|
---|
322 | // Try to create it if necessary, and write the file
|
---|
323 | // Let's use FILE_APPEND, in case this is the second time around and the
|
---|
324 | // GFAC job status was INSERTed, rather than UPDATEd
|
---|
325 | if ( ! is_dir( $output_dir ) )
|
---|
326 | mkdir( $output_dir, 0775, true );
|
---|
327 | $message_filename = "$output_dir/$db-$requestID-messages.txt";
|
---|
328 | file_put_contents( $message_filename, $message_log, FILE_APPEND );
|
---|
329 | // mysql_close( $gfac_link );
|
---|
330 | //write_log( "$me: *messages.txt written" );
|
---|
331 |
|
---|
332 | /////////
|
---|
333 | // Insert data into HPCAnalysis
|
---|
334 |
|
---|
335 | $query = "UPDATE HPCAnalysisResult SET " .
|
---|
336 | "stderr='" . mysql_real_escape_string( $stderr, $us3_link ) . "', " .
|
---|
337 | "stdout='" . mysql_real_escape_string( $stdout, $us3_link ) . "' " .
|
---|
338 | "WHERE HPCAnalysisResultID=$HPCAnalysisResultID";
|
---|
339 |
|
---|
340 | $result = mysql_query( $query, $us3_link );
|
---|
341 |
|
---|
342 | if ( ! $result )
|
---|
343 | {
|
---|
344 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
|
---|
345 | mail_to_user( "fail", "Bad query:\n$query\n" . mysql_error( $us3_link ) );
|
---|
346 | return( -1 );
|
---|
347 | }
|
---|
348 | //write_log( "$me: HPCAnalysisResult stderr,stdout updated" );
|
---|
349 |
|
---|
350 | // Save the tarfile and expand it
|
---|
351 |
|
---|
352 | if ( strlen( $tarfile ) == 0 )
|
---|
353 | {
|
---|
354 | write_log( "$me: No tarfile" );
|
---|
355 | mail_to_user( "fail", "No results" );
|
---|
356 | return( -1 );
|
---|
357 | }
|
---|
358 |
|
---|
359 | // Shouldn't happen
|
---|
360 | if ( ! is_dir( "$work" ) )
|
---|
361 | {
|
---|
362 | write_log( "$me: $work directory does not exist" );
|
---|
363 | mail_to_user( "fail", "$work directory does not exist" );
|
---|
364 | return( -1 );
|
---|
365 | }
|
---|
366 |
|
---|
367 | if ( ! is_dir( "$work/$gfacID" ) ) mkdir( "$work/$gfacID", 0770 );
|
---|
368 | chdir( "$work/$gfacID" );
|
---|
369 |
|
---|
370 | $f = fopen( "analysis.tar", "w" );
|
---|
371 | fwrite( $f, $tarfile );
|
---|
372 | fclose( $f );
|
---|
373 | write_log( "$me: analysis.tar file written to work dir" );
|
---|
374 |
|
---|
375 | $tar_out = array();
|
---|
376 | exec( "tar -xf analysis.tar 2>&1", $tar_out, $err );
|
---|
377 |
|
---|
378 | if ( $err != 0 )
|
---|
379 | {
|
---|
380 | chdir( $work );
|
---|
381 | exec( "rm -r $gfacID" );
|
---|
382 | $output = implode( "\n", $tar_out );
|
---|
383 |
|
---|
384 | write_log( "$me: Bad output tarfile: $output" );
|
---|
385 | mail_to_user( "fail", "Bad output file" );
|
---|
386 | return( -1 );
|
---|
387 | }
|
---|
388 | //write_log( "$me: tar files extracted" );
|
---|
389 |
|
---|
390 | // Insert the model files and noise files
|
---|
391 | $files = file( "analysis_files.txt", FILE_IGNORE_NEW_LINES );
|
---|
392 | $noiseIDs = array();
|
---|
393 | $modelGUIDs = array();
|
---|
394 | $mrecsIDs = array();
|
---|
395 | $rmodlGUIDs = array();
|
---|
396 |
|
---|
397 | foreach ( $files as $file )
|
---|
398 | {
|
---|
399 | $split = explode( ";", $file );
|
---|
400 |
|
---|
401 | if ( count( $split ) > 1 )
|
---|
402 | {
|
---|
403 | list( $fn, $meniscus, $mc_iteration, $variance ) = explode( ";", $file );
|
---|
404 |
|
---|
405 | list( $other, $mc_iteration ) = explode( "=", $mc_iteration );
|
---|
406 | list( $other, $variance ) = explode( "=", $variance );
|
---|
407 | list( $other, $meniscus ) = explode( "=", $meniscus );
|
---|
408 | }
|
---|
409 | else
|
---|
410 | $fn = $file;
|
---|
411 |
|
---|
412 | if ( preg_match( "/mdl.tmp$/", $fn ) )
|
---|
413 | continue;
|
---|
414 |
|
---|
415 | if ( filesize( $fn ) < 100 )
|
---|
416 | {
|
---|
417 | write_log( "$me:fn is invalid $fn" );
|
---|
418 | mail_to_user( "fail", "Internal error\n$fn is invalid" );
|
---|
419 | return( -1 );
|
---|
420 | }
|
---|
421 | //write_log( "$me: handling file: $fn" );
|
---|
422 |
|
---|
423 | if ( preg_match( "/^job_statistics\.xml$/", $fn ) ) // Job statistics file
|
---|
424 | {
|
---|
425 | $xml = file_get_contents( $fn );
|
---|
426 | $statistics = parse_xml( $xml, 'statistics' );
|
---|
427 | $otherdata = parse_xml( $xml, 'id' );
|
---|
428 |
|
---|
429 | $query = "UPDATE HPCAnalysisResult SET " .
|
---|
430 | "wallTime = {$statistics['walltime']}, " .
|
---|
431 | "CPUTime = {$statistics['cputime']}, " .
|
---|
432 | "CPUCount = {$statistics['cpucount']}, " .
|
---|
433 | "max_rss = {$statistics['maxmemory']}, " .
|
---|
434 | "startTime = '{$otherdata['starttime']}', " .
|
---|
435 | "endTime = '{$otherdata['endtime']}', " .
|
---|
436 | "mgroupcount = {$otherdata['groupcount']} " .
|
---|
437 | "WHERE HPCAnalysisResultID=$HPCAnalysisResultID";
|
---|
438 | $result = mysql_query( $query, $us3_link );
|
---|
439 |
|
---|
440 | if ( ! $result )
|
---|
441 | {
|
---|
442 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
|
---|
443 | }
|
---|
444 |
|
---|
445 | file_put_contents( "$output_dir/$fn", $xml ); // Copy to submit dir
|
---|
446 | $file_type = "job_stats";
|
---|
447 | //write_log( "$me: job_statistics file updated in Result and written" );
|
---|
448 |
|
---|
449 | }
|
---|
450 |
|
---|
451 | else if ( preg_match( "/\.noise/", $fn ) > 0 ) // It's a noise file
|
---|
452 | {
|
---|
453 | $xml = file_get_contents( $fn );
|
---|
454 | $noise_data = parse_xml( $xml, "noise" );
|
---|
455 | $type = ( $noise_data[ 'type' ] == "ri" ) ? "ri_noise" : "ti_noise";
|
---|
456 | $desc = $noise_data[ 'description' ];
|
---|
457 | $modelGUID = $noise_data[ 'modelGUID' ];
|
---|
458 | $noiseGUID = $noise_data[ 'noiseGUID' ];
|
---|
459 |
|
---|
460 | $query = "INSERT INTO noise SET " .
|
---|
461 | "noiseGUID='$noiseGUID'," .
|
---|
462 | "modelGUID='$modelGUID'," .
|
---|
463 | "editedDataID=1, " .
|
---|
464 | "modelID=1, " .
|
---|
465 | "noiseType='$type'," .
|
---|
466 | "description='$desc'," .
|
---|
467 | "xml='" . mysql_real_escape_string( $xml, $us3_link ) . "'";
|
---|
468 |
|
---|
469 | // Add later after all files are processed: editDataID, modelID
|
---|
470 |
|
---|
471 | $result = mysql_query( $query, $us3_link );
|
---|
472 |
|
---|
473 | if ( ! $result )
|
---|
474 | {
|
---|
475 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
|
---|
476 | mail_to_user( "fail", "Internal error\n$query\n" . mysql_error( $us3_link ) );
|
---|
477 | return( -1 );
|
---|
478 | }
|
---|
479 |
|
---|
480 | $id = mysql_insert_id( $us3_link );
|
---|
481 | $file_type = "noise";
|
---|
482 | $noiseIDs[] = $id;
|
---|
483 |
|
---|
484 | // Keep track of modelGUIDs for later, when we replace them
|
---|
485 | $modelGUIDs[ $id ] = $modelGUID;
|
---|
486 | //write_log( "$me: noise file inserted into DB : id=$id" );
|
---|
487 |
|
---|
488 | }
|
---|
489 |
|
---|
490 | else if ( preg_match( "/\.mrecs/", $fn ) > 0 ) // It's an mrecs file
|
---|
491 | {
|
---|
492 | $xml = file_get_contents( $fn );
|
---|
493 | $mrecs_data = parse_xml( $xml, "modelrecords" );
|
---|
494 | $desc = $mrecs_data[ 'description' ];
|
---|
495 | $editGUID = $mrecs_data[ 'editGUID' ];
|
---|
496 | write_log( "$me: mrecs file editGUID=$editGUID" );
|
---|
497 | if ( strlen( $editGUID ) < 36 )
|
---|
498 | $editGUID = "12345678-0123-5678-0123-567890123456";
|
---|
499 | $mrecGUID = $mrecs_data[ 'mrecGUID' ];
|
---|
500 | $modelGUID = $mrecs_data[ 'modelGUID' ];
|
---|
501 |
|
---|
502 | $query = "INSERT INTO pcsa_modelrecs SET " .
|
---|
503 | "editedDataID=" .
|
---|
504 | "(SELECT editedDataID FROM editedData WHERE editGUID='$editGUID')," .
|
---|
505 | "modelID=0, " .
|
---|
506 | "mrecsGUID='$mrecGUID'," .
|
---|
507 | "description='$desc'," .
|
---|
508 | "xml='" . mysql_real_escape_string( $xml, $us3_link ) . "'";
|
---|
509 |
|
---|
510 | // Add later after all files are processed: editDataID, modelID
|
---|
511 |
|
---|
512 | $result = mysql_query( $query, $us3_link );
|
---|
513 |
|
---|
514 | if ( ! $result )
|
---|
515 | {
|
---|
516 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
|
---|
517 | mail_to_user( "fail", "Internal error\n$query\n" . mysql_error( $us3_link ) );
|
---|
518 | return( -1 );
|
---|
519 | }
|
---|
520 |
|
---|
521 | $id = mysql_insert_id( $us3_link );
|
---|
522 | $file_type = "mrecs";
|
---|
523 | $mrecsIDs[] = $id;
|
---|
524 |
|
---|
525 | // Keep track of modelGUIDs for later, when we replace them
|
---|
526 | $rmodlGUIDs[ $id ] = $modelGUID;
|
---|
527 | //write_log( "$me: mrecs file inserted into DB : id=$id" );
|
---|
528 | }
|
---|
529 |
|
---|
530 | else // It's a model file
|
---|
531 | {
|
---|
532 | $xml = file_get_contents( $fn );
|
---|
533 | $model_data = parse_xml( $xml, "model" );
|
---|
534 | $description = $model_data[ 'description' ];
|
---|
535 | $modelGUID = $model_data[ 'modelGUID' ];
|
---|
536 | $editGUID = $model_data[ 'editGUID' ];
|
---|
537 |
|
---|
538 | if ( $mc_iteration > 1 )
|
---|
539 | {
|
---|
540 | //write_log( "$me: MODELUpd: mc_iteration=$mc_iteration" );
|
---|
541 | $miter = sprintf( "_mcN%03d", $mc_iteration );
|
---|
542 | //write_log( "$me: MODELUpd: miter=$miter" );
|
---|
543 | //write_log( "$me: MODELUpd: I:description=$description" );
|
---|
544 | $description = preg_replace( "/_mc[0-9]+/", $miter, $description );
|
---|
545 | write_log( "$me: MODELUpd: O:description=$description" );
|
---|
546 | }
|
---|
547 |
|
---|
548 | $query = "INSERT INTO model SET " .
|
---|
549 | "modelGUID='$modelGUID'," .
|
---|
550 | "editedDataID=" .
|
---|
551 | "(SELECT editedDataID FROM editedData WHERE editGUID='$editGUID')," .
|
---|
552 | "description='$description'," .
|
---|
553 | "MCIteration='$mc_iteration'," .
|
---|
554 | "meniscus='$meniscus'," .
|
---|
555 | "variance='$variance'," .
|
---|
556 | "xml='" . mysql_real_escape_string( $xml, $us3_link ) . "'";
|
---|
557 |
|
---|
558 | $result = mysql_query( $query, $us3_link );
|
---|
559 |
|
---|
560 | if ( ! $result )
|
---|
561 | {
|
---|
562 | write_log( "$me: Bad query:\n$query " . mysql_error( $us3_link ) );
|
---|
563 | mail_to_user( "fail", "Internal error\n$query\n" . mysql_error( $us3_link ) );
|
---|
564 | return( -1 );
|
---|
565 | }
|
---|
566 |
|
---|
567 | $modelID = mysql_insert_id( $us3_link );
|
---|
568 | $id = $modelID;
|
---|
569 | $file_type = "model";
|
---|
570 |
|
---|
571 | $query = "INSERT INTO modelPerson SET " .
|
---|
572 | "modelID=$modelID, personID=$personID";
|
---|
573 | $result = mysql_query( $query, $us3_link );
|
---|
574 | //write_log( "$me: model file inserted into DB : id=$id" );
|
---|
575 | }
|
---|
576 |
|
---|
577 | $query = "INSERT INTO HPCAnalysisResultData SET " .
|
---|
578 | "HPCAnalysisResultID='$HPCAnalysisResultID', " .
|
---|
579 | "HPCAnalysisResultType='$file_type', " .
|
---|
580 | "resultID=$id";
|
---|
581 |
|
---|
582 | $result = mysql_query( $query, $us3_link );
|
---|
583 |
|
---|
584 | if ( ! $result )
|
---|
585 | {
|
---|
586 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
|
---|
587 | mail_to_user( "fail", "Internal error\n$query\n" . mysql_error( $us3_link ) );
|
---|
588 | return( -1 );
|
---|
589 | }
|
---|
590 | //write_log( "$me: ResultData updated : file_type=$file_type" );
|
---|
591 | }
|
---|
592 |
|
---|
593 | // Now fix up noise entries
|
---|
594 | // For noise files, there is, at most two: ti_noise and ri_noise
|
---|
595 | // In this case there will only be one modelID
|
---|
596 |
|
---|
597 | foreach ( $noiseIDs as $noiseID )
|
---|
598 | {
|
---|
599 | $modelGUID = $modelGUIDs[ $noiseID ];
|
---|
600 | $query = "UPDATE noise SET " .
|
---|
601 | "editedDataID=" .
|
---|
602 | "(SELECT editedDataID FROM model WHERE modelGUID='$modelGUID')," .
|
---|
603 | "modelID=" .
|
---|
604 | "(SELECT modelID FROM model WHERE modelGUID='$modelGUID')" .
|
---|
605 | "WHERE noiseID=$noiseID";
|
---|
606 |
|
---|
607 | $result = mysql_query( $query, $us3_link );
|
---|
608 |
|
---|
609 | if ( ! $result )
|
---|
610 | {
|
---|
611 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
|
---|
612 | mail_to_user( "fail", "Bad query\n$query\n" . mysql_error( $us3_link ) );
|
---|
613 | return( -1 );
|
---|
614 | }
|
---|
615 | //write_log( "$me: noise entry updated : noiseID=$noiseID" );
|
---|
616 | }
|
---|
617 | //write_log( "$me: noise entries updated" );
|
---|
618 |
|
---|
619 | // Now possibly fix up mrecs entries
|
---|
620 |
|
---|
621 | foreach ( $mrecsIDs as $mrecsID )
|
---|
622 | {
|
---|
623 | $modelGUID = $rmodlGUIDs[ $mrecsID ];
|
---|
624 | $query = "UPDATE pcsa_modelrecs SET " .
|
---|
625 | "modelID=" .
|
---|
626 | "(SELECT modelID FROM model WHERE modelGUID='$modelGUID')" .
|
---|
627 | "WHERE mrecsID=$mrecsID";
|
---|
628 |
|
---|
629 | $result = mysql_query( $query, $us3_link );
|
---|
630 |
|
---|
631 | if ( ! $result )
|
---|
632 | {
|
---|
633 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
|
---|
634 | mail_to_user( "fail", "Bad query\n$query\n" . mysql_error( $us3_link ) );
|
---|
635 | return( -1 );
|
---|
636 | }
|
---|
637 | //write_log( "$me: mrecs entry updated : mrecsID=$mrecsID" );
|
---|
638 | }
|
---|
639 | //write_log( "$me: mrecs entries updated" );
|
---|
640 |
|
---|
641 | // Copy results to LIMS submit directory (files there are deleted after 7 days)
|
---|
642 | global $submit_dir; // LIMS submit files dir
|
---|
643 |
|
---|
644 | // Get the request guid (LIMS submit dir name)
|
---|
645 | $query = "SELECT HPCAnalysisRequestGUID FROM HPCAnalysisRequest " .
|
---|
646 | "WHERE HPCAnalysisRequestID = $requestID ";
|
---|
647 | $result = mysql_query( $query, $us3_link );
|
---|
648 |
|
---|
649 | if ( ! $result )
|
---|
650 | {
|
---|
651 | write_log( "$me: Bad query:\n$query\n" . mysql_error( $us3_link ) );
|
---|
652 | }
|
---|
653 |
|
---|
654 | list( $requestGUID ) = mysql_fetch_array( $result );
|
---|
655 |
|
---|
656 | chdir( "$submit_dir/$requestGUID" );
|
---|
657 | $f = fopen( "analysis.tar", "w" );
|
---|
658 | fwrite( $f, $tarfile );
|
---|
659 | fclose( $f );
|
---|
660 |
|
---|
661 | // Clean up
|
---|
662 | chdir ( $work );
|
---|
663 | // exec( "rm -rf $gfacID" );
|
---|
664 |
|
---|
665 | mysql_close( $us3_link );
|
---|
666 |
|
---|
667 | /////////
|
---|
668 | // Send email
|
---|
669 |
|
---|
670 | mail_to_user( "success", "" );
|
---|
671 | }
|
---|
672 |
|
---|
673 | ?>
|
---|