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