-
-
Save anonymous/e03d7625aa8632e8932203b1c7d785ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Handler<T, U extends QueuedItem<T>> { | |
protected final void handleQueuedItem( | |
Id<U> id, | |
Function<T, Result> action, | |
Class<U> rowClass) { | |
try { | |
T item = inTransaction(dbConnection -> { | |
U row = dbConnection.get(rowClass, id); | |
if (row.getPolledAt() == null) { | |
row.setPolledAt(clock.get()); | |
return item; | |
} | |
return null; | |
}); | |
if (item != null) { | |
Result result; | |
try { | |
result = action.apply(item) | |
} catch (Exception exception) { | |
logError(exception); | |
result = new Failure(exception); | |
} | |
inTransaction(dbConnection -> { | |
U row = dbConnection.get(rowClass, id); | |
if (result.isSuccess()) { | |
row.setSucceededAt(clock.get()); | |
} else { | |
row.recordFailure(clock.get(), result); | |
} | |
}) | |
} | |
} catch (Exception exception) { | |
logError(exception); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment