Use staggered start times for individual jobs within a Build Flow parallel build

Posted on August 8th, 2015 at 10:00 AM


A parallel build within a Build Flow project can provide significant improvements in execution times especially if individual jobs are running on seperate targets. In some cases however it may be useful to stagger the start of individual jobs while maintaining the bulk of the parallel activity. An example of a staggered start is provided in the code below (code is added to 'Define build flow using flow DSL' in the Flow area of a Build Flow project):

def mybuild(x,d){            

  def t1 = new Date()
  println 'Job: ' + x + ' T1:' + t1
  for (i=0; i < d; i++)
  {
    sleep(60000)
  }
  build(x)
  def t2 = new Date()
  println 'Job: ' + x + ' T2:' + t2
}

def delay(d){
  for (i=0; i < d; i++)
  {
    sleep(60000)
  }
}

parallel (
  {mybuild("st1",1)},
  {mybuild("st2",2)},
  {mybuild("st3",3)}
)

def t3 = new Date()
println 'T3:' + t3
delay(2)
def t4 = new Date()
println 'T4:' + t4

This example uses three simple freestyle jobs; st1, st2 and st3. Each job has same basic function:

echo -e "\n************* Time: $(date)\n"

The resulting log from the build flow job is shown below:

building remotely on target (Linux) in workspace /home/user/jenkins/workspace/StaggerTest
parallel {
Job: st1 T1:Wed Jul 15 14:55:50 BST 2015
Job: st2 T1:Wed Jul 15 14:55:50 BST 2015
Job: st3 T1:Wed Jul 15 14:55:50 BST 2015
    Schedule job st1
    Build st1 #11 started
    st1 #11 completed 
Job: st1 T2:Wed Jul 15 14:57:00 BST 2015
    Schedule job st2
    Build st2 #10 started
    st2 #10 completed 
Job: st2 T2:Wed Jul 15 14:58:00 BST 2015
    Schedule job st3
    Build st3 #9 started
   st3 #9 completed 
Job: st3 T2:Wed Jul 15 14:59:00 BST 2015
}

T3:Wed Jul 15 14:59:00 BST 2015
T4:Wed Jul 15 15:01:00 BST 2015
Notifying upstream projects of job completion
Finished: SUCCESS

Individual jobs show their start times:

Job logs:
St1: ************* Time: Wed Jul 15 14:57:00 BST 2015

St2: ************* Time: Wed Jul 15 14:58:00 BST 2015

St3: ************* Time: Wed Jul 15 14:59:00 BST 2015