From 7b64b5d27664c32bcca218cfbd2f89eb2dff845f Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 31 Oct 2017 16:02:49 +0100 Subject: [PATCH] SONAR-10026 Simplify running flag --- .../CeProcessingSchedulerImpl.java | 53 ++++++++----------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeProcessingSchedulerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeProcessingSchedulerImpl.java index 12b376398fc7..c3549ca18d3c 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeProcessingSchedulerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeProcessingSchedulerImpl.java @@ -24,7 +24,6 @@ import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableScheduledFuture; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.utils.log.Logger; @@ -104,7 +103,7 @@ public void stopScheduling() { } private class ChainingCallback implements FutureCallback { - private final AtomicBoolean keepRunning = new AtomicBoolean(true); + private volatile boolean keepRunning = true; private final CeWorker worker; @CheckForNull @@ -116,19 +115,21 @@ public ChainingCallback(CeWorker worker) { @Override public void onSuccess(@Nullable CeWorker.Result result) { - if (result == null) { - chainWithEnabledTaskDelay(); - } else { - switch (result) { - case DISABLED: - chainWithDisabledTaskDelay(); - break; - case NO_TASK: - chainWithEnabledTaskDelay(); - break; - case TASK_PROCESSED: - default: - chainWithoutDelay(); + if (keepRunning) { + if (result == null) { + chainWithEnabledTaskDelay(); + } else { + switch (result) { + case DISABLED: + chainWithDisabledTaskDelay(); + break; + case NO_TASK: + chainWithEnabledTaskDelay(); + break; + case TASK_PROCESSED: + default: + chainWithoutDelay(); + } } } } @@ -137,44 +138,34 @@ public void onSuccess(@Nullable CeWorker.Result result) { public void onFailure(Throwable t) { if (t instanceof Error) { LOG.error("Compute Engine execution failed. Scheduled processing interrupted.", t); - } else { + } else if (keepRunning) { chainWithoutDelay(); } } private void chainWithoutDelay() { - if (keepRunning()) { - workerFuture = executorService.submit(worker); - } + workerFuture = executorService.submit(worker); addCallback(); } private void chainWithEnabledTaskDelay() { - if (keepRunning()) { - workerFuture = executorService.schedule(worker, delayBetweenEnabledTasks, timeUnit); - } + workerFuture = executorService.schedule(worker, delayBetweenEnabledTasks, timeUnit); addCallback(); } private void chainWithDisabledTaskDelay() { - if (keepRunning()) { - workerFuture = executorService.schedule(worker, DELAY_BETWEEN_DISABLED_TASKS, timeUnit); - } + workerFuture = executorService.schedule(worker, DELAY_BETWEEN_DISABLED_TASKS, timeUnit); addCallback(); } private void addCallback() { - if (workerFuture != null && keepRunning()) { + if (workerFuture != null) { Futures.addCallback(workerFuture, this); } } - private boolean keepRunning() { - return keepRunning.get(); - } - public void stop(boolean interrupt) { - this.keepRunning.set(false); + keepRunning = false; if (workerFuture != null) { workerFuture.cancel(interrupt); }