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

View File

@ -151,12 +151,20 @@ public class CrawlerMain {
logger.info("Shutting down the pool, waiting for tasks to complete..."); logger.info("Shutting down the pool, waiting for tasks to complete...");
pool.shutDown(); pool.shutDown();
do { int activePoolCount = pool.getActiveCount();
System.out.println("Waiting for pool to terminate... " + pool.getActiveCount() + " remaining");
} while (!pool.awaitTermination(60, TimeUnit.SECONDS)); 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) { catch (Exception ex) {
logger.warn("Exception in crawler", ex);
} }
finally { finally {
heartbeat.shutDown(); heartbeat.shutDown();