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