KBEC-00252 - Precondition check during dynamic job step creation

Article ID:360032829032
2 minute readKnowledge base
On this page

Summary

While using /javascript getProperty("/myJob/iState") to check a job’s precondition in a dynamically created job, using a statement similar to the following never returns its value as expected.

precondition => '$[/javascript getProperty("/myJob/iState")=="complete"]'

While using /javascript getProperty("/myJob/iState") to check a parent job’s condition to create its sub job dynamically, using a statement similar to the following never returns its result to "true" so, the parent’s job never finishes.

precondition => '$[/javascript getProperty("/myJob/iState")=="complete"]'

For example,

use strict;
use ElectricCommander;

 my $ec = ElectricCommander->new();

 # Create the batch API object
my $batch = $ec->newBatch('serial');

 $batch->createJobStep({
jobStepName => 'echo 1',
command => 'echo 1; sleep 10; ectool setProperty "/myJob/iState" --value "complete"',
parallel => '1'
} );

 $batch->createJobStep({
jobStepName => 'echo 5',
command => 'echo 5; sleep 1;',
precondition => '$[/javascript getProperty("/myJob/iState")=="complete"]',
parallel => '1'
} );

 # Send off the requests
$batch->submit();

This code tries to create a second job named "echo 5" if the condition of the previous job, "echo 1", reaches its state to complete even if it is still running.

But, the second job will never be triggered by the comparison condition

"precondition => '$[/javascript getProperty("/myJob/iState") == "complete";

because the state that the second job tries to compare against always remains in the "waiting" state.

The reason why it stays in a "Waiting" status and never resumes is because the JavaScript statement is evaluated at the ec-perl script’s runtime when the script contents are in the step commands.

This results in the precondition for the generated dynamic step effectively being a fixed value of "false" that will never change.

Solution

The code in the comparison status condition must change from:

precondition => '$[/javascript getProperty("/myJob/iState")=="complete"]',

to:

precondition => '$' . '[/javascript getProperty("/myJob/iState")=="complete"]',

which breaks up the property reference into two parts and concatenates the parts using "." This encourages CloudBees CD (CloudBees Flow) to not see the string as a property reference when it is inside the Perl script.

This full code works properly in a test environment since with Commander 4.2.2:

use strict;
use ElectricCommander;

 my $ec = ElectricCommander->new();

 # Create the batch API object
my $batch = $ec->newBatch('serial');

 $batch->createJobStep({
jobStepName => 'echo 1',
command => 'echo 1; sleep 10; ectool setProperty "/myJob/iState" --value "complete"',
parallel => '1'
} );

 $batch->createJobStep({
jobStepName => 'echo 5',
command => 'echo 5; sleep 1;',
precondition => '$' . '[/javascript getProperty("/myJob/iState")=="complete"]',
parallel => '1'
} );

 # Send off the requests
$batch->submit();

Applies to

Product versions:

4.2.2 or later

OS versions:

All