Jenkins job running system groovy script how to respond to user killing

I have a long Groovy script system that runs tens of thousands of other builds. In order not to make the build queue too long, which makes the user interface unusable, it controls the length of the build queue. If the assembly queue is longer than the specified threshold, it will not start any new assemblies and sleep for one minute.

The problem is that this script is not responding to user actions. When the user clicks the kill this assembly button in the user interface, nothing happens. I wonder if there is a way for the Groovy script system to check if the current assembly should be killed, so it will exit the wait and wait loop?

I tried to control Executor.shouldRun (), but it is not changed by user action.

+3
source share
3 answers

I have studied issues with the Kill button in the past. I understand that the murder signal is actually sent immediately to the mission. However, if the task is occupied by some other API (or something in this direction), this does not guarantee that it will be raised. However, the destruction signal will not be saved, or the work will immediately respond to it, or it will be ignored. This may be the case here when you sleep for a minute.

So, as work, I would create my own mechanism to tell your script that it should stop. This can be done by placing the file in a specific location (for example, in the groovy script job space). This can be done using the second job Kill_job_1. Now, before your work starts to sleep (or immediately after it wakes up), you check for the presence of this file and end your script if the file is found. Remember to make sure the file is cleaned when you start your work a second time.

0
source

Just figured it out now, and here are my conclusions:

The task is canceled by the Thread.interrupt() method in the Executor object, and the evaluated groovy does not know about it. I solved this by waiting for the Executor from the evaluated script. Executor can be obtained from the bound build parameter.

 Executor executor = getBinding().getVariable('build').getExecutor() synchronized (executor){ executor.wait(timeTowait) } 
+3
source

It also seems to work

 Thread.sleep(timeToWait) 
0
source

Source: https://habr.com/ru/post/1232320/


All Articles