CSC334 getXGridOutput.pl

From CSclasswiki
Jump to: navigation, search

--D. Thiebaut 04:15, 4 November 2008 (UTC)


#! /usr/bin/perl
# getXGridOutput.pl
# D. Thiebaut
#
# this program should be piped after the command xgrid when submitting an 
# asynchronous job.
# the program gets the jobIdentifier of the job that was just submitted to
# the XGrid and keeps polling the grid until the job is finished.  Then
# it gets the results and spits them back on the screen.

#---------------------------------------------------------------------------
# getOutput: grabs the lines from the standard input and returns them
#             as an array of lines
#---------------------------------------------------------------------------
sub getOutput {
    my @lines = ( <> );
    #foreach ( @lines ) {
    #	print $_;
    #}
    return @lines;
}

#---------------------------------------------------------------------------
# getJobId: gets an array of lines, finds the one containing "jobIdentifier"
#           and extract the jobId from it.  Retuns the jobId.
#---------------------------------------------------------------------------
sub getJobId {
    my $jobId;
    foreach ( @_ ) {
	if ( $_ =~ "jobIdentifier" ) {
	    $_ =~ m{\D*(\d*)\D*}ig;
	    $jobId = $1;
	}
    }
    return $jobId;
}

#---------------------------------------------------------------------------
# getStatusOfJob: receives a job Id and an array of lines, and extracts the 
#                 status of the job.  Returns 1 if finished, 0 otherwise.
#---------------------------------------------------------------------------
sub getStatusOfJob {
    my ( $jobId, @lines ) = @_;
    foreach ( @lines ) {
	if ( $_ =~ "jobStatus" ) {
	    if ( $_ =~ "Finished" ) {
		return 1;
	    }
	    else {
		return 0;
	    }
	}
    }
}

#---------------------------------------------------------------------------
# The main program: 
#   1. get the job identifier
#   2. poll the job attributes until the job is finished
#   3. get the results and print them out
#   4. delete the job from the grid
#---------------------------------------------------------------------------
sub main {
    my $jobId = getJobId( getOutput() );
    print "jobId = $jobId\n";

    #--- loop until job is finished ---
    while ( 1 ) {
	my $status = getStatusOfJob( $jobId,  `xgrid -id $jobId -job attributes` );
	print "status = $status\n";
	#--- wait 100 ms ---
	select(undef,undef,undef,.1);
	last if ( $status == 1 );
    }
    
    #--- get the results ---
    my @results = `xgrid -id $jobId -job results`;
    foreach ( @results ) {
	print $_;
    }

    #--- remove the job from the XGrid ---
    `xgrid -id $jobId -job delete`;
}


main();