CSC352 getXGridOutput.pl

From dftwiki3
Revision as of 19:11, 15 February 2010 by Thiebaut (talk | contribs) (Created page with '--~~~~ ---- <source lang="python"> #! /usr/bin/perl # getXGridOutput.pl # D. Thiebaut # # this program should be piped after the command xgrid when submitting an # asynchronous …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 23:11, 15 February 2010 (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();