2

Let's say I have a Jenkins job which runs every 5 minutes and takes only 3 minutes to run, then the job suddenly became to take more than 5 minutes (say 8 minutes). In this case the job queue becomes full as Jenkins blocks the execution of newly added job until the already running job is finished.

I could allow concurrent execution of jobs in the queue, but I prefer that the newly added job is immediately cancelled if the last job is still running. Is this possible for Jenkins? Or should I look for more sophisticated job scheduling solutions?

  • 1
    If there is already a pending job in the queue, a next scheduled job is cancelled automatically. So the queue shouldn't be full of pending jobs (at least as far as I know). Is it any problem for you? – arasio Oct 28 '16 at 12:28
  • @arasio Ahh I didn't test it enough. You're absolutely right. If you post the comment as an answer I'll accept it. Thank you! –  Oct 31 '16 at 02:22
  • That's good if this was useful for you. I wrote it as an answer. – arasio Oct 31 '16 at 12:53

2 Answers2

5

If there is already a pending job in the queue, a next scheduled job is cancelled automatically. So the queue shouldn't be full of pending jobs even if a running job takes twice or much longer than the scheduled period.

arasio
  • 1,260
  • 10
  • 14
  • 1
    I don't think this is true for now. I just checked and found out Jenkins starts a new job before finishing the previous job. (w/ CRON H/30 * * * *) – Jiho Choi Apr 06 '21 at 02:16
0

I think you can use the "Prepare an environment for the run" scripts. When the second job starts up, you can search the build nodes to see if the same job is currently running, and cancel the queued up job. I found this with more information:

Cancel queued builds and aborting executing builds using Groovy for Jenkins

Community
  • 1
  • 1
RobertoS
  • 61
  • 4
  • Thank you for answering. Using Groovy scripts is a possible way, but I actually only need to prevent the build queue from being full of many blocked builds, and I really don't need to do something for that according to @arasio's comment. –  Oct 31 '16 at 09:01