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/lims/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 $endtime;
|
---|
36 | global $status;
|
---|
37 | global $stdout;
|
---|
38 | global $requestID;
|
---|
39 |
|
---|
40 | $requestID = $reqID;
|
---|
41 | $db = $us3_db;
|
---|
42 | write_log( "$me: debug db=$db; requestID=$requestID" );
|
---|
43 |
|
---|
44 | $us3_link = mysqli_connect( $dbhost, $user, $passwd, $db );
|
---|
45 |
|
---|
46 | if ( ! $us3_link )
|
---|
47 | {
|
---|
48 | write_log( "$me: could not connect: $dbhost, $user, $passwd, $db" );
|
---|
49 | mail_to_user( "fail", "Internal Error $requestID\nCould not connect to DB $db" );
|
---|
50 | return( -1 );
|
---|
51 | }
|
---|
52 |
|
---|
53 | // First get basic info for email messages
|
---|
54 | $query = "SELECT email, investigatorGUID, editXMLFilename FROM HPCAnalysisRequest " .
|
---|
55 | "WHERE HPCAnalysisRequestID=$requestID";
|
---|
56 | $result = mysqli_query( $us3_link, $query );
|
---|
57 |
|
---|
58 | if ( ! $result )
|
---|
59 | {
|
---|
60 | write_log( "$me: Bad query: $query" );
|
---|
61 | mail_to_user( "fail", "Internal Error $requestID\n$query\n" . mysqli_error( $us3_link ) );
|
---|
62 | return( -1 );
|
---|
63 | }
|
---|
64 |
|
---|
65 | list( $email_address, $investigatorGUID, $editXMLFilename ) = mysqli_fetch_array( $result );
|
---|
66 |
|
---|
67 | $query = "SELECT personID FROM people " .
|
---|
68 | "WHERE personGUID='$investigatorGUID'";
|
---|
69 | $result = mysqli_query( $us3_link, $query );
|
---|
70 |
|
---|
71 | list( $personID ) = mysqli_fetch_array( $result );
|
---|
72 |
|
---|
73 | /*
|
---|
74 | $query = "SELECT clusterName, submitTime, queueStatus, method " .
|
---|
75 | "FROM HPCAnalysisRequest h LEFT JOIN HPCAnalysisResult " .
|
---|
76 | "ON h.HPCAnalysisRequestID=HPCAnalysisResult.HPCAnalysisRequestID " .
|
---|
77 | "WHERE h.HPCAnalysisRequestID=$requestID";
|
---|
78 | */
|
---|
79 | $query = "SELECT clusterName, submitTime, queueStatus, analType " .
|
---|
80 | "FROM HPCAnalysisRequest h, HPCAnalysisResult r " .
|
---|
81 | "WHERE h.HPCAnalysisRequestID=$requestID " .
|
---|
82 | "AND h.HPCAnalysisRequestID=r.HPCAnalysisRequestID";
|
---|
83 |
|
---|
84 | $result = mysqli_query( $us3_link, $query );
|
---|
85 |
|
---|
86 | if ( ! $result )
|
---|
87 | {
|
---|
88 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
89 | return( -1 );
|
---|
90 | }
|
---|
91 |
|
---|
92 | if ( mysqli_num_rows( $result ) == 0 )
|
---|
93 | {
|
---|
94 | write_log( "$me: US3 Table error - No records for requestID: $requestID" );
|
---|
95 | return( -1 );
|
---|
96 | }
|
---|
97 |
|
---|
98 | list( $cluster, $submittime, $queuestatus, $jobtype ) = mysqli_fetch_array( $result );
|
---|
99 |
|
---|
100 | // Get the GFAC ID
|
---|
101 | $query = "SELECT HPCAnalysisResultID, gfacID, endTime FROM HPCAnalysisResult " .
|
---|
102 | "WHERE HPCAnalysisRequestID=$requestID";
|
---|
103 |
|
---|
104 | $result = mysqli_query( $us3_link, $query );
|
---|
105 |
|
---|
106 | if ( ! $result )
|
---|
107 | {
|
---|
108 | write_log( "$me: Bad query: $query" );
|
---|
109 | mail_to_user( "fail", "Internal Error $requestID\n$query\n" . mysqli_error( $us3_link ) );
|
---|
110 | return( -1 );
|
---|
111 | }
|
---|
112 |
|
---|
113 | list( $HPCAnalysisResultID, $gfacID, $endtime ) = mysqli_fetch_array( $result );
|
---|
114 |
|
---|
115 | ////////
|
---|
116 | // Get data from global GFAC DB and insert it into US3 DB
|
---|
117 | $gfac_link = mysqli_connect( $dbhost, $guser, $gpasswd, $gDB );
|
---|
118 |
|
---|
119 | if ( ! $gfac_link )
|
---|
120 | {
|
---|
121 | write_log( "$me: Could not connect to DB $dbhost : $gDB" );
|
---|
122 | mail_to_user( "fail", "Internal Error $requestID\nCould not connect to DB $gDB" );
|
---|
123 | return( -1 );
|
---|
124 | }
|
---|
125 |
|
---|
126 | $query = "SELECT status, cluster, id FROM analysis " .
|
---|
127 | "WHERE gfacID='$gfacID'";
|
---|
128 |
|
---|
129 | $result = mysqli_query( $gfac_link, $query );
|
---|
130 | if ( ! $result )
|
---|
131 | {
|
---|
132 | write_log( "$me: Could not select GFAC status for $gfacID" );
|
---|
133 | mail_to_user( "fail", "Could not select GFAC status for $gfacID" );
|
---|
134 | return( -1 );
|
---|
135 | }
|
---|
136 |
|
---|
137 | $num_rows = mysqli_num_rows( $result );
|
---|
138 | if ( $num_rows == 0 )
|
---|
139 | {
|
---|
140 | write_log( "$me: Cleanup analysis query found 0 entries for $gfacID" );
|
---|
141 | return( 0 );
|
---|
142 | }
|
---|
143 | //else
|
---|
144 | //{
|
---|
145 | //write_log( "$me: db=$db; num_rows=$num_rows; queuestatus=$queuestatus" );
|
---|
146 | //}
|
---|
147 |
|
---|
148 | list( $status, $cluster, $id ) = mysqli_fetch_array( $result );
|
---|
149 | //write_log( "$me: db=$db; requestID=$requestID; status=$status; cluster=$cluster" );
|
---|
150 |
|
---|
151 | // if ( $cluster == 'bcf-local' || $cluster == 'alamo-local' )
|
---|
152 | if ( preg_match( "/\-local/", $cluster ) ||
|
---|
153 | preg_match( "/us3iab/", $cluster ) )
|
---|
154 | {
|
---|
155 | // $clushost = $cluster;
|
---|
156 | // $clushost = preg_replace( "/\-local/", "", $clushost );
|
---|
157 | $parts = explode( "-", $cluster );
|
---|
158 | $clushost = $parts[ 0 ];
|
---|
159 | get_local_files( $gfac_link, $clushost, $requestID, $id, $gfacID );
|
---|
160 | write_log( "$me: clushost=$clushost reqID=$requestID get_local_files() gfacID=$gfacID" );
|
---|
161 | }
|
---|
162 | else
|
---|
163 | write_log( "$me: NO get_local_files()" );
|
---|
164 |
|
---|
165 | $query = "SELECT id, stderr, stdout, tarfile FROM analysis " .
|
---|
166 | "WHERE gfacID='$gfacID'";
|
---|
167 |
|
---|
168 | $result = mysqli_query( $gfac_link, $query );
|
---|
169 |
|
---|
170 | if ( ! $result )
|
---|
171 | {
|
---|
172 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $gfac_link ) );
|
---|
173 | mail_to_user( "fail", "Internal error " . mysqli_error( $gfac_link ) );
|
---|
174 | return( -1 );
|
---|
175 | }
|
---|
176 |
|
---|
177 | $num_rows = mysqli_num_rows( $result );
|
---|
178 | if ( $num_rows == 0 )
|
---|
179 | {
|
---|
180 | write_log( "$me: Cleanup analysis query found 0 entries for $gfacID" );
|
---|
181 | return( 0 );
|
---|
182 | }
|
---|
183 |
|
---|
184 | list( $analysisID, $stderr, $stdout, $tarfile ) = mysqli_fetch_array( $result );
|
---|
185 |
|
---|
186 | if ( strlen( $tarfile ) > 0 )
|
---|
187 | { // Log success at fetch attempt
|
---|
188 | write_log( "$me: Successful data fetch: $requestID $gfacID" );
|
---|
189 | }
|
---|
190 | else
|
---|
191 | { // Log failure at fetch attempt
|
---|
192 | write_log( "$me: Failed data fetch: $requestID $gfacID" );
|
---|
193 | if ( $analysisID == '' )
|
---|
194 | $analysisID = '0';
|
---|
195 | }
|
---|
196 |
|
---|
197 | // Save queue messages for post-mortem analysis
|
---|
198 | $query = "SELECT message, time FROM queue_messages " .
|
---|
199 | "WHERE analysisID = $analysisID " .
|
---|
200 | "ORDER BY time ";
|
---|
201 | $result = mysqli_query( $gfac_link, $query );
|
---|
202 |
|
---|
203 | if ( ! $result )
|
---|
204 | {
|
---|
205 | // Just log it and continue
|
---|
206 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $gfac_link ) );
|
---|
207 | }
|
---|
208 |
|
---|
209 | $now = date( 'Y-m-d H:i:s' );
|
---|
210 | $message_log = "US3 DB: $db\n" .
|
---|
211 | "RequestID: $requestID\n" .
|
---|
212 | "GFAC ID: $gfacID\n" .
|
---|
213 | "Processed: $now\n\n" .
|
---|
214 | "Queue Messages\n\n" ;
|
---|
215 |
|
---|
216 | $need_finish = ( $status == 'COMPLETE' );
|
---|
217 |
|
---|
218 | if ( mysqli_num_rows( $result ) > 0 )
|
---|
219 | {
|
---|
220 | $time_msg = time();
|
---|
221 | while ( list( $message, $time ) = mysqli_fetch_array( $result ) )
|
---|
222 | {
|
---|
223 | //write_log( "$me: message=$message" );
|
---|
224 | $message_log .= "$time $message\n";
|
---|
225 | if ( preg_match( "/^Finished/i", $message ) )
|
---|
226 | $need_finish = false;
|
---|
227 | $time_msg = strtotime( $time );
|
---|
228 | }
|
---|
229 |
|
---|
230 | if ( $need_finish )
|
---|
231 | { // No 'Finished' yet: forget if too much time has passed
|
---|
232 | $time_now = time();
|
---|
233 | $tdelta = $time_now - $time_msg;
|
---|
234 | write_log( "$me: no-Finish time: tnow=$time_now, tmsg=$time_msg, tdelt=$tdelta" );
|
---|
235 | if ( $tdelta > 600 )
|
---|
236 | $need_finish = false;
|
---|
237 | }
|
---|
238 | //write_log( "$me: no-Finish time: tnow=$time_now, tmsg=$time_msg, tdelt=$tdelta" );
|
---|
239 | }
|
---|
240 | else
|
---|
241 | {
|
---|
242 | write_log( "$me: No messages for analysisID=$analysisID ." );
|
---|
243 | $need_finish = false;
|
---|
244 | }
|
---|
245 |
|
---|
246 | if ( $need_finish )
|
---|
247 | {
|
---|
248 | write_log( "$me: Cleanup has not yet found 'Finished' for $gfacID" );
|
---|
249 | return( 0 );
|
---|
250 | }
|
---|
251 |
|
---|
252 | $query = "DELETE FROM queue_messages " .
|
---|
253 | "WHERE analysisID = $analysisID ";
|
---|
254 |
|
---|
255 | $result = mysqli_query( $gfac_link, $query );
|
---|
256 |
|
---|
257 | if ( ! $result )
|
---|
258 | {
|
---|
259 | // Just log it and continue
|
---|
260 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $gfac_link ) );
|
---|
261 | }
|
---|
262 |
|
---|
263 | // Save stdout, stderr, etc. for message log
|
---|
264 | $query = "SELECT stdout, stderr, status, queue_msg FROM analysis " .
|
---|
265 | "WHERE gfacID='$gfacID' ";
|
---|
266 | $result = mysqli_query( $gfac_link, $query );
|
---|
267 | try
|
---|
268 | {
|
---|
269 | // What if this is too large?
|
---|
270 | list( $stdout, $stderr, $status, $queue_msg ) = mysqli_fetch_array( $result );
|
---|
271 | }
|
---|
272 | catch ( Exception $e )
|
---|
273 | {
|
---|
274 | write_log( "$me: stdout + stderr larger than 128M - $gfacID\n" . mysqli_error( $gfac_link ) );
|
---|
275 | // Just go ahead and clean up
|
---|
276 | }
|
---|
277 |
|
---|
278 | // But let's allow for investigation of other large stdout and/or stderr
|
---|
279 | if ( strlen( $stdout ) > 20480000 ||
|
---|
280 | strlen( $stderr ) > 20480000 )
|
---|
281 | write_log( "$me: stdout + stderr larger than 20M - $gfacID\n" );
|
---|
282 |
|
---|
283 | $message_log .= "\n\n\nStdout Contents\n\n" .
|
---|
284 | $stdout .
|
---|
285 | "\n\n\nStderr Contents\n\n" .
|
---|
286 | $stderr .
|
---|
287 | "\n\n\nGFAC Status: $status\n" .
|
---|
288 | "GFAC message field: $queue_msg\n";
|
---|
289 |
|
---|
290 | // Delete data from GFAC DB
|
---|
291 | $query = "DELETE from analysis WHERE gfacID='$gfacID'";
|
---|
292 |
|
---|
293 | $result = mysqli_query( $gfac_link, $query );
|
---|
294 |
|
---|
295 | if ( ! $result )
|
---|
296 | {
|
---|
297 | // Just log it and continue
|
---|
298 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $gfac_link ) );
|
---|
299 | }
|
---|
300 | write_log( "$me: GFAC DB entry deleted" );
|
---|
301 |
|
---|
302 | // Copy queue messages to LIMS submit directory (files there are deleted after 7 days)
|
---|
303 | global $submit_dir;
|
---|
304 |
|
---|
305 | // Get the request guid (LIMS submit dir name)
|
---|
306 | $query = "SELECT HPCAnalysisRequestGUID FROM HPCAnalysisRequest " .
|
---|
307 | "WHERE HPCAnalysisRequestID = $requestID ";
|
---|
308 | $result = mysqli_query( $us3_link, $query );
|
---|
309 |
|
---|
310 | if ( ! $result )
|
---|
311 | {
|
---|
312 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
313 | }
|
---|
314 |
|
---|
315 | list( $requestGUID ) = mysqli_fetch_array( $result );
|
---|
316 | $output_dir = "$submit_dir/$requestGUID";
|
---|
317 | write_log( "$me: Output dir determined: $output_dir" );
|
---|
318 |
|
---|
319 | // Try to create it if necessary, and write the file
|
---|
320 | // Let's use FILE_APPEND, in case this is the second time around and the
|
---|
321 | // GFAC job status was INSERTed, rather than UPDATEd
|
---|
322 | if ( ! is_dir( $output_dir ) )
|
---|
323 | mkdir( $output_dir, 0775, true );
|
---|
324 | $message_filename = "$output_dir/$db-$requestID-messages.txt";
|
---|
325 | file_put_contents( $message_filename, $message_log, FILE_APPEND );
|
---|
326 | // mysqli_close( $gfac_link );
|
---|
327 | write_log( "$me: *messages.txt written" );
|
---|
328 |
|
---|
329 | /////////
|
---|
330 | // Insert data into HPCAnalysis
|
---|
331 |
|
---|
332 | $query = "UPDATE HPCAnalysisResult SET " .
|
---|
333 | "stderr='" . mysqli_real_escape_string( $us3_link, $stderr ) . "', " .
|
---|
334 | "stdout='" . mysqli_real_escape_string( $us3_link, $stdout ) . "' " .
|
---|
335 | "WHERE HPCAnalysisResultID=$HPCAnalysisResultID";
|
---|
336 |
|
---|
337 | $result = mysqli_query( $us3_link, $query );
|
---|
338 |
|
---|
339 | if ( ! $result )
|
---|
340 | {
|
---|
341 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
342 | mail_to_user( "fail", "Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
343 | return( -1 );
|
---|
344 | }
|
---|
345 |
|
---|
346 | // Save the tarfile and expand it
|
---|
347 |
|
---|
348 | if ( strlen( $tarfile ) == 0 )
|
---|
349 | {
|
---|
350 | write_log( "$me: No tarfile" );
|
---|
351 | mail_to_user( "fail", "No results" );
|
---|
352 | return( -1 );
|
---|
353 | }
|
---|
354 |
|
---|
355 | // Shouldn't happen
|
---|
356 | if ( ! is_dir( "$work" ) )
|
---|
357 | {
|
---|
358 | write_log( "$me: $work directory does not exist" );
|
---|
359 | mail_to_user( "fail", "$work directory does not exist" );
|
---|
360 | return( -1 );
|
---|
361 | }
|
---|
362 |
|
---|
363 | if ( ! is_dir( "$work/$gfacID" ) ) mkdir( "$work/$gfacID", 0770 );
|
---|
364 | chdir( "$work/$gfacID" );
|
---|
365 |
|
---|
366 | $f = fopen( "analysis-results.tar", "w" );
|
---|
367 | fwrite( $f, $tarfile );
|
---|
368 | fclose( $f );
|
---|
369 | //write_log( "$me: analysis-results.tar file written to work dir" );
|
---|
370 |
|
---|
371 | $tar_out = array();
|
---|
372 | exec( "tar -xf analysis-results.tar 2>&1", $tar_out, $err );
|
---|
373 |
|
---|
374 | if ( $err != 0 )
|
---|
375 | {
|
---|
376 | chdir( $work );
|
---|
377 | exec( "rm -r $gfacID" );
|
---|
378 | $output = implode( "\n", $tar_out );
|
---|
379 |
|
---|
380 | write_log( "$me: Bad output tarfile: $output" );
|
---|
381 | mail_to_user( "fail", "Bad output file" );
|
---|
382 | return( -1 );
|
---|
383 | }
|
---|
384 | //write_log( "$me: tar files extracted" );
|
---|
385 |
|
---|
386 | // Insert the model files and noise files
|
---|
387 | $files = file( "analysis_files.txt", FILE_IGNORE_NEW_LINES );
|
---|
388 | $noiseIDs = array();
|
---|
389 | $modelGUIDs = array();
|
---|
390 | $mrecsIDs = array();
|
---|
391 | $rmodlGUIDs = array();
|
---|
392 |
|
---|
393 | foreach ( $files as $file )
|
---|
394 | {
|
---|
395 | $split = explode( ";", $file );
|
---|
396 |
|
---|
397 | if ( count( $split ) > 1 )
|
---|
398 | {
|
---|
399 | list( $fn, $meniscus, $mc_iteration, $variance ) = explode( ";", $file );
|
---|
400 |
|
---|
401 | list( $other, $mc_iteration ) = explode( "=", $mc_iteration );
|
---|
402 | list( $other, $variance ) = explode( "=", $variance );
|
---|
403 | list( $other, $meniscus ) = explode( "=", $meniscus );
|
---|
404 | }
|
---|
405 | else
|
---|
406 | $fn = $file;
|
---|
407 |
|
---|
408 | if ( preg_match( "/mdl.tmp$/", $fn ) )
|
---|
409 | continue;
|
---|
410 |
|
---|
411 | if ( filesize( $fn ) < 100 )
|
---|
412 | {
|
---|
413 | write_log( "$me:fn is invalid $fn" );
|
---|
414 | mail_to_user( "fail", "Internal error\n$fn is invalid" );
|
---|
415 | return( -1 );
|
---|
416 | }
|
---|
417 | //write_log( "$me: handling file: $fn" );
|
---|
418 |
|
---|
419 | if ( preg_match( "/^job_statistics\.xml$/", $fn ) ) // Job statistics file
|
---|
420 | {
|
---|
421 | $xml = file_get_contents( $fn );
|
---|
422 | $statistics = parse_xml( $xml, 'statistics' );
|
---|
423 | $otherdata = parse_xml( $xml, 'id' );
|
---|
424 |
|
---|
425 | $query = "UPDATE HPCAnalysisResult SET " .
|
---|
426 | "wallTime = {$statistics['walltime']}, " .
|
---|
427 | "CPUTime = {$statistics['cputime']}, " .
|
---|
428 | "CPUCount = {$statistics['cpucount']}, " .
|
---|
429 | "max_rss = {$statistics['maxmemory']}, " .
|
---|
430 | "startTime = '{$otherdata['starttime']}', " .
|
---|
431 | "endTime = '{$otherdata['endtime']}', " .
|
---|
432 | "mgroupcount = {$otherdata['groupcount']} " .
|
---|
433 | "WHERE HPCAnalysisResultID=$HPCAnalysisResultID";
|
---|
434 | $result = mysqli_query( $us3_link, $query );
|
---|
435 |
|
---|
436 | if ( ! $result )
|
---|
437 | {
|
---|
438 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
439 | }
|
---|
440 |
|
---|
441 | file_put_contents( "$output_dir/$fn", $xml ); // Copy to submit dir
|
---|
442 | $file_type = "job_stats";
|
---|
443 | //write_log( "$me: job_statistics file updated in Result and written" );
|
---|
444 |
|
---|
445 | }
|
---|
446 |
|
---|
447 | else if ( preg_match( "/\.noise/", $fn ) > 0 ) // It's a noise file
|
---|
448 | {
|
---|
449 | $xml = file_get_contents( $fn );
|
---|
450 | $noise_data = parse_xml( $xml, "noise" );
|
---|
451 | $type = ( $noise_data[ 'type' ] == "ri" ) ? "ri_noise" : "ti_noise";
|
---|
452 | $desc = $noise_data[ 'description' ];
|
---|
453 | $modelGUID = $noise_data[ 'modelGUID' ];
|
---|
454 | $noiseGUID = $noise_data[ 'noiseGUID' ];
|
---|
455 | $editGUID = '00000000-0000-0000-0000-000000000000';
|
---|
456 | if ( isset( $model_data[ 'editGUID' ] ) )
|
---|
457 | $editGUID = $model_data[ 'editGUID' ];
|
---|
458 |
|
---|
459 | $query = "INSERT INTO noise SET " .
|
---|
460 | "noiseGUID='$noiseGUID'," .
|
---|
461 | "modelGUID='$modelGUID'," .
|
---|
462 | "editedDataID=" .
|
---|
463 | "(SELECT editedDataID FROM editedData WHERE editGUID='$editGUID')," .
|
---|
464 | "modelID=1, " .
|
---|
465 | "noiseType='$type'," .
|
---|
466 | "description='$desc'," .
|
---|
467 | "xml='" . mysqli_real_escape_string( $us3_link, $xml ) . "'";
|
---|
468 |
|
---|
469 | // Add later after all files are processed: editDataID, modelID
|
---|
470 |
|
---|
471 | $result = mysqli_query( $us3_link, $query );
|
---|
472 |
|
---|
473 | if ( ! $result )
|
---|
474 | {
|
---|
475 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
476 | mail_to_user( "fail", "Internal error\n$query\n" . mysqli_error( $us3_link ) );
|
---|
477 | return( -1 );
|
---|
478 | }
|
---|
479 |
|
---|
480 | $id = mysqli_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 modelGUID=$modelGUID" );
|
---|
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='" . mysqli_real_escape_string( $us3_link, $xml ) . "'";
|
---|
509 |
|
---|
510 | // Add later after all files are processed: editDataID, modelID
|
---|
511 |
|
---|
512 | $result = mysqli_query( $us3_link, $query );
|
---|
513 |
|
---|
514 | if ( ! $result )
|
---|
515 | {
|
---|
516 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
517 | mail_to_user( "fail", "Internal error\n$query\n" . mysqli_error( $us3_link ) );
|
---|
518 | return( -1 );
|
---|
519 | }
|
---|
520 |
|
---|
521 | $id = mysqli_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 if ( preg_match( "/\.model/", $fn ) > 0 ) // 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='" . mysqli_real_escape_string( $us3_link, $xml ) . "'";
|
---|
557 |
|
---|
558 | $result = mysqli_query( $us3_link, $query );
|
---|
559 |
|
---|
560 | if ( ! $result )
|
---|
561 | {
|
---|
562 | write_log( "$me: Bad query:\n$query " . mysqli_error( $us3_link ) );
|
---|
563 | mail_to_user( "fail", "Internal error\n$query\n" . mysqli_error( $us3_link ) );
|
---|
564 | return( -1 );
|
---|
565 | }
|
---|
566 |
|
---|
567 | $modelID = mysqli_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 = mysqli_query( $us3_link, $query );
|
---|
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 = mysqli_query( $us3_link, $query );
|
---|
583 |
|
---|
584 | if ( ! $result )
|
---|
585 | {
|
---|
586 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
587 | mail_to_user( "fail", "Internal error\n$query\n" . mysqli_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 = mysqli_query( $us3_link, $query );
|
---|
608 |
|
---|
609 | if ( ! $result )
|
---|
610 | {
|
---|
611 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
612 | mail_to_user( "fail", "Bad query\n$query\n" . mysqli_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 = mysqli_query( $us3_link, $query );
|
---|
630 |
|
---|
631 | if ( ! $result )
|
---|
632 | {
|
---|
633 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
634 | mail_to_user( "fail", "Bad query\n$query\n" . mysqli_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 = mysqli_query( $us3_link, $query );
|
---|
648 |
|
---|
649 | if ( ! $result )
|
---|
650 | {
|
---|
651 | write_log( "$me: Bad query:\n$query\n" . mysqli_error( $us3_link ) );
|
---|
652 | }
|
---|
653 |
|
---|
654 | list( $requestGUID ) = mysqli_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 | mysqli_close( $us3_link );
|
---|
666 |
|
---|
667 | /////////
|
---|
668 | // Send email
|
---|
669 |
|
---|
670 | mail_to_user( "success", "" );
|
---|
671 | }
|
---|
672 | ?>
|
---|