KBEC-00092 - Using Perl to monitor a parallel step on a resource for completion before continuing or messaging

Description

There are several resources in a source pool (say one per server) that performs an operation to a local destination. When the operation is complete, the local result is shared with other servers. The resource is still busy. How can you retain the resource until another operation is completed?

Solution

  1. Create a new step immediately after the step that is running the build.
  2. Mark them both to run in parallel.
  3. Set the Shell to "ec-perl".
  4. Set the Resource to the resource that you want to mark as being in use.
  5. Paste the following Perl code into the Command block.
  6. Adjust the sleep time to reduce the overhead, based on how long you expect the build step to run.

Example 1 - wait for a parallel step to complete

This example uses xPath to find the parent of the current step, and wait for the parallel step to complete.


use strict;
use ElectricCommander ();
$| = 1;
my $ec = new ElectricCommander->new();

# Get all the steps for the current job
my $xPath = $ec->getJobDetails($ENV{COMMANDER_JOBID});

# Find the preceding sibling step by its position relative to the current
my $precedingJobStepId = $xPath->findvalue(
qq#//jobStep[jobStepId="$ENV{COMMANDER_JOBSTEPID}"]# .
qq#/preceding-sibling::jobStep[1]/jobStepId#);

# Wait for the preceding parallel step to finish
my $stepIsDone = 0;
print "Waiting for step - $precedingJobStepId\n";
while (! $stepIsDone) {

# Get the status of the preceding step
$xPath = $ec->getJobStepStatus($precedingJobStepId);
my $precedingJobStepStatus = $xPath->findvalue('//status');
print " Status of step is $precedingJobStepStatus\n";

$stepIsDone = ($precedingJobStepStatus eq "completed");
next if ($stepIsDone);
sleep (10);
}

Example 2 - monitor a parallel step and send a notifier before the monitored step times out

  1. Create a procedure with two steps that run in parallel.
  2. This example step uses three parameters for the procedure to characterize durations of a step. Create these parameters:
    1. CheckStepDuration - How often in seconds to check if the step has completed. For example fifteen minutes, 900 seconds.
    2. MaxStepDuration - How long in seconds until the monitored step will be timed out. For example two hours, 7200 seconds.
    3. WarningBeforeDuration - How long in seconds before the end of the monitored step the notifier is sent indicating the job will be timed out. For example thirty minutes, 1800 seconds.
  3. The first step calls the procedure to be monitored.
  4. The second step
    1. Has the notifiers for the about to fail message.
    2. Contains the following Perl code:

use strict;
use ElectricCommander ();
$| = 1;
my $ec = new ElectricCommander->new();

# Find a sibling job step by its position relative to the current job step
my $precedingJobStepId = $xPath->findvalue(qq#//jobStep[jobStepId="$ENV{COMMANDER_JOBSTEPID}"]/preceding-sibling::jobStep[1]/jobStepId#);
# Wait for the preceding parallel step to finish
my $stepIsDone = 0;
my $sumDuration = 0;
while (! $stepIsDone) {
# Get the status of the preceding step
$xPath = $ec->getJobStepStatus($precedingJobStepId);
my $precedingJobStepStatus = $xPath->findvalue('//status');
$stepIsDone = ($precedingJobStepStatus eq "completed");
next if ($stepIsDone);
if (($[MaxStepDuration] - $[WarningBeforeDuration]) > $sumDuration) {
# Wait for the step to complete or the notifier time to approach
$sumDuration = $sumDuration + $[CheckStepDuration];
sleep ($[CheckStepDuration]);
} else {
# exit this step with a failure so the notifier for this step is sent
exit 1;
}
}
Have more questions? Submit a request

Comments

Powered by Zendesk