failsafe-lib / failsafe

Fault tolerance and resilience patterns for the JVM
https://failsafe.dev
Apache License 2.0
4.16k stars 295 forks source link

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

Open pandoras-toolbox opened 9 months ago

pandoras-toolbox commented 9 months ago

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?

Tembrel commented 9 months ago

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.

pandoras-toolbox commented 9 months ago

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

pandoras-toolbox commented 9 months ago

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.

jhalterman commented 9 months ago

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-

pandoras-toolbox commented 9 months ago

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.

Tembrel commented 9 months ago

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.)

pandoras-toolbox commented 9 months ago

Okay, I see.