Skip to content

Commit

Permalink
fix(emm): Also timeout when waiting for file to appear
Browse files Browse the repository at this point in the history
  • Loading branch information
olovy committed Nov 21, 2024
1 parent f5bd22d commit abeaba7
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions emm/src/main/java/whelk/Dump.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {}
}
}

0 comments on commit abeaba7

Please sign in to comment.