Merge branch 'master' of https://github.com/solgenomics/sgn
[sgn.git] / cgi-bin / tools / wait.pl
blob5092f8fc0d1b11898aa7b5a72eef63a3e0a64eaa
2 use strict;
3 use Storable qw / retrieve /;
4 use File::Temp qw / tempfile /;
5 use File::Copy;
6 use File::Basename qw / basename /;
8 use Cwd qw/ realpath /;
9 use File::Spec::Functions;
10 use File::NFSLock qw/uncache/;
12 use CXGN::Page;
13 use CXGN::Page::FormattingHelpers qw/ page_title_html blue_section_html /;
14 use CXGN::Tools::Run;
16 use CatalystX::GlobalContext qw( $c );
18 my $page = CXGN::Page->new("Generic SGN Cluster Job Waiting Page", "Lukas");
20 #extra_params added since I couldn't prevent encoding/decoding merges
21 # format: param1:value1::param2:value2::etc3:v3
22 # this adds parameters to the redirect page
24 my ($tmp_app_dir, $job_file, $redirect, $out_file_override, $extra_params)
25 = $page->get_arguments("tmp_app_dir", "job_file", "redirect", "out_file_override", "extra_params");
27 if( $out_file_override ) {
28 $out_file_override = realpath( $out_file_override );
30 print STDERR "OUTFILE: $out_file_override\n";
31 $out_file_override =~ m!/(data|export)/(prod|shared)|(/tmp)! && $out_file_override !~ /\.\./
32 or die "is someone trying to do something nasty? illegal out_file_override '$out_file_override'";
35 my ($message) = $page->get_arguments("message");
38 my $d = CXGN::Debug->new;
40 $tmp_app_dir =~ s!/!!g;
41 my $tmpdir = $page->path_to( $page->tempfiles_subdir($tmp_app_dir) );
43 $job_file = catfile($tmpdir,$job_file);
45 $d->d("Arguments: job_file = $job_file redirect = $redirect");
47 unless( -f $job_file ) {
48 $c->throw( message => "Job not found. Has it already been executed and the results retrieved?",
49 is_error => 0,
50 developer_message => "Job file was '$job_file'\n",
55 my $job = retrieve($job_file)
56 or die "Could not retrieve job_file $job_file";
58 if ( $job->alive ){
59 display_not_finished($message);
60 return;
62 else {
63 # the job has finished
64 # copy the cluster temp file back into "apache space"
66 my (undef, $apache_temp) = tempfile( DIR=>$tmpdir,
67 TEMPLATE=>"alignXXXXXX",
70 $d->debug("COPY --> OUTFILE: ".$job->out_file()." Apache temp: $apache_temp");
72 my $job_out_file = $job->out_file();
73 for( 1..10 ) {
74 uncache($job_out_file);
75 last if -f $job_out_file;
76 sleep 1;
79 -f $job_out_file or die "job output file ($job_out_file) doesn't exist";
80 -r $job_out_file or die "job output file ($job_out_file) not readable";
81 -w $apache_temp or die "apache temp directory doesn't exist or not writable - won't copy";
83 # You may wish to provide a different output file to send back
84 # rather than STDOUT from the job. Use the out_file_override
85 # parameter if this is the case.
86 my $out_file = $out_file_override || $job->out_file();
87 system("ls /data/prod/tmp 2>&1 >/dev/null");
88 copy($out_file, $apache_temp)
89 or die "Can't copy result file '$out_file' to temp dir $!";
91 #clean up the job tempfiles
92 $job->cleanup();
94 #also delete the job file
95 unlink $job_file;
98 my $redirect_string = $redirect . basename( $apache_temp );
100 $page->client_redirect($redirect_string);
104 sub display_not_finished {
106 my $message = shift;
107 $message ||= "Job running, please wait.";
109 print <<HTML;
110 Content-type: text/html
112 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
113 <html>
114 <head>
115 <title>Job running</title>
116 <meta http-equiv="Refresh" content="3" />
117 <link rel="stylesheet" href="/css/sgn.css" type="text/css" />
118 </head>
119 <body>
120 <style>
121 body { padding-top: 80px }
122 img, div, h2 {
123 display: block;
124 margin: 1em auto;
125 text-align: center;
127 </style>
128 <img src="/img/sgn_logo_animated.gif" alt="SGN logo"/>
129 <h2>$message</h2>
130 <div>Please note: jobs are limited to 1 hour of run time.</div>
131 <img src="/img/progressbar1.gif" alt="In Progress..."/>
132 </body>
133 </html>
134 HTML
138 sub display_finished {
139 my $link = shift;
141 my $title = page_title_html("Job completed.");
143 $page->header();
145 print <<HTML;
146 <br /><br />
147 $title
148 <center>
150 <br />
151 <a href="$link"><b>View Result</b></a>
152 <br /><br /><br />
153 </center>
155 <!-- Debug info: -->
156 <!-- -->
158 <br /><br /><br /><br />
159 HTML
161 $page->footer();