(crawler) Terminate crawler after a few hours of no progress

This commit is contained in:
Viktor Lofgren 2023-10-26 12:49:28 +02:00
parent 0f637fb722
commit a497e4c920
2 changed files with 17 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package nu.marginalia.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
@ -47,12 +48,15 @@ public class SimpleBlockingThreadPool {
this.shutDown = true;
}
public void shutDownNow() {
public void shutDownNow() throws InterruptedException {
this.shutDown = true;
tasks.clear();
for (Thread worker : workers) {
worker.interrupt();
}
for (Thread worker : workers) {
worker.join(Duration.ofMinutes(5));
}
}
private void worker() {

View File

@ -151,12 +151,20 @@ public class CrawlerMain {
logger.info("Shutting down the pool, waiting for tasks to complete...");
pool.shutDown();
do {
System.out.println("Waiting for pool to terminate... " + pool.getActiveCount() + " remaining");
} while (!pool.awaitTermination(60, TimeUnit.SECONDS));
int activePoolCount = pool.getActiveCount();
while (!pool.awaitTermination(5, TimeUnit.HOURS)) {
int newActivePoolCount = pool.getActiveCount();
if (activePoolCount == newActivePoolCount) {
logger.warn("Aborting the last {} jobs of the crawl, taking too long", newActivePoolCount);
pool.shutDownNow();
} else {
activePoolCount = newActivePoolCount;
}
}
}
catch (Exception ex) {
logger.warn("Exception in crawler", ex);
}
finally {
heartbeat.shutDown();