Skip to content

Commit

Permalink
SONAR-10026 Simplify running flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses committed Nov 6, 2017
1 parent f0971e5 commit 7b64b5d
Showing 1 changed file with 22 additions and 31 deletions.
Expand Up @@ -24,7 +24,6 @@
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableScheduledFuture; import com.google.common.util.concurrent.ListenableScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Logger;
Expand Down Expand Up @@ -104,7 +103,7 @@ public void stopScheduling() {
} }


private class ChainingCallback implements FutureCallback<CeWorker.Result> { private class ChainingCallback implements FutureCallback<CeWorker.Result> {
private final AtomicBoolean keepRunning = new AtomicBoolean(true); private volatile boolean keepRunning = true;
private final CeWorker worker; private final CeWorker worker;


@CheckForNull @CheckForNull
Expand All @@ -116,19 +115,21 @@ public ChainingCallback(CeWorker worker) {


@Override @Override
public void onSuccess(@Nullable CeWorker.Result result) { public void onSuccess(@Nullable CeWorker.Result result) {
if (result == null) { if (keepRunning) {
chainWithEnabledTaskDelay(); if (result == null) {
} else { chainWithEnabledTaskDelay();
switch (result) { } else {
case DISABLED: switch (result) {
chainWithDisabledTaskDelay(); case DISABLED:
break; chainWithDisabledTaskDelay();
case NO_TASK: break;
chainWithEnabledTaskDelay(); case NO_TASK:
break; chainWithEnabledTaskDelay();
case TASK_PROCESSED: break;
default: case TASK_PROCESSED:
chainWithoutDelay(); default:
chainWithoutDelay();
}
} }
} }
} }
Expand All @@ -137,44 +138,34 @@ public void onSuccess(@Nullable CeWorker.Result result) {
public void onFailure(Throwable t) { public void onFailure(Throwable t) {
if (t instanceof Error) { if (t instanceof Error) {
LOG.error("Compute Engine execution failed. Scheduled processing interrupted.", t); LOG.error("Compute Engine execution failed. Scheduled processing interrupted.", t);
} else { } else if (keepRunning) {
chainWithoutDelay(); chainWithoutDelay();
} }
} }


private void chainWithoutDelay() { private void chainWithoutDelay() {
if (keepRunning()) { workerFuture = executorService.submit(worker);
workerFuture = executorService.submit(worker);
}
addCallback(); addCallback();
} }


private void chainWithEnabledTaskDelay() { private void chainWithEnabledTaskDelay() {
if (keepRunning()) { workerFuture = executorService.schedule(worker, delayBetweenEnabledTasks, timeUnit);
workerFuture = executorService.schedule(worker, delayBetweenEnabledTasks, timeUnit);
}
addCallback(); addCallback();
} }


private void chainWithDisabledTaskDelay() { private void chainWithDisabledTaskDelay() {
if (keepRunning()) { workerFuture = executorService.schedule(worker, DELAY_BETWEEN_DISABLED_TASKS, timeUnit);
workerFuture = executorService.schedule(worker, DELAY_BETWEEN_DISABLED_TASKS, timeUnit);
}
addCallback(); addCallback();
} }


private void addCallback() { private void addCallback() {
if (workerFuture != null && keepRunning()) { if (workerFuture != null) {
Futures.addCallback(workerFuture, this); Futures.addCallback(workerFuture, this);
} }
} }


private boolean keepRunning() {
return keepRunning.get();
}

public void stop(boolean interrupt) { public void stop(boolean interrupt) {
this.keepRunning.set(false); keepRunning = false;
if (workerFuture != null) { if (workerFuture != null) {
workerFuture.cancel(interrupt); workerFuture.cancel(interrupt);
} }
Expand Down

0 comments on commit 7b64b5d

Please sign in to comment.