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