public void loopUntilExhausted(ThrowingAction action, long delay, TimeUnit delayUnit) throws Exception { while (!elapsed()) { action.run(); if (elapsed(delayUnit) < delay) { break; } sleep(delay, delayUnit); } }
public static <T> T retryUntilSuccessOrExhausted(Span span, ComputingAction<T> action, IRetryPolicy policy, IDelay delay) throws HyracksDataException { Throwable failure; int attempt = 0; do { attempt++; try { return action.compute(); } catch (Throwable th) { failure = th; if (!policy.retry(th)) { break; } try { LOGGER.log(Level.WARN, "Failure executing action {} for the {} time", action, attempt, failure); span.sleep(delay.calculate(attempt), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw HyracksDataException.create(e); } } } while (!span.elapsed()); LOGGER.log(Level.WARN, "Final Failure executing action {} after {} attempts", action, attempt, failure); throw HyracksDataException.create(failure); } }