[Bio] / FortyEightMeta / SimStatusDB.pm Repository:
ViewVC logotype

View of /FortyEightMeta/SimStatusDB.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (as text) (annotate)
Fri Jun 13 01:58:12 2008 UTC (11 years, 8 months ago) by olson
Branch: MAIN
CVS Tags: mgrast_dev_08112011, mgrast_dev_08022011, mgrast_dev_05262011, mgrast_dev_04082011, mgrast_version_3_2, mgrast_dev_12152011, mgrast_dev_06072011, mgrast_rel_2008_0806, mgrast_dev_10262011, mgrast_dev_02212011, mgrast_rel_2008_0923, mgrast_release_3_0, mgrast_dev_03252011, mgrast_rel_2008_0924, mgrast_rel_2008_1110_v2, mgrast_rel_2008_0625, mgrast_release_3_0_4, mgrast_release_3_0_2, mgrast_release_3_0_3, mgrast_release_3_0_1, mgrast_dev_03312011, mgrast_release_3_1_2, mgrast_release_3_1_1, mgrast_release_3_1_0, mgrast_dev_04132011, mgrast_dev_04012011, mgrast_rel_2008_0919, mgrast_rel_2008_1110, myrast_33, mgrast_rel_2008_0917, mgrast_dev_04052011, mgrast_dev_02222011, HEAD
Changes since 1.2: +44 -18 lines
Add connect/disconnect to SimStatusDB. Disconnect while sims run.

package FortyEightMeta::SimStatusDB;

use FIG_Config;
use strict;
use FileLocking qw(lock_file unlock_file);
use FileHandle;
use Data::Dumper;
use DBrtns;
use POSIX;

use base 'Class::Accessor';

__PACKAGE__->mk_accessors(qw(dbh mgdb job_id table retry_log_table));

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

    my $self = {
	job_id => $job_id,
	table => "sim_status_$job_id",
	retry_log_table => "sim_status_retry_$job_id",
    };

    bless($self, $class);

    if ($mgdb)
    {
	my $dbh = $mgdb->{_dbh};
	
	$self->dbh($dbh);
	$self->mgdb($mgdb);
    }
    else
    {
	$mgdb = $self->connect();
    }

    return $self;
}

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

    my $mgdb = eval { new DBrtns($FIG_Config::mgrast_dbms, $FIG_Config::mgrast_db,
			      $FIG_Config::mgrast_dbuser, $FIG_Config::mgrast_dbpass,
			      $FIG_Config::mgrast_dbport, $FIG_Config::mgrast_dbhost,
			      $FIG_Config::mgrast_dbsock);
	       };
    if (!defined($mgdb))
    {
	die "cannot connect to database: $@";
    }
    $self->mgdb($mgdb);
    $self->dbh($mgdb->{_dbh});

    return $mgdb;
}

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

    $self->dbh->disconnect();
    $self->mgdb(undef);
    $self->dbh(undef);
}
    

sub make_tables
{
    my($self) = @_;
    $self->mgdb->drop_table(tbl => $self->table);
    $self->mgdb->create_table(tbl => $self->table,
			      flds => qq(job_name		VARCHAR(32),
					 task_id 		INTEGER,
					 sim_sge_id		INTEGER,
					 sim_compute_host	VARCHAR(255),
					 load_sge_id		INTEGER,
					 load_compute_host	VARCHAR(255),
					 abbr			VARCHAR(255),
					 work_dir		VARCHAR(255),
					 blast_retries_left	INTEGER,
					 status			VARCHAR(32),
					 success		INTEGER,
					 exit_code		VARCHAR(255),
					 start_time		TIMESTAMP,
					 end_time		TIMESTAMP,
					 elap_time		INTEGER,
					 error_message 		TEXT,
					 PRIMARY KEY(job_name, task_id)
				     ));
    $self->mgdb->create_index(tbl => $self->table,
			      idx => $self->table . "_status",
			      flds => "status");
    
    $self->mgdb->drop_table(tbl => $self->retry_log_table);
    $self->mgdb->create_table(tbl => $self->retry_log_table,
			      flds => qq(job_name	       	VARCHAR(32),
					 task_id 		INTEGER,
					 retry			INTEGER,
					 sim_sge_id		INTEGER,
					 sim_compute_host	VARCHAR(255),
					 status			VARCHAR(32),
					 start_time		TIMESTAMP,
					 end_time		TIMESTAMP,
					 elap_time		INTEGER,
					 error_message 		TEXT,
					 PRIMARY KEY(job_name, task_id, retry)
				     ));
    $self->mgdb->create_index(tbl => $self->retry_log_table,
			      idx => $self->retry_log_table . "_status",
			      flds => "status");
    $self->mgdb->create_index(tbl => $self->retry_log_table,
			      idx => $self->retry_log_table . "_retry",
			      flds => "retry");
    $self->mgdb->create_index(tbl => $self->retry_log_table,
			      idx => $self->retry_log_table . "_job_task",
			      flds => "job_name, task_id");
}

sub get_task
{
    my($self, $job, $task) = @_;
    my $tbl = $self->table;
    my $res = $self->dbh->selectrow_hashref(qq(SELECT *
					       FROM $tbl
					       WHERE job_name = ? and task_id = ?), undef,
					    $job, $task);
    return $res;
}

sub set_task
{
    my($self, $job, $task, $rec) = @_;

    my(@set, @v, @n, @q);
    for my $k (keys %$rec)
    {
	next if $k eq 'job_name' or $k eq 'task_id';
	push (@set, "$k = ?");
	push(@n, $k);
	my $v = $rec->{$k};
	if ($k =~ /(start|end)_time/ && $v =~ /^\d+$/)
	{
	    $v = strftime("%Y-%m-%d %H:%M:%S", localtime($v))
	}

	push(@v, $v);
	push(@q, "?");
    }
    my $set = join(", ", @set);

    $self->dbh->begin_work();
    
    my $tbl = $self->table;
    my $n = $self->dbh->do(qq(UPDATE $tbl
			      SET $set
			      WHERE job_name = ? AND task_id = ?), undef,
			   @v, $job, $task);
    if ($n == 0)
    {
	#
	# Wasn't there, so insert.
	#
	push(@n, 'job_name', 'task_id');
	push(@v, $job, $task);
	push(@q, '?', '?');
	
	my $q = join(", ", @q);
	my $n = join(", ", @n);
	$self->dbh->do(qq(INSERT INTO $tbl ($n)
			  VALUES ($q)), undef, @v);
    }
    $self->dbh->commit();
}
   
sub set_status
{
    my($self, $job, $task, $status) = @_;

    my $tbl = $self->table;
    $self->dbh->do(qq(UPDATE $tbl
		      SET status = ?
		      WHERE job_name = ? AND task_id = ?), undef,
		   $status, $job, $task);
}

sub get_tasks_with_status
{
    my($self, $status) = @_;

    my $tbl = $self->table;
    my $sth = $self->dbh->prepare(qq(SELECT *
				     FROM $tbl
				     WHERE status = ?));
    $sth->execute($status);
    my @ret;
    while (my $r = $sth->fetchrow_hashref())
    {
	push(@ret, [$r->{job_name}, $r->{task_id}, $r]);
    }

    return @ret;
}

sub get_tasks_where
{
    my($self, $where, @vars) = @_;

    my $tbl = $self->table;
    my $sth = $self->dbh->prepare(qq(SELECT *
				     FROM $tbl
				     WHERE $where));
    $sth->execute(@vars);
    my @ret;
    while (my $r = $sth->fetchrow_hashref())
    {
	push(@ret, [$r->{job_name}, $r->{task_id}, $r]);
    }

    return @ret;
}

#
# Stash the current data into the retry table.
#
sub store_record_as_retry
{
    my($self, $job, $task) = @_;
    my $tbl = $self->table;
    my $rtbl = $self->retry_log_table;

    $self->dbh->begin_work();

    $self->dbh->do(qq(INSERT INTO $rtbl (job_name, task_id, retry, sim_sge_id, sim_compute_host,
					 status, start_time, end_time,elap_time, error_message)
		      (SELECT job_name, task_id, blast_retries_left, sim_sge_id, sim_compute_host,
					 status, start_time, end_time,elap_time, error_message
		       FROM $tbl
		       WHERE job_name = ? AND task_id = ?)), undef,
		   $job, $task);
    $self->dbh->do(qq(UPDATE $tbl
		      SET blast_retries_left = blast_retries_left - 1
		      WHERE job_name = ? and task_id = ?), undef,
		   $job, $task);
    $self->dbh->commit();
}


1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3