Answers: <p>In Jenkins scripted pipelines you could do something like this:</p>
  <pre><code>node() {
  
     Date startTime = new Date()
  
     long currentTimeMilli = startTime.getTime()
  
     long hourMilli = currentTimeMilli + 3600000
  
     Date cutoffTime = new Date(hourMilli)
  
     // Call the &quot;build&quot; function
  
     Date currentTime = new Date()
  
     if (currentTime.before(cutoffTime)) {
         // Call the &quot;build&quot; function again
     }
  
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-08-18T16:44:02.927Z
Answers: Wouldn't you need to update after the build function, but before the if statement so that it's comparing the actual current time to cutoff time instead of the current time established at the beginning? I think how it is written, the if statement will always be true.
Answers: Natolio
Answers: CC BY-SA 4.0
Answers: 2022-08-18T18:18:57.110Z
Answers: That does look like it's the case, but otherwise your answer worked perfectly for me. If you can update the answer so that there is a new time being compared to cutoff time, I will mark it correct.
Answers: Natolio
Answers: CC BY-SA 4.0
Answers: 2022-08-18T22:27:42.283Z
Answers: @Natolio good catch, will update soon
Answers: Levi
Answers: CC BY-SA 4.0
Answers: 2022-08-18T00:23:04.623Z
Answers: 2022-08-18T22:33:09.617Z
Answers: 2022-08-18T22:33:09.617Z
Answers: Levi
Body: <p>I am using Jenkins to build an <code>rpm</code> for some custom software.</p>
  <p>During the build there must be a race condition that only seems to appear when building the RPM, which results in the job finishing successfully, but the software not being built correctly.</p>
  <p>The most obvious way to tell the build is a false positive is to <strong>check how quickly the <code>build stage</code> finished</strong>.</p>
  <p>During an actually successful run, the build stage will take approximately <strong>an hour</strong>. When a false positive occurs it will finish in <strong>less than 25 minutes</strong>.</p>
  <p>The end goal of course is to <strong>fix the race condition</strong>, but preventing the pipeline from creating a bad RPM and saying the pipeline was successful in the mean time would be a great help.</p>
  
Tags: <jenkins><jenkins-pipeline>
Title: Force jenkins job to fail if stage did not run long enough
UserDisplayName: Natolio
ThreadText: # Force jenkins job to fail if stage did not run long enough
  
  (Asked by: username_0 on 2022-08-17)
  
  I am using Jenkins to build an `rpm` for some custom software.
  
  During the build there must be a race condition that only seems to appear when building the RPM, which results in the job finishing successfully, but the software not being built correctly.
  
  The most obvious way to tell the build is a false positive is to **check how quickly the `build stage` finished**.
  
  During an actually successful run, the build stage will take approximately **an hour**. When a false positive occurs it will finish in **less than 25 minutes**.
  
  The end goal of course is to **fix the race condition**, but preventing the pipeline from creating a bad RPM and saying the pipeline was successful in the mean time would be a great help.
  
  **Tags:** jenkins, jenkins-pipeline
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-08-18. Score: 1 | Accepted answer
  
  In Jenkins scripted pipelines you could do something like this:
  
  ```
  node() {
  
     Date startTime = new Date()
  
     long currentTimeMilli = startTime.getTime()
  
     long hourMilli = currentTimeMilli + 3600000
  
     Date cutoffTime = new Date(hourMilli)
  
     // Call the "build" function
  
     Date currentTime = new Date()
  
     if (currentTime.before(cutoffTime)) {
         // Call the "build" function again
     }
  
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Wouldn't you need to update after the build function, but before the if statement so that it's comparing the actual current time to cutoff time instead of the current time established at the beginning? I think how it is written, the if statement will always be true.  
  > **username_0**: That does look like it's the case, but otherwise your answer worked perfectly for me. If you can update the answer so that there is a new time being compared to cutoff time, I will mark it correct.  
  > **username_1**: @username_0 good catch, will update soon  
  
  ---
  
  