From e8f7fd5bf6ae881ea745be15c807e8a0ed2c36c5 Mon Sep 17 00:00:00 2001 From: nflaig Date: Sun, 5 Feb 2023 18:23:27 +0100 Subject: [PATCH] Add enum to check status of monitoring service --- .../beacon-node/src/monitoring/service.ts | 17 +++++++++------ .../test/unit/monitoring/service.test.ts | 21 +++++++++++++++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/beacon-node/src/monitoring/service.ts b/packages/beacon-node/src/monitoring/service.ts index 9746dc8e2548..a178a5d3777b 100644 --- a/packages/beacon-node/src/monitoring/service.ts +++ b/packages/beacon-node/src/monitoring/service.ts @@ -19,6 +19,11 @@ enum FetchAbortReason { Timeout = "timeout", } +enum Status { + Started = "started", + Stopped = "stopped", +} + export type Client = "beacon" | "validator"; /** @@ -35,6 +40,7 @@ export class MonitoringService { private readonly collectDataMetric: HistogramExtra; private readonly sendDataMetric: HistogramExtra<"status">; + private status = Status.Stopped; private initialDelayTimeout?: NodeJS.Timeout; private monitoringInterval?: NodeJS.Timer; private fetchAbortController?: AbortController; @@ -70,10 +76,8 @@ export class MonitoringService { * Start sending client stats based on configured interval */ start(): void { - if (this.initialDelayTimeout || this.monitoringInterval) { - // monitoring service is already started - return; - } + if (this.status === Status.Started) return; + this.status = Status.Started; const {interval, initialDelay, requestTimeout, collectSystemStats} = this.options; @@ -99,13 +103,14 @@ export class MonitoringService { * Stop sending client stats */ stop(): void { + if (this.status === Status.Stopped) return; + this.status = Status.Stopped; + if (this.initialDelayTimeout) { clearTimeout(this.initialDelayTimeout); - this.initialDelayTimeout = undefined; } if (this.monitoringInterval) { clearInterval(this.monitoringInterval); - this.monitoringInterval = undefined; } if (this.pendingRequest) { this.fetchAbortController?.abort(FetchAbortReason.Stop); diff --git a/packages/beacon-node/test/unit/monitoring/service.test.ts b/packages/beacon-node/test/unit/monitoring/service.test.ts index 2aec83063b77..34da774a1585 100644 --- a/packages/beacon-node/test/unit/monitoring/service.test.ts +++ b/packages/beacon-node/test/unit/monitoring/service.test.ts @@ -66,6 +66,12 @@ describe("monitoring / service", () => { }); describe("MonitoringService - start", () => { + it("should set the status to started", async () => { + const service = await startedMonitoringService(); + + expect(service["status"]).to.equal("started"); + }); + it("should set interval to continuously send client stats", async () => { const setInterval = sandbox.spy(global, "setInterval"); @@ -110,6 +116,14 @@ describe("monitoring / service", () => { }); describe("MonitoringService - stop", () => { + it("should set the status to stopped", async () => { + const service = await startedMonitoringService(); + + service.stop(); + + expect(service["status"]).to.equal("stopped"); + }); + it("should clear the monitoring interval", async () => { const clearInterval = sandbox.spy(global, "clearInterval"); @@ -117,8 +131,7 @@ describe("monitoring / service", () => { service.stop(); - expect(clearInterval).to.have.been.calledOnce; - expect(service["monitoringInterval"]).to.be.undefined; + expect(clearInterval).to.have.been.calledOnceWith(service["monitoringInterval"]); }); it("should clear the initial delay timeout", async () => { @@ -128,8 +141,7 @@ describe("monitoring / service", () => { service.stop(); - expect(clearTimeout).to.have.been.calledOnce; - expect(service["initialDelayTimeout"]).to.be.undefined; + expect(clearTimeout).to.have.been.calledOnceWith(service["initialDelayTimeout"]); }); it("should abort pending requests", async () => { @@ -201,6 +213,7 @@ describe("monitoring / service", () => { it("should abort pending requests if monitoring service is stopped", (done) => { const endpoint = `${baseUrl}${remoteServiceRoutes.pending}`; const service = new MonitoringService("beacon", {endpoint, collectSystemStats: false}, {register, logger}); + service.start(); service.send().finally(() => { try {