From f2f1fee6f179059a1a5ed40e36192946c0d4bc86 Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 27 Oct 2020 17:48:37 -0500 Subject: [PATCH] Short-circuit heartbeat when alive --- src/node/heart.ts | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/node/heart.ts b/src/node/heart.ts index 5198e33d..eed070e4 100644 --- a/src/node/heart.ts +++ b/src/node/heart.ts @@ -21,26 +21,28 @@ export class Heart { * activity. Failures are logged as warnings. */ public beat(): void { - if (!this.alive()) { - logger.trace("heartbeat") - fs.writeFile(this.heartbeatPath, "").catch((error) => { - logger.warn(error.message) - }) - this.lastHeartbeat = Date.now() - if (typeof this.heartbeatTimer !== "undefined") { - clearTimeout(this.heartbeatTimer) - } - this.heartbeatTimer = setTimeout(() => { - this.isActive() - .then((active) => { - if (active) { - this.beat() - } - }) - .catch((error) => { - logger.warn(error.message) - }) - }, this.heartbeatInterval) + if (this.alive()) { + return } + + logger.trace("heartbeat") + fs.writeFile(this.heartbeatPath, "").catch((error) => { + logger.warn(error.message) + }) + this.lastHeartbeat = Date.now() + if (typeof this.heartbeatTimer !== "undefined") { + clearTimeout(this.heartbeatTimer) + } + this.heartbeatTimer = setTimeout(() => { + this.isActive() + .then((active) => { + if (active) { + this.beat() + } + }) + .catch((error) => { + logger.warn(error.message) + }) + }, this.heartbeatInterval) } }