From 482a8d101de9e43f036b195933b868187166083e Mon Sep 17 00:00:00 2001 From: Steve Tu Date: Fri, 11 Oct 2024 07:01:15 -0700 Subject: [PATCH] handle empty files --- .../rei/stats/gsampler/GSamplerEngine.groovy | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/groovy/com/rei/stats/gsampler/GSamplerEngine.groovy b/src/main/groovy/com/rei/stats/gsampler/GSamplerEngine.groovy index d21c9ff..38db668 100644 --- a/src/main/groovy/com/rei/stats/gsampler/GSamplerEngine.groovy +++ b/src/main/groovy/com/rei/stats/gsampler/GSamplerEngine.groovy @@ -149,9 +149,26 @@ class GSamplerEngine { new File(runsDir.toFile(), "${id}.lastRun").text = System.currentTimeMillis() as String selfStats.lastRan[id] = new Date() } - + long readLastRun(id) { - def f = new File(runsDir.toFile(), "${id}.lastRun") - return f.exists() ? f.text as long : 0 + try { + def f = new File(runsDir.toFile(), "${id}.lastRun") + if (!f.exists() || !f.canRead()) { + return 0 + } + + String content = f.text?.trim() + if (!content) { + return 0 + } + + return content as long + } catch (NumberFormatException e) { + logger.warn("Failed to parse lastRun id: ${id}", e) + return 0 + } catch (IOException e) { + logger.error("Failed to read lastRun id: ${id}", e) + return 0 + } } }