使用 Groovy-Script 在 Jenkins 中进行节点分配的超时



问题:我需要为节点分配设置 2 小时的时间,一旦节点在时间限制内分配,构建应该继续,如果节点未在时间范围内分配,构建应该暂停。

我尝试使用超时功能,但是在这些中,如果分配了节点,一旦达到时间限制,则在执行过程中中止构建。

startTime = System.currentTimeMillis()
timeout(time:2, unit: 'HOURS'){ 
node('Slave_Node') {
// Will run on the slave
}
}

在控制台中,它将在等待期间打印,例如, "等待'Slave_Node'上的下一个可用执行器">

分配节点后, "奔跑Slave_Node">

可以建议实施请。 谢谢:)

请改用这个:

startTime = System.currentTimeMillis()
timeout(activity: true, time: 2, unit: 'HOURS') {
node('Slave_Node') {
// Will run on the slave
}
}

在定义的块的日志中没有活动后,activity: true将超时,而不是绝对持续时间。

这是一个我相信可以独立工作的解决方案 - 尽管要小心,因为它可能不完整。 我无法将其转换为"getNode(("库函数以使其可移植和可重用。

wait_for_node = 120 // minutes
waitNode_mS = wait_for_node * 60 * 1000        // minutes into milliseconds
got_a_node = false
targetLabel = 'DESIRED_AGENT'
bodytimeout = 120 // minutes 
parallel waitfor: {
if (waitNode_mS > 0) {
startms = System.currentTimeMillis()
waitUntil(initialRecurrencePeriod: 5000, quiet: true)
{ got_a_node || ((System.currentTimeMillis() - startms) > waitNode_mS) } // end waitUntil
if (!got_a_node) {
error "ABORTING: Failed to get $targetLabel in $wait_for_node minutes"
} else {
echo "Got a $targetLabel - Exiting waitfor"
}
} else {
echo "WARNING: MAY wait FOREVER."
}// if time argument = 0, exit cleanly and thus will wait indefinitely
}, executeon: {
node(targetLabel) {
got_a_node = true
try {
timeout(time: bodytimeout, unit: 'MINUTES') {
// -------------------------------
// MAIN BODY: DO THE WORK IN HERE
// -------------------------------
} // end bodyTimeout
} catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
throw (e)
} finally {
// When leaving this node, clean up the workspace.
deleteDir()
} // try/catch/finally executing on node
} // exit node(targetLabel)
}, failFast: true

有关此解决方案的说明:

使用
  • wait_for_node(分钟(和bodytimeout(分钟(值选择两个最大时间。
  • 使用parallel步骤,其中waitfor腿使用waitUntil步骤。
  • 如果 waitTil(暴力监视系统时钟(退出,则由于它抛出错误并且并行已failFast: true
  • 如果wait_for_node设置为零,它将永远等待获取节点。(旧行为(
  • 可能存在竞争条件。例如,如果达到waitfor时间并且同时分配节点,它将中止主主体。
  • 内部(主体(作品包裹在timeout()步骤中。
  • 内部waitUntil不是特别有效。它实际上以一个间隔等待的最长时间是 30 秒,因为它旨在进行短暂的等待。

最新更新