<commit_before>package solGS::DependentJob;


use Moose;
use namespace::autoclean;

use CXGN::Tools::Run;
use File::Spec::Functions qw / catfile catdir/;
use File::Temp qw / tempfile tempdir /;
use Try::Tiny;
use Storable qw/ nstore retrieve /;
use solGS::AnalysisReport;
use Carp qw/ carp confess croak /;

with 'MooseX::Getopt';
with 'MooseX::Runnable';


has "dependency_jobs" => (
    is       => 'ro',
    isa      => 'Str',
    required => 1, 
 );

has "dependency_type" => (
     is       => 'ro',
     isa      => 'Str',
     required => 1, 
 );

has "dependent_type" => (
     is       => 'ro',
     isa      => 'Str',
    required  => 1,
 );

has "temp_file_template" => (
     is       => 'ro',
     isa      => 'Str',
     required => 1, 
 );

has "analysis_report_args_file" => (
     is       => 'ro',
     isa      => 'Str',
     required => 1, 
 );

has "temp_dir" => (
     is       => 'ro',
     isa      => 'Str',
     required => 1, 
 );

has "r_script"   => (
     is       => 'ro',
     isa      => 'Str',
     required => 0, 
 );

has "script_args" => (
     is       => 'ro',
     isa      => 'ArrayRef',
     required => 0, 
 );

has "gs_model_args_file" => (
     is       => 'ro',
     isa      => 'Str',
     required => 0, 
 );



sub run {
    my $self = shift;
    
    my $dependency_jobs    = $self->dependency_jobs;
    my $dependent_job      = $self->r_script;
    my $temp_dir           = $self->temp_dir;
    my $temp_file_template = $self->temp_file_template;
    my $report_file        = $self->analysis_report_args_file;
    my $model_file         = $self->gs_model_args_file;
    my $job_type           = $self->dependent_type;
    my $dependency_type    = $self->dependency_type;
    my $args               = $self->script_args;
   
    print STDERR "\nrun dependency type: $dependency_type\n";
    print STDERR "\nrun tmpdir: $temp_dir -- template $temp_file_template\n";
    print STDERR "\nrun report file: $report_file\n";
    print STDERR "\nrun gs model file: $model_file\n";
    print STDERR "\nrun dependent job : $dependent_job -- pid: $$\n";
    print STDERR "\nrun job_type : $job_type\n";       
    print STDERR "\nrun r script args : $args->[0] -- $args->[1]\n";
    
    sleep 60;

    my $all_pre_jobs_done = $self->check_prerequisite_jobs();
    my $all_jobs_done;
     
    if ($all_pre_jobs_done)
    {
	unless ($job_type =~ /send_analysis_report/) 
	{ 
	    $all_jobs_done = $self->run_dependent_job();
	}
    }
    
    if ($all_jobs_done || $job_type =~ /send_analysis_report/) 
    {
	$self->send_analysis_report();
    }
    
}


sub check_prerequisite_jobs {
    my $self = shift;
    
    my $prerequisite_jobs = $self->dependency_jobs;
  
    my @prerequisite_jobs;

    if ($prerequisite_jobs =~ /:/g) 
    {	
	@prerequisite_jobs = split(':', $prerequisite_jobs);	
    }
    else 
    {
	push @prerequisite_jobs, $prerequisite_jobs;
    }
   
    my $all_pre_jobs_done;
    sleep 10;

    my ($sec, $min, $start_hr) = localtime();
    my $job_type = $self->dependent_type;
   
    while (1) 
    {
	no warnings 'uninitialized';
	foreach my $prerequisite_job (@prerequisite_jobs) 
	{
	     my @job_stdout;
	     my $check;

	     @job_stdout = qx /qstat -f $prerequisite_job 2>&1/;
	     $check = 'qstat: Unknown Job Id ' . $prerequisite_job;
	 
	     if (@job_stdout && $job_stdout[0] =~ /^($check)/) 
	     {
		 @prerequisite_jobs = grep {$_ ne $prerequisite_job} @prerequisite_jobs;
	     }
	}

	if (scalar(@prerequisite_jobs) == 0) 
	{
	    $all_pre_jobs_done = 1;
	    last;
	} 
	else
	{
	    my ($sec, $min, $now_hr) = localtime();
	    if ($now_hr == $start_hr + 2 ) 
	    { 
		last;
	    }
	    else
	    {
		sleep 60; 
	    }    
	}	
    }

    return $all_pre_jobs_done;
}


sub create_cluster_acccesible_tmp_files {
    my ($self, $temp_file_template)  = @_;
<commit_msg>extend script run to 8hrs<commit_after>package solGS::DependentJob;


use Moose;
use namespace::autoclean;

use CXGN::Tools::Run;
use File::Spec::Functions qw / catfile catdir/;
use File::Temp qw / tempfile tempdir /;
use Try::Tiny;
use Storable qw/ nstore retrieve /;
use solGS::AnalysisReport;
use Carp qw/ carp confess croak /;

with 'MooseX::Getopt';
with 'MooseX::Runnable';


has "dependency_jobs" => (
    is       => 'ro',
    isa      => 'Str',
    required => 1, 
 );

has "dependency_type" => (
     is       => 'ro',
     isa      => 'Str',
     required => 1, 
 );

has "dependent_type" => (
     is       => 'ro',
     isa      => 'Str',
    required  => 1,
 );

has "temp_file_template" => (
     is       => 'ro',
     isa      => 'Str',
     required => 1, 
 );

has "analysis_report_args_file" => (
     is       => 'ro',
     isa      => 'Str',
     required => 1, 
 );

has "temp_dir" => (
     is       => 'ro',
     isa      => 'Str',
     required => 1, 
 );

has "r_script"   => (
     is       => 'ro',
     isa      => 'Str',
     required => 0, 
 );

has "script_args" => (
     is       => 'ro',
     isa      => 'ArrayRef',
     required => 0, 
 );

has "gs_model_args_file" => (
     is       => 'ro',
     isa      => 'Str',
     required => 0, 
 );



sub run {
    my $self = shift;
    
    my $dependency_jobs    = $self->dependency_jobs;
    my $dependent_job      = $self->r_script;
    my $temp_dir           = $self->temp_dir;
    my $temp_file_template = $self->temp_file_template;
    my $report_file        = $self->analysis_report_args_file;
    my $model_file         = $self->gs_model_args_file;
    my $job_type           = $self->dependent_type;
    my $dependency_type    = $self->dependency_type;
    my $args               = $self->script_args;
   
    print STDERR "\nrun dependency type: $dependency_type\n";
    print STDERR "\nrun tmpdir: $temp_dir -- template $temp_file_template\n";
    print STDERR "\nrun report file: $report_file\n";
    print STDERR "\nrun gs model file: $model_file\n";
    print STDERR "\nrun dependent job : $dependent_job -- pid: $$\n";
    print STDERR "\nrun job_type : $job_type\n";       
    print STDERR "\nrun r script args : $args->[0] -- $args->[1]\n";
    
    sleep 60;

    my $all_pre_jobs_done = $self->check_prerequisite_jobs();
    my $all_jobs_done;
     
    if ($all_pre_jobs_done)
    {
	unless ($job_type =~ /send_analysis_report/) 
	{ 
	    $all_jobs_done = $self->run_dependent_job();
	}
    }
    
    if ($all_jobs_done || $job_type =~ /send_analysis_report/) 
    {
	$self->send_analysis_report();
    }
    
}


sub check_prerequisite_jobs {
    my $self = shift;
    
    my $prerequisite_jobs = $self->dependency_jobs;
  
    my @prerequisite_jobs;

    if ($prerequisite_jobs =~ /:/g) 
    {	
	@prerequisite_jobs = split(':', $prerequisite_jobs);	
    }
    else 
    {
	push @prerequisite_jobs, $prerequisite_jobs;
    }
   
    my $all_pre_jobs_done;
    sleep 10;

    my ($sec, $min, $start_hr) = localtime();
    my $job_type = $self->dependent_type;
   
    while (1) 
    {
	no warnings 'uninitialized';
	foreach my $prerequisite_job (@prerequisite_jobs) 
	{
	     my @job_stdout;
	     my $check;

	     @job_stdout = qx /qstat -f $prerequisite_job 2>&1/;
	     $check = 'qstat: Unknown Job Id ' . $prerequisite_job;
	 
	     if (@job_stdout && $job_stdout[0] =~ /^($check)/) 
	     {
		 @prerequisite_jobs = grep {$_ ne $prerequisite_job} @prerequisite_jobs;
	     }
	}

	if (scalar(@prerequisite_jobs) == 0) 
	{
	    $all_pre_jobs_done = 1;
	    last;
	} 
	else
	{
	    my ($sec, $min, $now_hr) = localtime();
	    if ($now_hr == $start_hr + 8 ) 
	    { 
		last;
	    }
	    else
	    {
		sleep 60; 
	    }    
	}	
    }

    return $all_pre_jobs_done;
}


sub create_cluster_acccesible_tmp_files {
    my ($self, $temp_file_template)  = @_;

{'fraction_non_alphanumeric': 0.14933886440238528, 'fraction_numerical': 0.006352087114337568, 'mean_word_length': 2.229384679782336, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3483378', 'n_tokens_mistral': 3108, 'n_tokens_neox': 2826, 'n_words': 604}