Skip to content

Commit f2f1fee

Browse files
committed
Short-circuit heartbeat when alive
1 parent 504d896 commit f2f1fee

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/node/heart.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,28 @@ export class Heart {
2121
* activity. Failures are logged as warnings.
2222
*/
2323
public beat(): void {
24-
if (!this.alive()) {
25-
logger.trace("heartbeat")
26-
fs.writeFile(this.heartbeatPath, "").catch((error) => {
27-
logger.warn(error.message)
28-
})
29-
this.lastHeartbeat = Date.now()
30-
if (typeof this.heartbeatTimer !== "undefined") {
31-
clearTimeout(this.heartbeatTimer)
32-
}
33-
this.heartbeatTimer = setTimeout(() => {
34-
this.isActive()
35-
.then((active) => {
36-
if (active) {
37-
this.beat()
38-
}
39-
})
40-
.catch((error) => {
41-
logger.warn(error.message)
42-
})
43-
}, this.heartbeatInterval)
24+
if (this.alive()) {
25+
return
4426
}
27+
28+
logger.trace("heartbeat")
29+
fs.writeFile(this.heartbeatPath, "").catch((error) => {
30+
logger.warn(error.message)
31+
})
32+
this.lastHeartbeat = Date.now()
33+
if (typeof this.heartbeatTimer !== "undefined") {
34+
clearTimeout(this.heartbeatTimer)
35+
}
36+
this.heartbeatTimer = setTimeout(() => {
37+
this.isActive()
38+
.then((active) => {
39+
if (active) {
40+
this.beat()
41+
}
42+
})
43+
.catch((error) => {
44+
logger.warn(error.message)
45+
})
46+
}, this.heartbeatInterval)
4547
}
4648
}

0 commit comments

Comments
 (0)