Skip to content

Commit

Permalink
graceful shutdown sharedprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
yue9944882 authored and brendandburns committed Aug 11, 2020
1 parent c59623f commit fdccec2
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.commons.collections4.CollectionUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
* SharedProcessor class manages all the registered ProcessorListener and distributes notifications.
*/
public class SharedProcessor<ApiType extends KubernetesObject> {

private static final Logger log = LoggerFactory.getLogger(SharedProcessor.class);

private ReadWriteLock lock = new ReentrantReadWriteLock();

private List<ProcessorListener<ApiType>> listeners;
Expand Down Expand Up @@ -155,16 +159,16 @@ public void stop() {
} finally {
lock.writeLock().unlock();
}
// Disable new tasks from being submitted
executorService.shutdown();
// Interrupts running listeners by signalling InterruptedException
executorService.shutdownNow();
try {
// Wait a while for existing tasks to terminate
// Hold until all the listeners exits
if (!executorService.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
// Cancel currently executing tasks
executorService.shutdownNow();
log.warn(
"SharedProcessors wasn't gracefully terminated, there can be listener thread leakage");
}
} catch (InterruptedException e) {
executorService.shutdownNow();
log.error("Graceful shutdown process of SharedProcessors was interrupted");
}
}
}

0 comments on commit fdccec2

Please sign in to comment.