* Write to the heartbeat file if we haven't already done so within the * timeout and start or reset a timer that keeps running as long as there is * activity. Failures are logged as warnings.
()
| 38 | * activity. Failures are logged as warnings. |
| 39 | */ |
| 40 | public async beat(): Promise<void> { |
| 41 | if (this.alive()) { |
| 42 | this.setState("alive") |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | logger.debug("heartbeat") |
| 47 | this.lastHeartbeat = Date.now() |
| 48 | if (typeof this.heartbeatTimer !== "undefined") { |
| 49 | clearTimeout(this.heartbeatTimer) |
| 50 | } |
| 51 | |
| 52 | this.heartbeatTimer = setTimeout(async () => { |
| 53 | try { |
| 54 | if (await this.isActive()) { |
| 55 | this.beat() |
| 56 | } else { |
| 57 | this.setState("expired") |
| 58 | } |
| 59 | } catch (error: unknown) { |
| 60 | logger.warn((error as Error).message) |
| 61 | this.setState("unknown") |
| 62 | } |
| 63 | }, this.heartbeatInterval) |
| 64 | |
| 65 | this.setState("alive") |
| 66 | |
| 67 | try { |
| 68 | return await fs.writeFile(this.heartbeatPath, "") |
| 69 | } catch (error: any) { |
| 70 | logger.warn(error.message) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Call to clear any heartbeatTimer for shutdown. |
no test coverage detected