From a93f55f5e7fb1afc72fdadec037aec5cfeff4b56 Mon Sep 17 00:00:00 2001 From: James Berry Date: Wed, 13 Nov 2024 11:27:53 +0000 Subject: [PATCH] Switch isAlive to be connection leng --- packages/client/src/app/server.ts | 4 ++++ packages/connections/src/lib/controllerConnection.ts | 11 ++++++++--- .../connections/src/lib/deviceControlConnection.ts | 10 +++++++--- .../connections/src/lib/deviceWorkerConnection.ts | 12 ++++++++---- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/client/src/app/server.ts b/packages/client/src/app/server.ts index 3e0d1fd..4a805fd 100644 --- a/packages/client/src/app/server.ts +++ b/packages/client/src/app/server.ts @@ -38,6 +38,9 @@ export const makeServer = ({ environment = 'test' } = {}) => { isAlive() { return faker.datatype.boolean(); }, + heartbeatCheckStatus() { + return faker.datatype.boolean(); + }, lastMemory() { return { memFree: faker.datatype.number(), @@ -69,6 +72,7 @@ export const makeServer = ({ environment = 'test' } = {}) => { init: faker.datatype.boolean(), instanceNo: 0, isAlive: faker.datatype.boolean(), + heartbeatCheckStatus: faker.datatype.boolean(), noMessagesReceived: 1, noMessagesSent: 1, origin: 'none', diff --git a/packages/connections/src/lib/controllerConnection.ts b/packages/connections/src/lib/controllerConnection.ts index edc4241..4372961 100644 --- a/packages/connections/src/lib/controllerConnection.ts +++ b/packages/connections/src/lib/controllerConnection.ts @@ -26,6 +26,7 @@ export class ControllerConnection extends EventEmitter { dateLastMessageSent: number; instanceNo: number; workerName: string; + heartbeatCheckStatus: boolean; isAlive: boolean; loginListener: number; @@ -34,6 +35,7 @@ export class ControllerConnection extends EventEmitter { this.ws = ws; this.log = log; this.dateLastMessageSent = Date.now(); + this.heartbeatCheckStatus = true; this.isAlive = true; this.workerName = ''; @@ -68,6 +70,7 @@ export class ControllerConnection extends EventEmitter { * Handle Mitm device disconnection */ #handleMitmDisconnection() { + this.isAlive = false; this.disconnect(); } @@ -128,6 +131,7 @@ export class ControllerConnection extends EventEmitter { } #handleControllerDisconnection() { + this.heartbeatCheckStatus = false; this.isAlive = false; clearInterval(this.heartbeatHandle); @@ -157,11 +161,11 @@ export class ControllerConnection extends EventEmitter { } heartbeat() { - this.isAlive = true; + this.heartbeatCheckStatus = true; } checkHeartbeat() { - if (!this.isAlive) { + if (!this.heartbeatCheckStatus) { // Pong has not been received in last interval seconds this.log.warn( `${this.workerName}/${this.instanceNo}->${this.workerId}: Scanner - No response to ping - forcing disconnect`, @@ -182,7 +186,7 @@ export class ControllerConnection extends EventEmitter { return; } - this.isAlive = false; + this.heartbeatCheckStatus = false; this.ws.ping(); } @@ -190,6 +194,7 @@ export class ControllerConnection extends EventEmitter { return { dateLastMessageSent: this.dateLastMessageSent, instanceNo: this.instanceNo, + heartbeatCheckStatus: this.heartbeatCheckStatus, isAlive: this.isAlive, loginListener: this.loginListener, origin: this.origin, diff --git a/packages/connections/src/lib/deviceControlConnection.ts b/packages/connections/src/lib/deviceControlConnection.ts index f3d28ef..fcab138 100644 --- a/packages/connections/src/lib/deviceControlConnection.ts +++ b/packages/connections/src/lib/deviceControlConnection.ts @@ -40,6 +40,7 @@ export class DeviceControlConnection extends EventEmitter { version?: string; publicIp?: string; ws: WebSocket; + heartbeatCheckStatus: boolean; isAlive: boolean; instanceNo: number; heartbeatHandle: NodeJS.Timer; @@ -52,6 +53,7 @@ export class DeviceControlConnection extends EventEmitter { this.log = log; this.ws = ws; this.init = true; + this.heartbeatCheckStatus = true; this.isAlive = true; this.origin = ''; @@ -125,11 +127,11 @@ export class DeviceControlConnection extends EventEmitter { } heartbeat() { - this.isAlive = true; + this.heartbeatCheckStatus = true; } checkHeartbeat() { - if (!this.isAlive) { + if (!this.heartbeatCheckStatus) { // Pong has not been received in last interval seconds this.log.warn(`${this.deviceId}/${this.instanceNo}: DEVICE - No response to ping - forcing disconnect`); clearInterval(this.heartbeatHandle); @@ -138,11 +140,12 @@ export class DeviceControlConnection extends EventEmitter { return; } - this.isAlive = false; + this.heartbeatCheckStatus = false; this.ws.ping(); } disconnected() { + this.heartbeatCheckStatus = false; this.isAlive = false; clearInterval(this.heartbeatHandle); @@ -216,6 +219,7 @@ export class DeviceControlConnection extends EventEmitter { deviceId: this.deviceId, init: this.init, instanceNo: this.instanceNo, + heartbeatCheckStatus: this.heartbeatCheckStatus, isAlive: this.isAlive, lastMemory: this.lastMemory, nextId: this.nextId, diff --git a/packages/connections/src/lib/deviceWorkerConnection.ts b/packages/connections/src/lib/deviceWorkerConnection.ts index 9f946a1..9eb4a92 100644 --- a/packages/connections/src/lib/deviceWorkerConnection.ts +++ b/packages/connections/src/lib/deviceWorkerConnection.ts @@ -22,6 +22,7 @@ export class DeviceWorkerConnection extends EventEmitter { origin?: string; version?: string; ws: WebSocket; + heartbeatCheckStatus: boolean; isAlive: boolean; instanceNo: number; heartbeatHandle: NodeJS.Timer; @@ -33,6 +34,7 @@ export class DeviceWorkerConnection extends EventEmitter { this.ws = ws; this.log = log; this.init = true; + this.heartbeatCheckStatus = true; this.isAlive = true; this.origin = ''; this.traceMessages = false; @@ -101,11 +103,11 @@ export class DeviceWorkerConnection extends EventEmitter { } heartbeat() { - this.isAlive = true; + this.heartbeatCheckStatus = true; } checkHeartbeat() { - if (!this.isAlive) { + if (!this.heartbeatCheckStatus) { // Pong has not been received in last interval seconds this.log.warn(`${this.workerId}/${this.instanceNo}: MITM - No response to ping - forcing disconnect`); clearInterval(this.heartbeatHandle); @@ -114,12 +116,13 @@ export class DeviceWorkerConnection extends EventEmitter { return; } - this.isAlive = false; + this.heartbeatCheckStatus = false; this.ws.ping(); } disconnected() { - this.isAlive = false; + this.heartbeatCheckStatus = false; + this.isAlive = true; clearInterval(this.heartbeatHandle); this.emit('disconnected', this); @@ -131,6 +134,7 @@ export class DeviceWorkerConnection extends EventEmitter { dateLastMessageSent: this.dateLastMessageSent, init: this.init, instanceNo: this.instanceNo, + heartbeatCheckStatus: this.heartbeatCheckStatus, isAlive: this.isAlive, noMessagesReceived: this.noMessagesReceived, noMessagesSent: this.noMessagesSent,