Answers: <p>Jenkins pipeline jobs are designed to be serializable to disk, to increase durability (their words not mine). Therefore they are considered safe to be suspended. The only things you can do is either wrap your stages in functions with @NonCPS decorator or using properties make the jobs non-resumable after restart, but this will cause them to not be resumed after restart.</p>
  
  <p>Also, restarting jenkins is not really a thing that happens all the time, so it might not be worth the effort to protect against.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-09-30T13:34:51.073Z
Answers: 2019-09-30T13:34:51.073Z
Answers: Gothrek
Body: <p>I have a simple pipeline job with a single stage:</p>
  
  <pre><code>timestamps {
      node ("jenkins-slave-small") {
              stage('Stage 1') {
                  sh "echo 'stage2 echo'; sleep 500s"
              }
      }
  }
  </code></pre>
  
  <p>I am testing whether or not /safeRestart waits for my pipeline to finish executing, but it seems like it's not. </p>
  
  <p>Logs:</p>
  
  <pre><code>[Pipeline] { (Stage 1)
  [Pipeline] sh
  + echo stage2 echo
  stage2 echo
  + sleep 500s
  Resuming build at Thu Sep 26 18:14:18 UTC 2019 after Jenkins restart
  Waiting to resume part of test-folder » test-pipeline-no-parallels #6: Finished waiting
  Ready to run at Thu Sep 26 18:14:55 UTC 2019
  </code></pre>
  
  <p>Am I misunderstanding how /safeRestart is supposed to behave or is that the intended behaviour?</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-09-26T21:51:58.773Z
Comments: I don't think you are misunderstanding.  I suspect that since Pipeline jobs can (theoretically) be safely suspended and resumed unlike traditional "Freestyle" jobs, safe restart doesn't need to wait for Pipeline builds to finish.
Comments: jayhendren
Comments: CC BY-SA 4.0
Comments: 2019-09-26T22:01:21.257Z
Comments: Thanks @jayhendren. Is it possible to "safely" shutdown Jenkins without suspending pipeline jobs?
Comments: user5056973
Tags: <jenkins>
Title: Why does Jenkins safeRestart not wait for job to finish?
UserDisplayName: user5056973
ThreadText: # Why does Jenkins safeRestart not wait for job to finish?
  
  (Asked by: username_0 on 2019-09-26)
  
  I have a simple pipeline job with a single stage:
  
  ```
  timestamps {
      node ("jenkins-slave-small") {
              stage('Stage 1') {
                  sh "echo 'stage2 echo'; sleep 500s"
              }
      }
  }
  
  ```
  
  I am testing whether or not /safeRestart waits for my pipeline to finish executing, but it seems like it's not.
  
  Logs:
  
  ```
  [Pipeline] { (Stage 1)
  [Pipeline] sh
  + echo stage2 echo
  stage2 echo
  + sleep 500s
  Resuming build at Thu Sep 26 18:14:18 UTC 2019 after Jenkins restart
  Waiting to resume part of test-folder » test-pipeline-no-parallels #6: Finished waiting
  Ready to run at Thu Sep 26 18:14:55 UTC 2019
  
  ```
  
  Am I misunderstanding how /safeRestart is supposed to behave or is that the intended behaviour?
  
  **Tags:** jenkins
  
  > **Comments:**
  > **username_1**: I don't think you are misunderstanding.  I suspect that since Pipeline jobs can (theoretically) be safely suspended and resumed unlike traditional "Freestyle" jobs, safe restart doesn't need to wait for Pipeline builds to finish.  
  > **username_0**: Thanks @username_1. Is it possible to "safely" shutdown Jenkins without suspending pipeline jobs?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-09-30. Score: 0 | Accepted answer
  
  Jenkins pipeline jobs are designed to be serializable to disk, to increase durability (their words not mine). Therefore they are considered safe to be suspended. The only things you can do is either wrap your stages in functions with @NonCPS decorator or using properties make the jobs non-resumable after restart, but this will cause them to not be resumed after restart.
  
  Also, restarting jenkins is not really a thing that happens all the time, so it might not be worth the effort to protect against.
  
  
  ---
  
  