From abeaba7becd65543b17354a99629809410cc796d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olov=20Ylinenp=C3=A4=C3=A4?= Date: Thu, 21 Nov 2024 17:05:51 +0100 Subject: [PATCH] fix(emm): Also timeout when waiting for file to appear --- emm/src/main/java/whelk/Dump.java | 38 ++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/emm/src/main/java/whelk/Dump.java b/emm/src/main/java/whelk/Dump.java index ab459906d4..86e61ba86f 100644 --- a/emm/src/main/java/whelk/Dump.java +++ b/emm/src/main/java/whelk/Dump.java @@ -113,8 +113,9 @@ private static void sendDumpPageResponse(Whelk whelk, String apiBaseUrl, String try { // Has the dump not begun being written yet ? + var t = new Timeout(60 * 1000); while (!Files.exists(dumpFilePath)) { - Thread.sleep(10); + t.sleep(); } try (RandomAccessFile file = new RandomAccessFile(dumpFilePath.toFile(), "r")) { @@ -129,14 +130,9 @@ private static void sendDumpPageResponse(Whelk whelk, String apiBaseUrl, String // Is there not enough data for a full page yet ? long offsetBytes = 17 * offsetLines; - long startWait = System.currentTimeMillis(); + t = new Timeout(60 * 1000); while (!dumpFinished && file.length() < offsetBytes + (17 * (long)EmmChangeSet.TARGET_HITS_PER_PAGE)) { - if (System.currentTimeMillis() > startWait + (60 * 1000)) { - logger.info("Timed out (1 minute) waiting for enough data to be generated in: " + dumpFilePath); - res.sendError(500); - return; - } - Thread.sleep(10); + t.sleep(); if (file.length() >= 17) { file.seek(file.length() - 17); @@ -162,8 +158,12 @@ private static void sendDumpPageResponse(Whelk whelk, String apiBaseUrl, String } } - } catch (IOException | InterruptedException e) { - logger.error("Failed reading dumpfile: " + dumpFilePath, e); + } catch (Timeout.TimeOutException | InterruptedException e) { + logger.info("Timed out (1 minute) waiting for enough data to be generated in: {}", dumpFilePath); + HttpTools.sendError(res, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "timeout"); + return; + } catch (IOException e) { + logger.error("Failed reading dumpfile: {}", dumpFilePath, e); HttpTools.sendError(res, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ""); return; } @@ -222,7 +222,7 @@ private static void sendFormattedResponse(Whelk whelk, String apiBaseUrl, String HttpTools.sendResponse(res, responseObject, JSON_CONTENT_TYPE); } - + private static void invalidateIfOld(Path dumpFilePath) { try { if (!Files.exists(dumpFilePath)) @@ -338,4 +338,20 @@ private static PreparedStatement getTypeXDumpStatement(Connection connection, Wh return preparedStatement; } + + private static class Timeout { + long time; + Timeout(long maxWaitMs) { + this.time = System.currentTimeMillis() + maxWaitMs; + } + + public void sleep() throws InterruptedException, TimeOutException { + Thread.sleep(10); + if (System.currentTimeMillis() > time) { + throw new TimeOutException(); + } + } + + static class TimeOutException extends Exception {} + } }