我们的Jenkins服务器有一个已经运行了三天的作业,但是没有做任何事情。单击角落里的小X没有任何作用,控制台输出日志也没有显示任何内容。我在我们的构建服务器上检查过,该作业实际上似乎根本没有在运行。

有没有办法告诉jenkins工作已经“完成”了,比如编辑一些文件或锁之类的?因为我们有很多任务,所以我们并不想重新启动服务器。


当前回答

最近我遇到了一个节点/代理,它的一个执行程序被管道作业的构建“X”占用了几天,尽管该作业页面声称构建“X”不再存在(在后续10个构建后被丢弃(!),正如管道作业中配置的那样)。在磁盘上验证:构建“X”真的消失了。

解决方案:代理/节点错误地报告了被占用的执行程序正在忙着运行构建“X”。中断该执行程序的线程会立即释放它。

def executor = Jenkins.instance.getNode('NODENAME').computer.executors.find {
    it.isBusy() && it.name.contains('JOBNAME')
}

println executor?.name
if (executor?.isBusy()) executor.interrupt()

考虑的其他答案:

来自@cheffe的答案:没有工作(见下一点,并在下面更新)。 thread. getallstacktraces()的答案:没有匹配的线程。 来自@levente-holló的答案和getBuildByNumber()的所有答案:不适用,因为构建已经不存在了! 来自@austinfromboston的答案:这接近于我的需求,但它也会破坏目前正在运行的任何其他构建。

更新: 我再次经历了类似的情况,Executor被一个(仍然存在的)已完成的管道构建占用了数天。这个代码片段是唯一可行的解决方案。

其他回答

非常简单的解决方案

我看到这个问题的原因是页面上不正确的http链接,而不是应该停止工作的https。所有你需要做的是编辑onclick属性在html页面,通过以下

Open up a console log of the job (pipeline) that got hang Click whatever is available to kill the job (x icon, "Click here to forcibly terminate running steps" etc) to get "Click here to forcibly kill entire build" link visible (it's NOT gonna be clickable at the moment) Open the browser's console (use any one of three for chrome: F12; ctrl + shift + i; menu->more tools->developer tools) Locate "Click here to forcibly kill entire build" link manually or using "select an element in the page" button of the console Double click on onclick attribute to edit its value Append s to http to have https Press enter to submit the changes Click "Click here to forcibly kill entire build" link

使用截图作为参考

构建超时插件在这种情况下可以派上用场。如果花费太长时间,它会自动终止工作。

我看了Jenkins源代码,似乎我要做的是不可能的,因为停止一个作业似乎是通过线程中断完成的。我不知道为什么这个工作被搁置了。

编辑:

就业势不可挡的可能原因:

如果Jenkins陷入了无限循环,它就永远不能中止。 如果Jenkins在Java虚拟机中进行网络或文件I/O(例如长时间的文件复制或SVN更新),则不能中止。

进入"Manage Jenkins" > "Script Console"在服务器上运行一个脚本以中断挂起的线程。

您可以使用Thread.getAllStackTraces()获取所有活动线程,并中断正在挂起的线程。

Thread.getAllStackTraces().keySet().each() {
  t -> if (t.getName()=="YOUR THREAD NAME" ) {   t.interrupt();  }
}

更新:

上述使用线程的解决方案可能不适用于最新的Jenkins版本。要中断冻结的管道,请参考这个解决方案(由alexandru-bantiuc),并运行:

Jenkins.instance.getItemByFullName("JobName")
                .getBuildByNumber(JobNumber)
                .finish(
                        hudson.model.Result.ABORTED,
                        new java.io.IOException("Aborting build")
                );

第一个提议的解决方案非常接近。如果使用stop()而不是interrupt(),它甚至会杀死在groovy系统脚本中无休止地运行的失控线程。这将杀死任何构建,任何工作。 代码如下:

Thread.getAllStackTraces().keySet().each() {
    if (it.name.contains('YOUR JOBNAME')) {  
      println "Stopping $it.name"
      it.stop()
    }
}