Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch isAlive to be connection status #45

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/client/src/app/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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',
Expand Down
11 changes: 8 additions & 3 deletions packages/connections/src/lib/controllerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class ControllerConnection extends EventEmitter {
dateLastMessageSent: number;
instanceNo: number;
workerName: string;
heartbeatCheckStatus: boolean;
isAlive: boolean;
loginListener: number;

Expand All @@ -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 = '<unknown>';

Expand Down Expand Up @@ -68,6 +70,7 @@ export class ControllerConnection extends EventEmitter {
* Handle Mitm device disconnection
*/
#handleMitmDisconnection() {
this.isAlive = false;
this.disconnect();
}

Expand Down Expand Up @@ -128,6 +131,7 @@ export class ControllerConnection extends EventEmitter {
}

#handleControllerDisconnection() {
this.heartbeatCheckStatus = false;
this.isAlive = false;
clearInterval(this.heartbeatHandle);

Expand Down Expand Up @@ -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`,
Expand All @@ -182,14 +186,15 @@ export class ControllerConnection extends EventEmitter {
return;
}

this.isAlive = false;
this.heartbeatCheckStatus = false;
this.ws.ping();
}

serialize(): ControllerConnectionDTO {
return {
dateLastMessageSent: this.dateLastMessageSent,
instanceNo: this.instanceNo,
heartbeatCheckStatus: this.heartbeatCheckStatus,
isAlive: this.isAlive,
loginListener: this.loginListener,
origin: this.origin,
Expand Down
10 changes: 7 additions & 3 deletions packages/connections/src/lib/deviceControlConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class DeviceControlConnection extends EventEmitter {
version?: string;
publicIp?: string;
ws: WebSocket;
heartbeatCheckStatus: boolean;
isAlive: boolean;
instanceNo: number;
heartbeatHandle: NodeJS.Timer;
Expand All @@ -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 = '';

Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions packages/connections/src/lib/deviceWorkerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class DeviceWorkerConnection extends EventEmitter {
origin?: string;
version?: string;
ws: WebSocket;
heartbeatCheckStatus: boolean;
isAlive: boolean;
instanceNo: number;
heartbeatHandle: NodeJS.Timer;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -114,11 +116,12 @@ export class DeviceWorkerConnection extends EventEmitter {
return;
}

this.isAlive = false;
this.heartbeatCheckStatus = false;
this.ws.ping();
}

disconnected() {
this.heartbeatCheckStatus = false;
this.isAlive = false;
clearInterval(this.heartbeatHandle);

Expand All @@ -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,
Expand Down
Loading