The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/env perl

use 5.008;
use strict;
use warnings;
use Getopt::Long;

use Helios::Service;

our $VERSION = '2.80';

our $Args;
our @JobHistory;
our @Logs;

our $OPT_JOBID = '';
our $OPT_HELP    = 0;

# 1st arg is jobid if it doesn't start with -
if ($ARGV[0] !~ /^-/) {
	$OPT_JOBID = shift @ARGV;
}

GetOptions(
	"jobid=s" => \$OPT_JOBID,
	"help"    => \$OPT_HELP,
);

# help mode
if ($OPT_HELP) {
	require Pod::Usage;
	Pod::Usage::pod2usage(-verbose => 2, -exitstatus => 0);
}

unless ($OPT_JOBID) {
	warn "$0: A jobid is required.\n";
	exit(1);
}

# this is a little old school, but we'll 
# instantiate the base Helios::Service to 
# get a collective database connection
my $s = Helios::Service->new();
$s->prep();
my $dbh = $s->dbConnect();

my @placeholders;
my $sql = q{
	SELECT 
		exitstatus
	FROM
		helios_job_history_tb
	WHERE
		jobid = ?
	ORDER BY complete_time DESC
		
};
push(@placeholders, $OPT_JOBID );

my @rs;
eval {
	@rs = @{ $dbh->selectall_arrayref($sql, undef, @placeholders) };
	1;
} or do {
	my $E = $@;
	warn "$0: ERROR: $E\n";
	exit(1);
};

if ( @rs ) {
	print $rs[0]->[0],"\n";
}

$dbh->disconnect();


exit(0);


=head1 NAME

helios_job_status - get a Helios job's exitstatus

=head1 SYNOPSIS

 helios_job_status --jobid=<jobid> 
   
 # return the exitstatus for job 12345
 helios_job_status --jobid=12345

 # first argument is assumed to be jobid, so "--jobid=" is optional
 helios_job_status 12345

=head1 DESCRIPTION

The helios_job_status command can be used to retrieve the exitstatus a job 
recorded in the Helios collective database.  When a job is finished using the 
completedJob(), failedJob(), or failedJobPermanent() methods in your 
Helios::Service class, an exitstatus is recorded.  Successful jobs are marked 
with a status of 0, while failed jobs will have an nonzero status.  If a job
has been marked as finished in a Helios collective, this command will return 
that job's exitstatus via STDOUT.  If the job in question does not exist or
has not been completed, nothing will be returned.

=head1 AUTHOR

Andrew Johnson, E<lt>lajandy at cpan dot orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2013-4 by Logical Helion, LLC.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.0 or,
at your option, any later version of Perl 5 you may have available.

=head1 WARRANTY

This software comes with no warranty of any kind.

=cut