Search in sources :

Example 1 with Retryable

use of io.kestra.core.annotations.Retryable in project kestra by kestra-io.

the class OverrideRetryInterceptor method intercept.

@Nullable
@Override
public Object intercept(MethodInvocationContext<Object, Object> context) {
    Optional<AnnotationValue<Retryable>> opt = context.findAnnotation(Retryable.class);
    if (opt.isEmpty()) {
        return context.proceed();
    }
    AnnotationValue<Retryable> retry = opt.get();
    MutableRetryState retryState = new SimpleRetry(retry.get("attempts", Integer.class).orElse(5), retry.get("multiplier", Double.class).orElse(2D), retry.get("delay", Duration.class).orElse(Duration.ofSeconds(1)), retry.get("maxDelay", Duration.class).orElse(null), new DefaultRetryPredicate(resolveIncludes(retry, "includes"), resolveIncludes(retry, "excludes")), Throwable.class);
    MutableConvertibleValues<Object> attrs = context.getAttributes();
    attrs.put(RetryState.class.getName(), retry);
    InterceptedMethod interceptedMethod = InterceptedMethod.of(context);
    try {
        retryState.open();
        Object result = retrySync(context, retryState, interceptedMethod);
        switch(interceptedMethod.resultType()) {
            case SYNCHRONOUS:
                retryState.close(null);
                return result;
            default:
                return interceptedMethod.unsupported();
        }
    } catch (Exception e) {
        return interceptedMethod.handleException(e);
    }
}
Also used : Retryable(io.kestra.core.annotations.Retryable) InterceptedMethod(io.micronaut.aop.InterceptedMethod) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) DefaultRetryPredicate(io.micronaut.retry.annotation.DefaultRetryPredicate) RetryState(io.micronaut.retry.RetryState) Nullable(io.micronaut.core.annotation.Nullable)

Aggregations

Retryable (io.kestra.core.annotations.Retryable)1 InterceptedMethod (io.micronaut.aop.InterceptedMethod)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 Nullable (io.micronaut.core.annotation.Nullable)1 RetryState (io.micronaut.retry.RetryState)1 DefaultRetryPredicate (io.micronaut.retry.annotation.DefaultRetryPredicate)1