找到你要的答案

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

Q:詹金斯的工作运行Groovy脚本如何响应用户的杀

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.

我有一个长时间运行的系统脚本,推出数以万计的其他版本。为了不使建造队列过长,使UI可用,它监视生成队列的长度。如果生成队列长于给定的阈值,它将不会启动任何新的生成并休眠一分钟。

问题是,此脚本不响应用户杀死操作。当用户点击用户界面上的“杀死这个构建”按钮时,什么都不会发生。我不知道是否有系统的Groovy脚本检查如果当前版本应该被杀死,那么它将退出其睡眠和等待循环?

我试图监控执行。shouldrun(),但它不是由用户的杀人行动改变。

answer1: 回答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.

我看了过去的杀人按钮的问题。我的理解是,杀死信号实际上是马上发送到工作。然而,如果工作在其他一些API是忙碌的(或者沿着线)不保证它将回升。但是,杀死信号不会被存储,无论是工作反应立即或将被忽略。这可能是在这里,当你睡了一分钟。

因此,作为一个工作,我会创建自己的机制,以指示您的脚本,它应该停止。这可以通过将一个文件到一个特定的位置(例如Groovy脚本做的工作worspace)。这可以用一个二工作称为kill_job_1。现在,在你的工作开始睡觉之前(或者在它醒来之后),你检查文件的存在,如果文件被发现,你就结束脚本。第二次启动工作时,不要忘记确保文件被清理干净。

answer2: 回答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)
}

刚才处理了,这是我的发现:

这项工作是由线程取消。interrupt()方法的执行者和评价对象内的常规不知道它。我已从评估剧本中等待执行者,解决了它。执行器可以从绑定生成参数中检索。

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