From 40af3285f4d4e31e798a0e63943348dccad746e3 Mon Sep 17 00:00:00 2001 From: Andreas Fankhauser <23085769+hiddenalpha@users.noreply.github.com> Date: Tue, 7 May 2024 12:36:22 +0200 Subject: [PATCH] [SDCISA-15833, #170] Reduce risk of 'onDone()' from being called too many times. --- .../redisques/scheduling/PeriodicSkipScheduler.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/org/swisspush/redisques/scheduling/PeriodicSkipScheduler.java b/src/main/java/org/swisspush/redisques/scheduling/PeriodicSkipScheduler.java index 03f2a7d..3bcc27b 100644 --- a/src/main/java/org/swisspush/redisques/scheduling/PeriodicSkipScheduler.java +++ b/src/main/java/org/swisspush/redisques/scheduling/PeriodicSkipScheduler.java @@ -4,6 +4,7 @@ import io.vertx.core.Vertx; import org.slf4j.Logger; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; import static java.lang.System.currentTimeMillis; @@ -47,10 +48,16 @@ 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_); } 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(); } @@ -64,6 +71,7 @@ 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 task) { this.task = task;