-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement an exception handler for api client
Signed-off-by: Jorge Aguilera <[email protected]>
- Loading branch information
Showing
5 changed files
with
175 additions
and
31 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
29 changes: 29 additions & 0 deletions
29
plugins/nf-nomad/src/main/nextflow/nomad/config/RetryConfig.groovy
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package nextflow.nomad.config | ||
|
||
import groovy.transform.CompileStatic | ||
import nextflow.util.Duration | ||
|
||
|
||
@CompileStatic | ||
class RetryConfig { | ||
|
||
Duration delay = Duration.of('250ms') | ||
Duration maxDelay = Duration.of('90s') | ||
int maxAttempts = 10 | ||
double jitter = 0.25 | ||
|
||
RetryConfig(){ | ||
this(Collections.emptyMap()) | ||
} | ||
|
||
RetryConfig(Map config){ | ||
if( config.delay ) | ||
delay = config.delay as Duration | ||
if( config.maxDelay ) | ||
maxDelay = config.maxDelay as Duration | ||
if( config.maxAttempts ) | ||
maxAttempts = config.maxAttempts as int | ||
if( config.jitter ) | ||
jitter = config.jitter as double | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
plugins/nf-nomad/src/main/nextflow/nomad/executor/FailsafeExecutor.groovy
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package nextflow.nomad.executor | ||
|
||
import dev.failsafe.Failsafe | ||
import dev.failsafe.RetryPolicy | ||
import dev.failsafe.event.EventListener | ||
import dev.failsafe.event.ExecutionAttemptedEvent | ||
import dev.failsafe.function.CheckedSupplier | ||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Slf4j | ||
import io.nomadproject.client.ApiException | ||
import nextflow.nomad.config.RetryConfig | ||
|
||
import java.time.temporal.ChronoUnit | ||
import java.util.concurrent.TimeoutException | ||
import java.util.function.Predicate | ||
|
||
@Slf4j | ||
@CompileStatic | ||
class FailsafeExecutor { | ||
|
||
private RetryConfig config | ||
|
||
FailsafeExecutor(RetryConfig config){ | ||
this.config = config | ||
} | ||
|
||
protected <T> RetryPolicy<T> retryPolicy(Predicate<? extends Throwable> cond) { | ||
|
||
final listener = new EventListener<ExecutionAttemptedEvent<T>>() { | ||
@Override | ||
void accept(ExecutionAttemptedEvent<T> event) throws Throwable { | ||
log.debug("Nomad TooManyRequests response error - attempt: ${event.attemptCount}; reason: ${event.lastFailure.message}") | ||
} | ||
} | ||
return RetryPolicy.<T>builder() | ||
.handleIf(cond) | ||
.withBackoff(config.delay.toMillis(), config.maxDelay.toMillis(), ChronoUnit.MILLIS) | ||
.withMaxAttempts(config.maxAttempts) | ||
.withJitter(config.jitter) | ||
.onRetry(listener) | ||
.build() | ||
} | ||
|
||
final private static List<Integer> RETRY_CODES = List.of(408, 429, 500, 502, 503, 504) | ||
|
||
protected <T> T apply(CheckedSupplier<T> action) { | ||
// define the retry condition | ||
final cond = new Predicate<? extends Throwable>() { | ||
@Override | ||
boolean test(Throwable t) { | ||
if( t instanceof ApiException && t.code in RETRY_CODES ) | ||
return true | ||
if( t instanceof IOException || t.cause instanceof IOException ) | ||
return true | ||
if( t instanceof TimeoutException || t.cause instanceof TimeoutException ) | ||
return true | ||
return false | ||
} | ||
} | ||
// create the retry policy object | ||
final policy = retryPolicy(cond) | ||
// apply the action with | ||
return Failsafe.with(policy).get(action) | ||
} | ||
|
||
} |
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