[Bio] / RegSubMa / Job48.pm Repository:
ViewVC logotype

View of /RegSubMa/Job48.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (download) (as text) (annotate) (vendor branch)
Wed Apr 4 17:08:51 2007 UTC (13 years, 3 months ago) by mkubal
Branch: MAIN, fig
CVS Tags: v_1_0_0, HEAD
Changes since 1.1: +0 -0 lines
initial checkin

#
# Module to wrap the lookups of 48-hour server job information.
#

package Job48;

use FIG;
use FIGV;
use GenomeMeta;
use DBMaster;
use Mail::Mailer;

use DirHandle;
use strict;

use FIG_Config;

sub all_jobs
{
    my @jobs;

    my $dh = new DirHandle($FIG_Config::fortyeight_jobs);

    while (defined($_ = $dh->read()))
    {
	next unless /^\d+$/;

	my $job = Job48->new($_);
	if ($job)
	{
	    push(@jobs, $job);
	}
    }
    return sort { $a->id <=> $b->id } @jobs;
}

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

    my $dir = "$FIG_Config::fortyeight_jobs/$job_id";

    return if (! -d $dir);

    my $self = {
	id => $job_id,
	dir => $dir,
    };
    $self = bless $self, $class;
    $self->init();
    return $self;
}

sub init
{
    my($self) = @_;

    my $dir = $self->{dir};

    my $genome = &FIG::file_head("$dir/GENOME_ID", 1);
    chomp $genome;
    $self->{genome_id} = $genome;

    $self->{genome_name} = &FIG::file_head("$dir/GENOME", 1);
    chomp $self->{genome_name};

    $self->{user} = &FIG::file_head("$dir/USER", 1);
    chomp $self->{user};

    $self->{orgdir} = "$dir/rp/$genome";
    $self->{meta} = new GenomeMeta($genome, "$dir/meta.xml");

    $self->{active} = -f "$dir/ACTIVE";
}

sub dir { return $_[0]->{dir}; }
sub id { return $_[0]->{id}; }
sub genome_id { return $_[0]->{genome_id}; }
sub genome_name { return $_[0]->{genome_name}; }
sub meta { return $_[0]->{meta}; }
sub user { return $_[0]->{user}; }
sub active { return $_[0]->{active}; }
sub orgdir { return $_[0]->{orgdir}; }

#
# return the dbmaster user object for the owner of the job
#
sub getUserObject
{
    my($self) = @_;
    my $user = $self->user();

    $ENV{DBHOST} = 'bioseed.mcs.anl.gov';
    my $dbm = DBMaster->new('FortyEight_WebApplication');
    return undef unless $dbm;
	
    my $l = $dbm->User->get_objects({ login => $user });
    if ($l && @$l)
    {
	return $l->[0];
    }
}
	     
sub get_figv
{
    my ($self) = @_;
    return new FIGV($self->orgdir());
}

#
# A job is finished for our purposes when it has
# completed the auto_assign phase.
#
sub finished
{
    my($self) = @_;

    return $self->meta->get_metadata('status.bbhs') eq 'complete';
}

#
# Return a list of contigs. Easiest to find by walking the tbl.
#
sub contigs
{
    my($self) = @_;
    my %contigs;

    my $tbl = $self->orgdir . "/Features/peg/tbl";

    if (open(TBL, "<$tbl"))
    {
	while (<TBL>)
	{
	    chomp;
	    if (/^\S+\t(\S+)_\d+_\d+/)
	    {
		$contigs{$1}++;
	    }
	}
    }
    else
    {
	warn "No $tbl found\n";
    }
    return sort keys %contigs;
}

#
# Send an email message to the owner of the job.
#
# Do so only if the metadata key passed in has not been set to "yes".
#
sub send_email_to_owner
{
    my($self, $key, $subject, $body) = @_;

    my $meta = $self->meta;

    if ($meta->get_metadata($key) ne "yes")
    {
	my $userobj = $self->getUserObject();

	if ($userobj)
	{
	    my $email = $userobj->eMail();
	    my $name = join(" " , $userobj->firstName(), $userobj->lastName());
	    
	    my $full = $name ? "$name <$email>" : $email;
	    warn "send notification email to $full\n";

	    eval {
		my $mail = Mail::Mailer->new();
		$mail->open({
		    To => $full,
		    From => 'Annotation Server <48@mcs.anl.gov>',
		    Subject => $subject,
		});
		
		print $mail $body;
		$mail->close();
		$meta->set_metadata($key, "yes");
		$meta->set_metadata("${key}_address", $email);
		$meta->set_metadata("${key}_timestamp", time);
	    };

	    if ($@)
	    {
		warn "Error sending mail to $full: $@\n";
		return 0;
	    }
	    else
	    {
		return 1;
	    }
	}
    }
    return 0;
}

1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3