Skip to content

Instantly share code, notes, and snippets.

@kohsuke
Last active September 16, 2016 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohsuke/0e5a731701757047f1430ae3213b9216 to your computer and use it in GitHub Desktop.
Save kohsuke/0e5a731701757047f1430ae3213b9216 to your computer and use it in GitHub Desktop.
// 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";
}
}
}
// 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