/** * Calls {@code doWork()} with proper error handling and then {@code loop()} again to schedule the next call. */ private void executeWork() { try { if (orchestration == null || orchestration.tryExecuteBackgroundLoop(getName())) { try { Watch w = Watch.start(); LocalDateTime now = LocalDateTime.now(); String executedWork = doWork(); buildAndLogExecutionInfo(w, now, executedWork); } finally { if (orchestration != null) { orchestration.backgroundLoopCompleted(getName(), executionInfo); } } } } catch (Exception e) { Exceptions.handle(Tasks.LOG, e); } loop(); }
@Override public String toString() { return "BackgroundLoop '" + getName() + "': " + getExecutionInfo(); }
/** * Calls {@link #executeWork()} in the determined executor. * <p> * This is kind of the main loop, as {@code executeWork()} will call {@code loop()} once the computation * ({@code doWork()} is finished. Using {@link ExecutionBuilder#frequency(Object, double)} this is limited to the * call frequency as determined by {@code maxCallFrequency()}. */ protected void loop() { tasks.executor(determineExecutor()).frequency(this, maxCallFrequency()).start(this::executeWork); }
private void buildAndLogExecutionInfo(Watch watch, LocalDateTime startedAt, String executedWorkDescription) { if (executedWorkDescription != null) { executionInfo = NLS.toUserString(startedAt) + " (" + watch.duration() + "): " + executedWorkDescription; Log.BACKGROUND.FINE(getName() + ": " + executionInfo); } else { executionInfo = NLS.toUserString(startedAt) + " (" + watch.duration() + ") - no work executed..."; } }
output.separator(); for (BackgroundLoop loop : loops) { output.apply("%-60s %s", loop.getName(), loop.getExecutionInfo());