Jenkins job running system Groovy script how to respond to user kill

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I have a long running System Groovy script that launches tens thousands of other builds. In order not to make the build queue overly long, which will make the UI unusable, it monitors the length of the build queue. If the build queue is longer than a given threshold, it will not launch any new builds and sleep for one minute.

The problem is, this script does not respond to user kill actions. When the user clicks the "kill this build" button on the UI, nothing happens. I wonder if there is a way for the system Groovy script to check if the current build should be killed, so it will quit its sleep and wait loop?

I tried to monitor Executor.shouldRun(), but it is not changed by user's kill action.

回答1:

I looked into issues with the Kill button in the past. My understanding is that the kill signal is actually sent right away to the job. However, if the job is busy within some other API (or something along that line) it is not guarantied that it will be picked up. However, the kill signal will not be stored, either the job reacts on it right away or it will be ignored. This might be the case here when you sleep for a minute.

So as a work around, I would create my own mechanism to signal to your script that it should stop. This can be done by placing a file into a specific location (e.g. the worspace of the groovy script job). This can be done with a second job called Kill_job_1. Now, before your job starts sleeping (or right after it wakes up), you check for existence of that file and end your script if the file is found. Don't forget to make sure that the file is cleaned up when you start your job a second time.



回答2:

Just have dealt with it now and here are my findings:

The job is cancelled by the Thread.interrupt() method within the Executor object and the evaluated groovy isn't aware about it. I have solved it by waiting on the Executor from the evaluated script. The Executor can be retrieved from the bound build parameter.

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!