summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgumi <git@gumi.ca>2018-04-02 19:12:34 -0400
committergumi <git@gumi.ca>2018-04-02 19:12:34 -0400
commitb710b746b9aadf82e7ba60cd11e384378e51e206 (patch)
tree552dc4e04f5f351fa37405badf3156022f09efaa
parent53e26e39ffde2ad71e24949882c6eb75fbc74689 (diff)
downloadapi-b710b746b9aadf82e7ba60cd11e384378e51e206.tar.gz
api-b710b746b9aadf82e7ba60cd11e384378e51e206.tar.bz2
api-b710b746b9aadf82e7ba60cd11e384378e51e206.tar.xz
api-b710b746b9aadf82e7ba60cd11e384378e51e206.zip
gracefully handle read errors when fetching the online list
-rw-r--r--server.js11
1 files 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);
});
}
};