Skip to content

Commit

Permalink
add retry on probe
Browse files Browse the repository at this point in the history
  • Loading branch information
simlarsen committed May 25, 2023
1 parent ab25a98 commit 95c6cee
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 58 deletions.
3 changes: 3 additions & 0 deletions Probe/Dockerfile.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ RUN mkdir /tmp/npm && chmod 2777 /tmp/npm && chown 1000:1000 /tmp/npm && npm co
RUN apk update && apk add bash && apk add curl



# Install python
RUN apk update && apk add --no-cache --virtual .gyp python3 make g++

#Use bash shell by default
SHELL ["/bin/bash", "-c"]

# Install iputils
RUN apk add iputils

RUN mkdir /usr/src

Expand Down
12 changes: 11 additions & 1 deletion Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export default class ApiMonitor {
requestHeaders?: Headers | undefined;
requestBody?: JSONObject | undefined;
requestType?: HTTPMethod | undefined;
}
},
retry?: number | undefined
): Promise<APIResponse> {
try {
const startTime: [number, number] = process.hrtime();
Expand Down Expand Up @@ -56,6 +57,15 @@ export default class ApiMonitor {
requestBody: options.requestBody || {},
};
} catch (err) {
if (!retry) {
retry = 0; // default value
}

if (retry < 5) {
retry++;
return await this.ping(url, options, retry);
}

return {
url: url,
isOnline: false,
Expand Down
97 changes: 41 additions & 56 deletions Probe/Utils/Monitors/MonitorTypes/PingMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import URL from 'Common/Types/API/URL';
import IPv4 from 'Common/Types/IP/IPv4';
import IPv6 from 'Common/Types/IP/IPv6';
import PositiveNumber from 'Common/Types/PositiveNumber';
import net, { Socket } from 'net';
import ping from 'ping';

// TODO - make sure it work for the IPV6
export interface PingResponse {
isOnline: boolean;
responseTimeInMS?: PositiveNumber;
responseTimeInMS?: PositiveNumber | undefined;
}

export interface PingOptions {
Expand All @@ -18,62 +18,47 @@ export interface PingOptions {
export default class PingMonitor {
public static async ping(
host: Hostname | IPv4 | IPv6 | URL,
pingOptions?: PingOptions
pingOptions?: PingOptions,
retry?: number | undefined
): Promise<PingResponse> {
return new Promise<PingResponse>(
(resolve: Function, _reject: Function) => {
const timeout: number =
pingOptions?.timeout?.toNumber() || 5000;
const startTime: [number, number] = process.hrtime();
let responseTimeInMS: PositiveNumber;
let connectionOptions: net.NetConnectOpts;
if (host instanceof Hostname) {
connectionOptions = {
host: host.hostname,
port: host.port.toNumber(),
timeout,
};
} else if (host instanceof URL) {
connectionOptions = {
host: host.hostname.hostname,
port:
host.hostname.port?.toNumber() ||
(host.isHttps() ? 443 : 80),
timeout,
};
} else {
connectionOptions = {
host: host.toString(),
port: 80,
timeout,
};
let hostAddress: string = '';
if (host instanceof Hostname) {
hostAddress = host.hostname;
} else if (host instanceof URL) {
hostAddress = host.hostname.hostname;
} else {
hostAddress = host.toString();
}

try {
const res: ping.PingResponse = await ping.promise.probe(
hostAddress,
{
timeout: Math.ceil(
(pingOptions?.timeout?.toNumber() || 5000) / 1000
),
}
const socket: Socket = net.connect(connectionOptions);
socket.on('connect', () => {
const endTime: [number, number] = process.hrtime(startTime);
responseTimeInMS = new PositiveNumber(
(endTime[0] * 1000000000 + endTime[1]) / 1000000
);
});
socket.on('timeout', () => {
resolve({
isOnline: false,
});
});
socket.on('connect', () => {
socket.end(() => {
resolve({
isOnline: true,
responseTimeInMS,
});
});
});
socket.on('error', () => {
resolve({
isOnline: false,
});
});
);

return {
isOnline: res.alive,
responseTimeInMS: res.time
? new PositiveNumber(Math.ceil(res.time as any))
: undefined,
};
} catch (err) {
if (!retry) {
retry = 0; // default value
}
);

if (retry < 5) {
retry++;
return await this.ping(host, pingOptions, retry);
}

return {
isOnline: false,
};
}
}
}
14 changes: 13 additions & 1 deletion Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export interface ProbeWebsiteResponse {
}

export default class WebsiteMonitor {
public static async ping(url: URL): Promise<ProbeWebsiteResponse> {
public static async ping(
url: URL,
retry?: number | undefined
): Promise<ProbeWebsiteResponse> {
try {
const startTime: [number, number] = process.hrtime();
const result: WebsiteResponse = await WebsiteRequest.get(url, {});
Expand All @@ -38,6 +41,15 @@ export default class WebsiteMonitor {
responseHeaders: result.responseHeaders,
};
} catch (err) {
if (!retry) {
retry = 0; // default value
}

if (retry < 5) {
retry++;
return await this.ping(url, retry);
}

if (err instanceof AxiosError) {
return {
url: url,
Expand Down
15 changes: 15 additions & 0 deletions Probe/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Probe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
"author": "",
"license": "ISC",
"dependencies": {
"@types/ping": "^0.4.1",
"axios": "^1.4.0",
"Common": "file:../Common",
"CommonServer": "file:../CommonServer",
"ejs": "^3.1.8",
"jsrsasign": "^10.6.1",
"Model": "file:../Model",
"node-cron": "^3.0.2",
"ping": "^0.4.4",
"saml2-js": "^4.0.1",
"ts-node": "^10.9.1"
},
Expand Down

0 comments on commit 95c6cee

Please sign in to comment.