-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use binary backoff for errors by default. Add builder for WorkflowSet…
…tings.
- Loading branch information
Showing
14 changed files
with
111 additions
and
147 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
110 changes: 71 additions & 39 deletions
110
...e/src/main/java/com/nitorcreations/nflow/engine/workflow/definition/WorkflowSettings.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,95 @@ | ||
package com.nitorcreations.nflow.engine.workflow.definition; | ||
|
||
import static java.util.concurrent.TimeUnit.HOURS; | ||
import static java.lang.Math.min; | ||
import static java.util.concurrent.TimeUnit.DAYS; | ||
import static java.util.concurrent.TimeUnit.MINUTES; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.joda.time.DateTime.now; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.joda.time.DateTime; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class WorkflowSettings { | ||
public final int minErrorTransitionDelay; | ||
public final int maxErrorTransitionDelay; | ||
public final int shortTransitionDelay; | ||
public final int immediateTransitionDelay; | ||
public final int maxRetries; | ||
|
||
private final Environment env; | ||
private WorkflowSettings(Builder builder) { | ||
this.minErrorTransitionDelay = builder.minErrorTransitionDelay; | ||
this.maxErrorTransitionDelay = builder.maxErrorTransitionDelay; | ||
this.shortTransitionDelay = builder.shortTransitionDelay; | ||
this.immediateTransitionDelay = builder.immediateTransitionDelay; | ||
this.maxRetries = builder.maxRetries; | ||
} | ||
|
||
private final int errorTransitionDelay; | ||
private final int shortTransitionDelay; | ||
private final int immediateTransitionDelay; | ||
private final int maxRetries; | ||
public static class Builder { | ||
public int maxErrorTransitionDelay; | ||
public int minErrorTransitionDelay; | ||
public int shortTransitionDelay; | ||
public int immediateTransitionDelay; | ||
public int maxRetries; | ||
|
||
@Inject | ||
public WorkflowSettings(Environment env) { | ||
this.env = env; | ||
errorTransitionDelay = getIntegerProperty("nflow.transition.delay.waiterror.ms", (int) HOURS.toMillis(2)); | ||
shortTransitionDelay = getIntegerProperty("nflow.transition.delay.waitshort.ms", (int) SECONDS.toMillis(30)); | ||
immediateTransitionDelay = getIntegerProperty("nflow.transition.delay.immediate.ms", 0); | ||
maxRetries = getIntegerProperty("nflow.max.state.retries", 3); | ||
} | ||
public Builder() { | ||
this(null); | ||
} | ||
|
||
public DateTime getErrorTransitionActivation() { | ||
return now().plusMillis(getErrorTransitionDelay()); | ||
} | ||
public Builder(Environment env) { | ||
minErrorTransitionDelay = getIntegerProperty(env, "nflow.transition.delay.error.min.ms", (int) MINUTES.toMillis(1)); | ||
maxErrorTransitionDelay = getIntegerProperty(env, "nflow.transition.delay.error.max.ms", (int) DAYS.toMillis(1)); | ||
shortTransitionDelay = getIntegerProperty(env, "nflow.transition.delay.waitshort.ms", (int) SECONDS.toMillis(30)); | ||
immediateTransitionDelay = getIntegerProperty(env, "nflow.transition.delay.immediate.ms", 0); | ||
maxRetries = getIntegerProperty(env, "nflow.max.state.retries", 17); | ||
} | ||
|
||
public int getErrorTransitionDelay() { | ||
return errorTransitionDelay; | ||
} | ||
public Builder setMaxErrorTransitionDelay(int maxErrorTransitionDelay) { | ||
this.maxErrorTransitionDelay = maxErrorTransitionDelay; | ||
return this; | ||
} | ||
|
||
public DateTime getShortTransitionActivation() { | ||
return now().plusMillis(getShortTransitionDelay()); | ||
} | ||
public Builder setMinErrorTransitionDelay(int minErrorTransitionDelay) { | ||
this.minErrorTransitionDelay = minErrorTransitionDelay; | ||
return this; | ||
} | ||
|
||
public int getShortTransitionDelay() { | ||
return shortTransitionDelay; | ||
} | ||
public Builder setShortTransitionDelay(int shortTransitionDelay) { | ||
this.shortTransitionDelay = shortTransitionDelay; | ||
return this; | ||
} | ||
|
||
public Builder setImmediateTransitionDelay(int immediateTransitionDelay) { | ||
this.immediateTransitionDelay = immediateTransitionDelay; | ||
return this; | ||
} | ||
|
||
public int getImmediateTransitionDelay() { | ||
return immediateTransitionDelay; | ||
public Builder setMaxRetries(int maxRetries) { | ||
this.maxRetries = maxRetries; | ||
return this; | ||
} | ||
|
||
private int getIntegerProperty(Environment env, String key, int defaultValue) { | ||
if (env != null) { | ||
return env.getProperty(key, Integer.class, defaultValue); | ||
} | ||
return defaultValue; | ||
} | ||
|
||
public WorkflowSettings build() { | ||
return new WorkflowSettings(this); | ||
} | ||
} | ||
|
||
public int getMaxRetries() { | ||
return maxRetries; | ||
public DateTime getErrorTransitionActivation(int retryCount) { | ||
return now() | ||
.plusMillis(calculateBinaryBackoffDelay(retryCount + 1, minErrorTransitionDelay, maxErrorTransitionDelay)); | ||
} | ||
|
||
private int getIntegerProperty(String key, int defaultValue) { | ||
if (env != null) { | ||
return env.getRequiredProperty(key, Integer.class); | ||
} | ||
return defaultValue; | ||
protected int calculateBinaryBackoffDelay(int retryCount, int minDelay, int maxDelay) { | ||
return min(minDelay * (1 << retryCount), maxDelay); | ||
} | ||
|
||
public DateTime getShortTransitionActivation() { | ||
return now().plusMillis(shortTransitionDelay); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.