Skip to content

Commit

Permalink
Add enum to check status of monitoring service
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Feb 5, 2023
1 parent bd8fc4f commit e8f7fd5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
17 changes: 11 additions & 6 deletions packages/beacon-node/src/monitoring/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ enum FetchAbortReason {
Timeout = "timeout",
}

enum Status {
Started = "started",
Stopped = "stopped",
}

export type Client = "beacon" | "validator";

/**
Expand All @@ -35,6 +40,7 @@ export class MonitoringService {
private readonly collectDataMetric: HistogramExtra<string>;
private readonly sendDataMetric: HistogramExtra<"status">;

private status = Status.Stopped;
private initialDelayTimeout?: NodeJS.Timeout;
private monitoringInterval?: NodeJS.Timer;
private fetchAbortController?: AbortController;
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand Down
21 changes: 17 additions & 4 deletions packages/beacon-node/test/unit/monitoring/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -110,15 +116,22 @@ 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");

const service = await startedMonitoringService();

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 () => {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e8f7fd5

Please sign in to comment.