From b710b746b9aadf82e7ba60cd11e384378e51e206 Mon Sep 17 00:00:00 2001 From: gumi Date: Mon, 2 Apr 2018 19:12:34 -0400 Subject: gracefully handle read errors when fetching the online list --- server.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 884ac6d..9f39adc 100644 --- a/server.js +++ b/server.js @@ -8,10 +8,17 @@ const api = express(); const tmwa = { status: "OfflineTemporarily", num_online: 0, - interval: null, + timeout: null, poll: () => { fs.readFile("./online.txt", "utf8", (err, data) => { const lines = data.split("\n"); + + if (err || lines.length < 2) { + console.error("encountered an error while retrieving online.txt", err); + tmwa.timeout = setTimeout(tmwa.poll, 30000); // <= it failed, so check again later + return; + } + const last_online = Date.parse(lines[0].match(/\((.+)\)/)[1] + ` ${process.env.npm_package_config_timezone}`); if (Date.now() - last_online < 30000) { @@ -22,7 +29,7 @@ const tmwa = { tmwa.num_online = 0; } - setTimeout(tmwa.poll, 2000); + tmwa.timeout = setTimeout(tmwa.poll, 2000); }); } }; -- cgit v1.2.3-60-g2f50