Last active
September 16, 2016 22:38
-
-
Save kohsuke/0e5a731701757047f1430ae3213b9216 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // src/main/java/acme/RetryStep.java | |
| ppackage acme; | |
| class RetryStep extends GroovyStep { | |
| private int times; | |
| @DataBoundConstructor | |
| public RetryStep(int times) { | |
| this.times = times; | |
| } | |
| public int getTimes() { | |
| return times; | |
| } | |
| @Extension | |
| public static class DescriptorImpl extends GroovyStepDescriptor { | |
| @Override public String getFunctionName() { | |
| return "retry"; | |
| } | |
| @Override public String getDisplayName() { | |
| return "Retry step"; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // src/main/resources/acme/RetryStepExecution.groovy | |
| // this entire file gets CPS transformed & executed accordingly | |
| public class RetryStepExecution extends GroovyStepExecution { | |
| public Object call(Closure body) { | |
| // should allow arbitrary code in trusted classpath | |
| int attempt = 0; | |
| while (true) { | |
| try { | |
| echo "trying ${attempt}" | |
| body(); | |
| return; | |
| } catch (Exception e) { | |
| if (attempt++ < step.times) { | |
| continue; // retry | |
| } else { | |
| throw e; | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment