failsafe icon indicating copy to clipboard operation
failsafe copied to clipboard

Having a context object in the run(...) and get(...) methods

Open pandoras-toolbox opened this issue 2 years ago • 7 comments

A suggestion about the method FailsafeExecutor#run(dev.failsafe.function.CheckedRunnable) and the similar get method.

I think it would be nice if a ExecutionEvent variable would be available like in the method RetryPolicyBuilder#onFailedAttempt(...)

There are some things which you cannot do with the available features of Failsafe if there is no such information in the execution block itself, I mean without workarounds or doing it in an awkward way.

What do you think?

pandoras-toolbox avatar Nov 06 '23 13:11 pandoras-toolbox

Doesn't FailsafeExecutor::run(ContextualRunnable<Void>) give you that already?

Failsafe.with(...)
    .run(ctx -> {
        int count = ctx.getExecutionCount();
        ... do stuff with count, maybe throw checked exception ...
    });

As far as I know, the ExecutionEvent types just offer subsets of ExecutionContext information.

Tembrel avatar Nov 06 '23 13:11 Tembrel

Oh, yes, you are right, I overlooked that method. Thank you!

pandoras-toolbox avatar Nov 06 '23 14:11 pandoras-toolbox

But one question, I cannot obtain the duration for the next attempt? I wanted to log that it will wait now for that duration until retrying again.

pandoras-toolbox avatar Nov 06 '23 14:11 pandoras-toolbox

You can configure an OnRetryScheduled event listener on the retry policy, and use event.getDelay(). See:

https://failsafe.dev/retry/#event-listeners https://failsafe.dev/javadoc/core/dev/failsafe/RetryPolicyBuilder.html#onRetryScheduled-dev.failsafe.event.EventListener-

jhalterman avatar Nov 06 '23 15:11 jhalterman

Thank you. I cannot do exactly what I wanted in an elegant way because the information is not available in the run(...) block, but nevermind.

pandoras-toolbox avatar Nov 06 '23 16:11 pandoras-toolbox

There's no way, elegant or otherwise, to supply the delay before the next execution attempt within the current attempt, because you don't in general know whether the current attempt will be seen as a failure or, if a delay function is used, what delay will be computed. (The delay function can use the result of the failed attempt to determine the delay.)

Tembrel avatar Nov 06 '23 16:11 Tembrel

Okay, I see.

pandoras-toolbox avatar Nov 06 '23 16:11 pandoras-toolbox