groovy Gradle任务失败时重试

6rqinv9w  于 2023-04-19  发布在  其他
关注(0)|答案(1)|浏览(147)

bounty将在2天后过期。回答此问题可获得+50的声誉奖励。Parag Kadam正在寻找来自声誉良好来源的答案

这是间歇性失败的任务,当它手动重新运行时,它通常会通过,我想以编程方式重试此任务3次,每次重试之间有5秒的延迟,直到将其声明为真正的失败:

uploadWildFlyWar {
    dependsOn createAndCopySasJar
    description 'Uploads the Wild-fly war'
    group 'publishing'
    repositories {
        mavenDeployer {
            repository(url: repoProps["remoteRepoUrlVal"]) {
                authentication(userName: repoProps["remoteRepoUsernameVal"], password: repoProps["remoteRepoPasswordVal"])
            }
        }
    }
}

故障原因:-
出了什么问题:任务“:commons:src:uploadWildFlyWar”执行失败。无法发布配置“wildFlyWar”无法部署项目:Could not transfer artifact common:wildfly-startup:war:分支-8.8.3.5c8e849b5 from/to remote(https://artifactory.abc.com/artifactory/cprms-build-branch):无法传输文件:https://artifactory.abc.com/artifactory/cprms-build-branch/com/ideas/cpro/common/wildfly-startup/Branch-8.8.3.5c8e849b5/wildfly-startup-Branch-8.8.3.5c8e849b5.war.返回代码为:502,ReasonPhrase:错误网关。

kfgdxczn

kfgdxczn1#

假设你不是在寻找一个“干净”的解决方案,你可以像这样修改它:

// configure the uploadWildFlyWar task
uploadWildFlyWar { task ->
    // remember the original task actions, copying them to a new list as the  returned list is a live view
    def actions = [*task.actions]
    // replace the original task actions with one own task action that does the error handling
    task.actions = [{
        // remembered exceptions from first two runs
        def exceptions = []
        // try up to three times
        for (i in 1..3) {
            try {
                // execute the original actions
                actions.each { it.execute(task) }
                // if the original actions executed successfully, break the loop
                break
            } catch (e) {
                // on the third try if still throwing an exception
                if (i == 3) {
                    // add the first two exceptions as "suppressed" ones
                    exceptions.each { e.addSuppressed(it) }
                    // rethrow the exception
                    throw e
                } else {
                    // remember the first two exceptions
                    exceptions.add(e)
                    // wait 5 seconds before retrying
                    sleep 5000
                }
            }
        }
    } as Action]
}

相关问题