From 09917837d0e5a47ba8456f6518c9e00b73dcfc5c Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Wed, 22 Nov 2023 18:31:27 +0100 Subject: [PATCH] (process) Ensure construction exceptions are logged Wrapping these exceptions in a try-catch and logging them with slf4j will ensure they end up in the process logs. The way it worked using the default exception handler, they'd print on console (which nothing captures!), leading to a very annoying debugging experience. --- .../marginalia/converting/ConverterMain.java | 28 +++++++++------ .../java/nu/marginalia/crawl/CrawlerMain.java | 36 ++++++++++--------- .../index/IndexConstructorMain.java | 26 ++++++++------ .../nu/marginalia/loading/LoaderMain.java | 21 +++++------ 4 files changed, 63 insertions(+), 48 deletions(-) diff --git a/code/processes/converting-process/src/main/java/nu/marginalia/converting/ConverterMain.java b/code/processes/converting-process/src/main/java/nu/marginalia/converting/ConverterMain.java index 20ded57d..fb919018 100644 --- a/code/processes/converting-process/src/main/java/nu/marginalia/converting/ConverterMain.java +++ b/code/processes/converting-process/src/main/java/nu/marginalia/converting/ConverterMain.java @@ -50,21 +50,27 @@ public class ConverterMain { private final int node; public static void main(String... args) throws Exception { - Injector injector = Guice.createInjector( - new ConverterModule(), - new ProcessConfigurationModule("converter"), - new DatabaseModule() - ); - var converter = injector.getInstance(ConverterMain.class); + try { + Injector injector = Guice.createInjector( + new ConverterModule(), + new ProcessConfigurationModule("converter"), + new DatabaseModule() + ); - logger.info("Starting pipe"); + var converter = injector.getInstance(ConverterMain.class); - converter - .fetchInstructions() - .execute(converter); + logger.info("Starting pipe"); - logger.info("Finished"); + converter + .fetchInstructions() + .execute(converter); + + logger.info("Finished"); + } + catch (Exception ex) { + logger.error("Uncaught Exception", ex); + } System.exit(0); } diff --git a/code/processes/crawling-process/src/main/java/nu/marginalia/crawl/CrawlerMain.java b/code/processes/crawling-process/src/main/java/nu/marginalia/crawl/CrawlerMain.java index bff49aaa..dc76abde 100644 --- a/code/processes/crawling-process/src/main/java/nu/marginalia/crawl/CrawlerMain.java +++ b/code/processes/crawling-process/src/main/java/nu/marginalia/crawl/CrawlerMain.java @@ -46,7 +46,7 @@ import java.util.concurrent.atomic.AtomicInteger; import static nu.marginalia.mqapi.ProcessInboxNames.CRAWLER_INBOX; public class CrawlerMain { - private final Logger logger = LoggerFactory.getLogger(getClass()); + private final static Logger logger = LoggerFactory.getLogger(CrawlerMain.class); private final ProcessHeartbeatImpl heartbeat; private final ConnectionPool connectionPool = new ConnectionPool(5, 10, TimeUnit.SECONDS); @@ -110,26 +110,28 @@ public class CrawlerMain { // We don't want to use too much memory caching sessions for https System.setProperty("javax.net.ssl.sessionCacheSize", "2048"); - Injector injector = Guice.createInjector( - new CrawlerModule(), - new ProcessConfigurationModule("crawler"), - new DatabaseModule() - ); - var crawler = injector.getInstance(CrawlerMain.class); - - var instructions = crawler.fetchInstructions(); try { - crawler.run(instructions.specProvider, instructions.outputDir); - instructions.ok(); + Injector injector = Guice.createInjector( + new CrawlerModule(), + new ProcessConfigurationModule("crawler"), + new DatabaseModule() + ); + var crawler = injector.getInstance(CrawlerMain.class); + + var instructions = crawler.fetchInstructions(); + try { + crawler.run(instructions.specProvider, instructions.outputDir); + instructions.ok(); + } catch (Exception ex) { + logger.error("Crawler failed", ex); + instructions.err(); + } + + TimeUnit.SECONDS.sleep(5); } catch (Exception ex) { - System.err.println("Crawler failed"); - ex.printStackTrace(); - instructions.err(); + logger.error("Uncaught exception", ex); } - - TimeUnit.SECONDS.sleep(5); - System.exit(0); } diff --git a/code/processes/index-constructor-process/src/main/java/nu/marginalia/index/IndexConstructorMain.java b/code/processes/index-constructor-process/src/main/java/nu/marginalia/index/IndexConstructorMain.java index 3e93dafd..94f01162 100644 --- a/code/processes/index-constructor-process/src/main/java/nu/marginalia/index/IndexConstructorMain.java +++ b/code/processes/index-constructor-process/src/main/java/nu/marginalia/index/IndexConstructorMain.java @@ -48,25 +48,31 @@ public class IndexConstructorMain { private static final Logger logger = LoggerFactory.getLogger(IndexConstructorMain.class); private final Gson gson = GsonFactory.get(); public static void main(String[] args) throws Exception { - new org.mariadb.jdbc.Driver(); - - var main = Guice.createInjector( - new IndexConstructorModule(), - new ProcessConfigurationModule("index-constructor"), - new DatabaseModule()) - .getInstance(IndexConstructorMain.class); - - var instructions = main.fetchInstructions(); + CreateIndexInstructions instructions = null; try { + new org.mariadb.jdbc.Driver(); + + var main = Guice.createInjector( + new IndexConstructorModule(), + new ProcessConfigurationModule("index-constructor"), + new DatabaseModule()) + .getInstance(IndexConstructorMain.class); + + instructions = main.fetchInstructions(); + main.run(instructions); instructions.ok(); } catch (Exception ex) { logger.error("Constructor failed", ex); - instructions.err(); + + if (instructions != null) { + instructions.err(); + } } + // Grace period so we don't rug pull the logger or jdbc TimeUnit.SECONDS.sleep(5); System.exit(0); diff --git a/code/processes/loading-process/src/main/java/nu/marginalia/loading/LoaderMain.java b/code/processes/loading-process/src/main/java/nu/marginalia/loading/LoaderMain.java index 0e731156..98e66c5f 100644 --- a/code/processes/loading-process/src/main/java/nu/marginalia/loading/LoaderMain.java +++ b/code/processes/loading-process/src/main/java/nu/marginalia/loading/LoaderMain.java @@ -52,17 +52,18 @@ public class LoaderMain { private final int node; private final Gson gson; - public static void main(String... args) throws Exception { - new org.mariadb.jdbc.Driver(); - - Injector injector = Guice.createInjector( - new ProcessConfigurationModule("loader"), - new LoaderModule(), - new DatabaseModule() - ); - - var instance = injector.getInstance(LoaderMain.class); + public static void main(String... args) { try { + new org.mariadb.jdbc.Driver(); + + Injector injector = Guice.createInjector( + new ProcessConfigurationModule("loader"), + new LoaderModule(), + new DatabaseModule() + ); + + var instance = injector.getInstance(LoaderMain.class); + var instructions = instance.fetchInstructions(); logger.info("Instructions received"); instance.run(instructions);