KBEC-00015 - Checking the status of a running job

Article ID:360033195171
2 minute readKnowledge base
On this page

Summary

getJobStatus API can now accomplish this as an API call. This script can still be modified for more detailed information about a job.

This article describes how to check the current status of a running job because the "outcome" job property is not set until the job is complete.

Solution

The general strategy for this solution is to use getJobDetails for the running job and then "walk" each step looking for any steps that have an "error" outcome. If at least one step has an error, job status is considered to be "error".

This implementation example uses ElectricCommander’s Perl API to get the details for the job. It then uses XML::XPath - a set of modules for parsing and evaluating XPath statements - to extract the step outcome from each step.

use ElectricCommander ();
use XML::XPath;

sub jobStatus($) {
 my ($jobDetails) = @_;
 my @jobSteps = $jobDetails->findnodes("//jobStep");
 foreach my $step (@jobSteps) {
 my $jobStepOutcome = $step->findvalue("outcome");
 if ($jobStepOutcome eq "error") {
 return 0;
 }
 }
 return 1;
}

# Open a connection to the server
my $N = new ElectricCommander->new();

my $jobId = $ENV{'COMMANDER_JOBID'};

# Get the job details for a running
my $jobDetails = $N->getJobDetails($jobId);
if (jobStatus($jobDetails) == 0) {
 print "Job $jobId status was error\n";
} else {
 print "Job was successful\n";
}

Use this example anytime you need to make a decision about what to do based on the job result in progress. For example, you could use this implementation inside the last step of a job. You could send an email in this step, using the job status to determine the contents of the email and the list to whom it should be sent.