Skip to content

Commit

Permalink
Ensuring stacktrace on error in Scheduled processing in StepEngine.sc…
Browse files Browse the repository at this point in the history
…heduleExecutor (TweetWallFX#1963)
  • Loading branch information
mklaehn authored Oct 7, 2024
1 parent 34b309f commit 682cc01
Showing 1 changed file with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,43 @@ private void initScheduledDataProvider(final DataProvider.Scheduled scheduled) {
final DataProvider.ScheduledConfig sc = scheduled.getScheduleConfig();

try {
final Runnable r = exceptionLoggingRunnable(scheduled);

if (DataProvider.ScheduleType.FIXED_DELAY == sc.scheduleType()) {
scheduleExecutor.scheduleAtFixedRate(scheduled, sc.initialDelay(), sc.scheduleDuration(), TimeUnit.SECONDS);
scheduleExecutor.scheduleAtFixedRate(r, sc.initialDelay(), sc.scheduleDuration(), TimeUnit.SECONDS);
} else {
scheduleExecutor.scheduleWithFixedDelay(scheduled, sc.initialDelay(), sc.scheduleDuration(), TimeUnit.SECONDS);
scheduleExecutor.scheduleWithFixedDelay(r, sc.initialDelay(), sc.scheduleDuration(), TimeUnit.SECONDS);
}
} catch (final RuntimeException re) {
LOGGER.error("failed to initializing Scheduled: {}", scheduled, re);
throw re;
}
}

/**
* Wrapps the given Runnable in a try-catch block logging any exception
* produced by the wrapped {@link Runnable}.
*
* When using {@link java.util.concurrent.ExecutorService} instances
* produced by {@link Executors} no stacktrace will be produced. It could
* normally be handled in
* {@link java.util.concurrent.ThreadPoolExecutor#afterExecute(java.lang.Runnable, java.lang.Throwable)}
* but the default implementation is a no-op so none will be printed.
*
* @param r the {@link Runnable} to wrap
*
* @return the wrapped {@link Runnable}
*/
private static Runnable exceptionLoggingRunnable(final Runnable r) {
return () -> {
try {
r.run();
} catch (final Exception e) {
LOGGER.error("#### Runnable {} failed with: ", r, e);
}
};
}

private void awaitScheduledDataProviderInitialization(final DataProvider.Scheduled scheduled) {
while (!scheduled.isInitialized()) {
try {
Expand Down Expand Up @@ -279,13 +305,13 @@ private void process() {
}
});
} else {
try {
stepToExecute.doStep(context);
} catch (RuntimeException | Error e) {
LOG.error("StepExecution has terminal failure {} ", stepToExecute.getClass().getSimpleName(), e);
// enforce that animation continues
context.proceed();
}
try {
stepToExecute.doStep(context);
} catch (RuntimeException | Error e) {
LOG.error("StepExecution has terminal failure {} ", stepToExecute.getClass().getSimpleName(), e);
// enforce that animation continues
context.proceed();
}
}

final long stop = System.currentTimeMillis();
Expand Down

0 comments on commit 682cc01

Please sign in to comment.