[Bio] / FigWebServices / seed_ctl.cgi Repository:
ViewVC logotype

View of /FigWebServices/seed_ctl.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (annotate)
Wed Jun 16 14:06:48 2004 UTC (15 years, 11 months ago) by olson
Branch: MAIN
Changes since 1.2: +55 -33 lines
Revamp background job stuff.

#
# SEED control panel.
#
# Functionality:
#
# Show info about this seed
# Enable / disable SEED
# Remake indexes
# Switch code versions
#

use FIG;
#$FIG_Config::dbport =3000;

use strict;

use Text::Wrap;
use POSIX;

use CGI;
my $cgi = new CGI;

use HTML;
my @html = ();

my $fig;
my $ok = eval {
    $fig = new FIG;
};

#
# Common dirs.
#

my $job_dir = "$FIG_Config::data/Global/background_jobs";
my $down_file = "$FIG_Config::data/Global/why_down";

#
# Handle actions first.
#

if ($cgi->param('enable_server'))
{
    push(@html, $cgi->p("Enabling server."));


    unlink($down_file);
}
elsif ($cgi->param('disable_server'))
{
    push(@html, $cgi->p("Disabling server."));


    open my $fh, ">$down_file";
    print $fh $cgi->param("disable_reason");
    close($fh);
}
elsif ($cgi->param("rebuild_indexes"))
{
    #
    # Fork a process to run the rebuild scripts.
    #

    my $add = "$FIG_Config::bin/add_assertions_of_function";
    my $mkindex = "$FIG_Config::bin/make_indexes";

    $fig->run_in_background(sub {
	
	print "Running $add\n";
	
	open(my $fh, ">$down_file");
	print $fh "SEED server is rebuilding indexes.\n";
	print $fh "Running $add\n";
	close($fh);
	
	system($add);
	
	print "Running $mkindex\n";
	
	open my $fh, ">>$down_file";
	print $fh "Running $mkindex\n";
	close($fh);
	
	system($mkindex);
	
	print "Complete\n";
	
	unlink($down_file);
    });
}

#
# Check params list for other actions (that encode names within the param name).
#

for my $action ($cgi->param)
{
    if ($action =~ /^delete_job_(\d+)/)
    {
	system("rm -r $job_dir/$1");
	push(@html, $cgi->p("Removed job $1"));
    }
    elsif ($action =~ /^kill_job_(\d+)/)
    {
	my $ret = kill(1, -$1);
	push(@html, $cgi->p("Killed job $1; return code is $ret"));
    }
}

#
# Check database
#

push(@html, $cgi->h1("SEED current status"));
push(@html, $cgi->start_form(-action => "seed_ctl.cgi"));
push(@html, $cgi->h2("Database status"));

if (!$ok)
{
    push(@html, "Database system is unavailable.\n");
}
else
{
    my $dbh = $fig->db_handle;

    my $resp = $dbh->SQL("SELECT count(*) from genome");
    if (!$resp or @$resp == 0)
    {
	push(@html, "Could not get count from genome table\n");
    }
    else
    {
	my $c = $resp->[0]->[0];
	push(@html, "Database live: found $c genomes\n");
    }
}

#
# Check enabled state.
#

push(@html, $cgi->h2("SEED status"));

my $uptime = `uptime`;
if ($uptime =~ /load average(s?):\s+(.*)/)
{
    push(@html, "Server load averages: $2<p>\n");
}

if (-f $down_file)
{
    my $down_text = `cat $down_file`;

    push(@html, $cgi->p("SEED server is down:"));
    push(@html, $cgi->pre($down_text));
    push(@html, $cgi->submit(-value => "Enable SEED server.", -name => "enable_server"));
    
}
else
{
    push(@html, $cgi->p("SEED server is up.\n"));
    push(@html, $cgi->p("To disable SEED server, enter a reason below and click button."));
    push(@html, $cgi->textarea(-name => "disable_reason",
			       -rows => 3, -cols => 70));
    push(@html, $cgi->p);
    push(@html, $cgi->submit(-value => "Disable SEED server.", -name => "disable_server"));
}

push(@html, $cgi->hr);

#
# Check for background jobs.
#

&FIG::verify_dir($job_dir);

push(@html, $cgi->h2("Background jobs"));
push(@html, "\n");

if ($cgi->param("show_output"))
{
    my $job = $cgi->param("show_output");

    push(@html, $cgi->hr);

    push(@html, $cgi->h2("Job $job output"));
    open(my $fh, "<$job_dir/$job/OUTPUT");
    push(@html, "<pre>");
    
    while (<$fh>)
    {
	push(@html, $_);
    }
    close($fh);

    push(@html, "</pre>");
    push(@html, $cgi->hr);
}
opendir(D, $job_dir);
my @jobs = grep { $_ =~ /^\d+$/ } readdir(D);

if (@jobs > 0)
{
    my @hdrs;
    my $tbl = [];

    @hdrs = ("Process", "Running", "Status", "Description", "Output", "Action");

    for my $job (@jobs)
    {
	my($running, $status, $but, @output, $info, $output);
	
	if (kill(0, $job) > 0)
	{
	    $running = "Running";
	    $but = $cgi->submit(-value => "Kill process", -name => "kill_job_$job");
	}
	else
	{
	    $running = "Dead";
	    $but = $cgi->submit(-value => "Remove entry", -name => "delete_job_$job");
	}

	$status = &FIG::file_read("$job_dir/$job/STATUS");
	$info = &FIG::file_read("$job_dir/$job/INFO");
	@output = &FIG::file_read("$job_dir/$job/OUTPUT");
	my $outlink;

	if (@output > 10)
	{
	    splice(@output, 0, @output - 10);
	    my $link = "seed_ctl.cgi?show_output=$job";
	    $outlink = "<a href=\"$link\">Show full output</a>\n";
	}

	$Text::Wrap::columns = 60;
	$output = wrap('', '', @output);
	$output = "<pre>$output\n</pre><br>$outlink\n";
	push(@$tbl, [$job, $running, $status, $info, $output, $but]);
    }
    push(@html, &HTML::make_table(\@hdrs, $tbl));
}
else
{
    push(@html, $cgi->p("No background jobs running"));
}

push(@html, $cgi->hr());

#
# Actions support.
#

push(@html, $cgi->h1("SEED actions"));

#
# making indexes.
#

push(@html, $cgi->h2("Indexes"));
push(@html, $cgi->p("Recreating indexes can take a while; the SEED server will be unavailable to users while it is running."));
push(@html, $cgi->submit(-value => "Rebuild indexes", -name => "rebuild_indexes"));

push(@html, $cgi->end_form);

&HTML::show_page($cgi,\@html);


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3