[Bio] / ClusterTools / joblib.pm Repository:
ViewVC logotype

View of /ClusterTools/joblib.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (download) (as text) (annotate) (vendor branch)
Fri Oct 22 19:45:53 2004 UTC (15 years, 1 month ago) by olson
Branch: foo, MAIN
CVS Tags: bob, HEAD
Changes since 1.1: +0 -0 lines
New cluster stuff.

package joblib;

#
# A package of routines to aid in the setup and execution of remote computational jobs.
#
# These make SOAP calls against the job manager server.
#

use strict;

use Data::Dumper;
use SOAP::Lite;

our $namespace = "http://www.mcs.anl.gov/fl/research/SEED/schemas/jobmgr";
our $default_jobmgr = "http://www-unix.mcs.anl.gov/~olson/SEED/jobmgr.cgi";

package joblib::JobManagerClient;

use HTTP::Request::Common;
use LWP::UserAgent;

use File::Basename;

use strict;

use Data::Dumper;
use SOAP::Lite;

sub new
{
    my($class, $url) = @_;

    defined($url) or $url = $joblib::default_jobmgr;

    print "Using $url\n";

    my $proxy = SOAP::Lite->uri($joblib::namespace)->proxy($url);
    
    my $self = {
	url => $url,
	proxy  => $proxy,
    };

    return bless($self, $class);
}

sub job_create
{
    my($self, $email, $type, $name) = @_;

    my $reply = $self->call("job_create", $email, $type, $name);

    if ($reply)
    {
	return $reply;
    }
    else
    {
	return undef;
    }
}

sub job_add_file
{
    my($self, $job_id, $local_file, $remote_name) = @_;

    #
    # add a new file.
    #
    # Invoke the soap call, which will return a file upload url.
    #

    if (! -f $local_file)
    {
	die "Local file $local_file does not exist";
    }

    if ($remote_name eq '')
    {
	$remote_name = basename($local_file);
    }
    elsif ($remote_name =~ m,/,)
    {
	die "Remote  name not (yet) allowed to contain /";
    }

    if ($job_id !~ /^\d+/)
    {
	die "Invalid job_id '$job_id'\n";
    }

    my $size = -s $local_file;
    
    my $reply = $self->call("job_add_file", $job_id,  $remote_name, $size);

    if ($reply)
    {
	my ($file_id, $upload_url) = @$reply;

	$HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 1;

	print "Uploading file...\n";
	my $req = POST($upload_url,
		       Content_Type => 'form-data',
		       Content => [job_id => $job_id,
				   file_id => $file_id,
				   file => [$local_file]]);
	my $ua = new LWP::UserAgent;

	my $resp = $ua->request($req);

	if ($resp->is_success)
	{
	    print "Successful upload:\n";
	    print $resp->content;
	}
	else
	{
	    print "Failed upload\n";
	    print $resp->error_as_HTML();
	}
    }
}

sub job_set_params
{
    my($self, $job_id, $params_file) = @_;

    #
    # add a parameters file.
    #
    # Invoke the soap call, which will return a file upload url.
    #

    if (! -f $params_file)
    {
	die "Local file $params_file does not exist";
    }

    if ($job_id !~ /^\d+/)
    {
	die "Invalid job_id '$job_id'\n";
    }

    my $size = -s $params_file;
    
    my $reply = $self->call("job_set_params", $job_id, $size);

    if ($reply)
    {
	my ($upload_url) = @$reply;

	$HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 1;

	print "Uploading file...\n";
	my $req = POST($upload_url,
		       Content_Type => 'form-data',
		       Content => [job_id => $job_id,
				   params_file => 1,
				   file => [$params_file]]);
	my $ua = new LWP::UserAgent;

	my $resp = $ua->request($req);

	if ($resp->is_success)
	{
	    print "Successful upload:\n";
	    print $resp->content;
	}
	else
	{
	    print "Failed upload\n";
	    print $resp->error_as_HTML();
	}
    }
}

sub job_run
{
    my($self, $job_id) = @_;

    if ($job_id !~ /^\d+/)
    {
	die "Invalid job_id '$job_id'\n";
    }

    my $reply = $self->call("job_run", $job_id);

    if ($reply)
    {
	return $reply;	
    }
}

sub job_get_status
{
    my($self, $job_id) = @_;

    if ($job_id !~ /^\d+/)
    {
	die "Invalid job_id '$job_id'\n";
    }

    my $reply = $self->call("job_get_status", $job_id);

    if ($reply)
    {
	return $reply;	
    }
}

sub job_get_output
{
    my($self, $job_id) = @_;

    if ($job_id !~ /^\d+/)
    {
	die "Invalid job_id '$job_id'\n";
    }

    my $reply = $self->call("job_get_output", $job_id);

    if ($reply)
    {
	return $reply;	
    }
}


sub call
{
    my($self, $method, @args) = @_;

    my $reply = $self->{proxy}->$method(@args);

    if ($reply->fault)
    {
	die "$method failed: ", $reply->faultcode, " ", $reply->faultstring;
    }
    return $reply->result;
}

1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3