Skip to content

Commit 2c675de

Browse files
Add support to use both retry policy and handler at the same time
Signed-off-by: Mason <[email protected]>
1 parent e3e1a5b commit 2c675de

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

client/src/main/java/io/dapr/durabletask/TaskOrchestrationExecutor.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,8 @@ public <V> Task<V> callSubOrchestrator(
406406

407407
private <V> Task<V> createAppropriateTask(TaskFactory<V> taskFactory, TaskOptions options) {
408408
// Retry policies and retry handlers will cause us to return a RetriableTask<V>
409-
if (options != null && options.hasRetryPolicy()) {
410-
return new RetriableTask<V>(this, taskFactory, options.getRetryPolicy());
411-
} if (options != null && options.hasRetryHandler()) {
412-
return new RetriableTask<V>(this, taskFactory, options.getRetryHandler());
409+
if (options != null && (options.hasRetryPolicy() || options.hasRetryHandler())) {
410+
return new RetriableTask<V>(this, taskFactory, options.getRetryPolicy(), options.getRetryHandler());
413411
} else {
414412
// Return a single vanilla task without any wrapper
415413
return taskFactory.create();
@@ -1170,25 +1168,29 @@ private boolean shouldRetry() {
11701168
return false;
11711169
}
11721170

1173-
if (this.policy != null) {
1174-
logger.warning("Performing retires based on policy");
1175-
1176-
return this.shouldRetryBasedOnPolicy();
1177-
} else if (this.handler != null) {
1178-
RetryContext retryContext = new RetryContext(
1179-
this.context,
1180-
this.attemptNumber,
1181-
this.lastFailure,
1182-
this.totalRetryTime);
1183-
return this.handler.handle(retryContext);
1184-
} else {
1171+
if(this.policy == null && this.handler == null) {
11851172
// We should never get here, but if we do, returning false is the natural behavior.
11861173
return false;
11871174
}
1175+
1176+
RetryContext retryContext = new RetryContext(
1177+
this.context,
1178+
this.attemptNumber,
1179+
this.lastFailure,
1180+
this.totalRetryTime);
1181+
1182+
// These must default to true if not provided, so it is possible to use only one of them at a time
1183+
boolean shouldRetryBasedOnPolicy = this.policy == null || this.shouldRetryBasedOnPolicy();
1184+
boolean shouldRetryBasedOnHandler = this.handler == null || this.handler.handle(retryContext);
1185+
1186+
logger.info(() -> String.format("Retry policy provided: %s, shouldRetryBasedOnPolicy: %s", this.policy != null, shouldRetryBasedOnPolicy));
1187+
logger.info(() -> String.format("Retry handler provided: %s, shouldRetryBasedOnHandler: %s", this.handler != null, shouldRetryBasedOnHandler));
1188+
1189+
return shouldRetryBasedOnPolicy && shouldRetryBasedOnHandler;
11881190
}
11891191

11901192
private boolean shouldRetryBasedOnPolicy() {
1191-
logger.warning(() -> String.format("%d retries out of total %d performed ", this.attemptNumber, this.policy.getMaxNumberOfAttempts()));
1193+
logger.warning(() -> String.format("Retry Policy: %d retries out of total %d performed ", this.attemptNumber, this.policy.getMaxNumberOfAttempts()));
11921194

11931195
if (this.attemptNumber >= this.policy.getMaxNumberOfAttempts()) {
11941196
// Max number of attempts exceeded

0 commit comments

Comments
 (0)