Skip to content

Commit

Permalink
Updated interrupt logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lwj5 committed Sep 11, 2019
1 parent 8862c57 commit 4deae61
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/main/java/ai/preferred/venom/Crawler.java
Expand Up @@ -469,6 +469,10 @@ public void close() throws Exception {

Exception cachedException = null;
for (final AutoCloseable closeable : new AutoCloseable[]{workerManager, fetcher}) {
if (Thread.interrupted()) {
interrupt();
}

try {
closeable.close();
} catch (InterruptedException e) {
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/ai/preferred/venom/ThreadedWorkerManager.java
Expand Up @@ -72,24 +72,29 @@ public final Worker getWorker() {

@Override
public final void interrupt() {
if (executor == null) {
if (executor == null || executor.isTerminated()) {
return;
}
LOGGER.debug("Forcefully shutting down the worker manager.");
executor.shutdownNow();
}

@Override
public final void close() throws InterruptedException {
if (executor == null) {
public final void close() {
if (executor == null || executor.isTerminated()) {
return;
}
LOGGER.debug("Shutting down the worker manager.");
executor.shutdown();
if (executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) {
LOGGER.debug("The worker manager has been terminated.");
} else {
executor.shutdownNow();
try {
if (executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) {
LOGGER.debug("The worker manager has been terminated.");
} else {
interrupt();
}
} catch (final InterruptedException e) {
interrupt();
Thread.currentThread().interrupt();
}
}

Expand Down

0 comments on commit 4deae61

Please sign in to comment.