Skip to content

Commit

Permalink
[SDCISA-15833, swisspost#170] Fully prevent 'onDone()' from being cal…
Browse files Browse the repository at this point in the history
…led too often.
  • Loading branch information
hiddenalpha committed May 7, 2024
1 parent 40af328 commit 53733bd
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.swisspush.redisques.scheduling;

import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import org.slf4j.Logger;

Expand Down Expand Up @@ -48,16 +49,15 @@ private void onTrigger(Timer timer) {
return;
}
timer.begEpochMs = currentTimeMillis();
boolean oldValue = timer.isPending.getAndSet(true);
assert !oldValue : "Why is this already pending?";
timer.task.accept(timer::onTaskDone_);
Promise<Void> p = Promise.promise();
var fut = p.future();
fut.onSuccess((Void v) -> timer.onTaskDone_());
fut.onFailure(ex -> log.error("This is expected to be UNREACHABLE", ex));
timer.task.accept(p::complete);
}

private void onTaskDone(Timer timer) {
boolean oldVal = timer.isPending.getAndSet(false);
if (!oldVal) {
throw new IllegalStateException("MUST NOT be called multiple times!");
}
timer.endEpochMs = currentTimeMillis();
}

Expand All @@ -71,7 +71,6 @@ public class Timer {
private long id;
// When the last run has begun and end.
private long begEpochMs, endEpochMs;
private final AtomicBoolean isPending = new AtomicBoolean();

private Timer(Consumer<Runnable> task) {
this.task = task;
Expand Down

0 comments on commit 53733bd

Please sign in to comment.