Jobrun

 view release on metacpan or  search on metacpan

lib/Jobrun.pm  view on Meta::CPAN

	$sth->execute( $status, $exit_code, $startTime, $endTime, $elapsedTime, $name );

	#$dbh->commit();
	return;
}

# this is a sanity check for the child processes
# validate that the child process is running
# if not then update status as failed and -1 in exit_code
sub childSanityCheck {
	my ( $logFileFH, $verbose ) = @_;
	logger( $logFileFH, $verbose, "childSanityCheck()\n" );
	my $sth = $utilDBH->prepare("SELECT pid FROM $controlTable WHERE status = ?");
	$sth->execute('running');
	while ( my $row = $sth->fetchrow_hashref ) {

		my $pid = $row->{pid};
		logger( $logFileFH, $verbose, "   pid: $pid\n" );

		my $rc = kill 0, $pid;
		logger( $logFileFH, $verbose, "    rc: $rc\n" );

		if ( $rc == 0 ) {
			my $dbh = createDbConnection();
			my $sth = $dbh->prepare("UPDATE $controlTable SET status = ?, exit_code = ? WHERE pid = ?");
			$sth->execute( 'failed', -1, $pid );
		}
	}
	return;
}

sub logger {
	my ( $fh, $verbose, @msg ) = @_;
	while (@msg) {
		my $line = shift @msg;
		$fh->print($line);
		print "$line" if $verbose;
	}
	return;
}

sub new {
	my ( $pkg, %args ) = @_;
	my $class = ref($pkg) || $pkg;

	#print "Class: $class\n";

	$args{dbh} = createDbConnection();

	$args{insert}       = \&insertTable;
	$args{updateStatus} = \&updateStatus;
	$args{delete}       = \&deleteTable;
	$args{select}       = \&selectTable;

	# name,pid,status,start_time,end_time,elapsed_time,exit_code,cmd) VALUES (?,?,?,?,?)");

	$args{columnNamesByName}  = { name => 0,      pid => 1,     cmd => 2,     status => 3,        exit_code => 4 };
	$args{columnNamesByIndex} = { 0    => 'name', 1   => 'pid', 2   => 'cmd', 3      => 'status', 4         => 'exit_code' };
	$args{columnValues}       = [qw/undef undef undef undef undef/];

	my ( $user, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell, $expire ) = getpwuid($<) or croak "getpwuid: $!";
	print "User: $user, UID: $uid\n";

	my $retval = bless \%args, $class;

	$retval->{insert}(
		$retval,
		$retval->{JOBNAME},    #job name
		$$,                    #pid
		, 'running'            #status
		, 'NA'                 #exit code
		, ''                   #start time
		, ''                   #end time
		, ''                   #elapsed time
		, $retval->{CMD},      #command

	);
	return $retval;
}

sub getChildrenCount {

	# Return current child count
	my $sth = $utilDBH->prepare("SELECT count(*) child_count FROM $controlTable WHERE status = 'running'");
	$sth->execute();
	my $row = $sth->fetchrow_hashref;
	return $row->{child_count} ? $row->{child_count} : 0;
}

# status is called as a separate process
# and will have not knowledge of the Jobrun object
# so we need to pass the controlTable name
sub status {
	my ( $controlTable, $statusType, %config ) = @_;
	my $dbh = createDbConnection();
	my $sql;
	-r "$tableDir/${controlTable}.csv" or croak "table $tableDir/$controlTable.csv does not exist: $!\n";
	if ( $statusType eq 'all' ) {
		$sql = "SELECT * FROM $controlTable order by start_time asc";
	}
	else {
		$sql = "SELECT * FROM $controlTable WHERE status = '$statusType' order by start_time asc";
	}

	print "table: $controlTable\n";

	my $sth = $dbh->prepare($sql);
	$sth->execute();

	# %-$config{colLenSTART_TIME}s %-$config{colLenEND_TIME}s %-$config{colLenELAPSED_TIME}s
	printf "%-$config{colLenNAME}s %-$config{colLenPID}s %-$config{colLenSTATUS}s %-$config{colLenEXIT_CODE}s %-$config{colLenSTART_TIME}s %-$config{colLenEND_TIME}s %-$config{colLenELAPSED_TIME}s %-$config{colLenCMD}s\n", 'name', 'pid', 'status', 'exit...

	printf "%-$config{colLenNAME}s %-$config{colLenPID}s %-$config{colLenSTATUS}s %-$config{colLenEXIT_CODE}s %-$config{colLenSTART_TIME}s %-$config{colLenEND_TIME}s %-$config{colLenELAPSED_TIME}s %-$config{colLenCMD}s\n", '-' x $config{colLenNAME}, '-'...
	while ( my $row = $sth->fetchrow_hashref ) {
		my $rowlen = length( $row->{cmd} ) + 0;

		#warn "rowlen: $rowlen\n";
		printf "%-$config{colLenNAME}s %-$config{colLenPID}s %-$config{colLenSTATUS}s %-$config{colLenEXIT_CODE}s %-$config{colLenSTART_TIME}s %-$config{colLenEND_TIME}s %$config{colLenELAPSED_TIME}s %-$config{colLenCMD}s\n", $row->{name}, $row->{pid}, $ro...
			substr(
			  $row->{cmd}, defined( $config{colCmdStartPos} ) ? $config{colCmdStartPos} : 0, defined( $config{colCmdEndPos} )
			? $config{colCmdEndPos}



( run in 2.195 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )