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

chore(dashmate): report port check errors #2245

Merged
merged 5 commits into from
Oct 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ export default function collectSamplesTaskFactory(
title: 'Core P2P port',
task: async () => {
const port = config.get('core.p2p.port');
const response = await providers.mnowatch.checkPortStatus(port, config.get('externalIp'));
const response = await providers.mnowatch.checkPortStatus(port, config.get('externalIp'))
.catch((e) => e.toString());

ctx.samples.setServiceInfo('core', 'p2pPort', response);
},
Expand All @@ -177,7 +178,8 @@ export default function collectSamplesTaskFactory(
enabled: () => config.get('platform.enable'),
task: async () => {
const port = config.get('platform.gateway.listeners.dapiAndDrive.port');
const response = await providers.mnowatch.checkPortStatus(port, config.get('externalIp'));
const response = await providers.mnowatch.checkPortStatus(port, config.get('externalIp'))
.catch((e) => e.toString());

ctx.samples.setServiceInfo('gateway', 'httpPort', response);
},
Expand All @@ -186,7 +188,8 @@ export default function collectSamplesTaskFactory(
title: 'Tenderdash P2P port',
task: async () => {
const port = config.get('platform.drive.tenderdash.p2p.port');
const response = await providers.mnowatch.checkPortStatus(port, config.get('externalIp'));
const response = await providers.mnowatch.checkPortStatus(port, config.get('externalIp'))
.catch((e) => e.toString());

ctx.samples.setServiceInfo('drive_tenderdash', 'p2pPort', response);
},
Expand Down
27 changes: 17 additions & 10 deletions packages/dashmate/src/status/providers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import https from 'https';
import { PortStateEnum } from './enums/portState.js';

const MAX_REQUEST_TIMEOUT = 5000;
const MAX_RESPONSE_SIZE = 1 * 1024 * 1024; // 1 MB
Expand Down Expand Up @@ -82,10 +81,9 @@ export default {
path: ip ? `/${port}/?validateIp=${ip}` : `/${port}/`,
method: 'GET',
family: 4, // Force IPv4
timeout: MAX_REQUEST_TIMEOUT,
};

return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = '';

Expand All @@ -95,9 +93,12 @@ export default {
// eslint-disable-next-line no-console
console.warn(`Port check request failed with status code ${res.statusCode}`);
}
// Consume response data to free up memory
res.resume();
resolve(PortStateEnum.ERROR);

const error = new Error(`Invalid status code ${res.statusCode}`);

res.destroy(error);

// Do not handle request further
return;
}

Expand All @@ -109,14 +110,14 @@ export default {
data += chunk;

if (data.length > MAX_RESPONSE_SIZE) {
resolve(PortStateEnum.ERROR);

if (process.env.DEBUG) {
// eslint-disable-next-line no-console
console.warn('Port check response size exceeded');
}

req.destroy();
const error = new Error('Response size exceeded');

req.destroy(error);
}
});

Expand All @@ -126,13 +127,19 @@ export default {
});
});

req.setTimeout(MAX_REQUEST_TIMEOUT, () => {
const error = new Error('Port check timed out');

req.destroy(error);
});

req.on('error', (e) => {
if (process.env.DEBUG) {
// eslint-disable-next-line no-console
console.warn(`Port check request failed: ${e}`);
}

resolve(PortStateEnum.ERROR);
reject(e);
});

req.end();
Expand Down
4 changes: 3 additions & 1 deletion packages/dashmate/src/status/scopes/core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable camelcase */
import { PortStateEnum } from '../enums/portState.js';
import providers from '../providers.js';
import { ServiceStatusEnum } from '../enums/serviceStatus.js';
import { DockerStatusEnum } from '../enums/dockerStatus.js';
Expand Down Expand Up @@ -116,7 +117,8 @@ export default function getCoreScopeFactory(

const providersResult = await Promise.allSettled([
providers.github.release('dashpay/dash'),
providers.mnowatch.checkPortStatus(config.get('core.p2p.port'), config.get('externalIp')),
providers.mnowatch.checkPortStatus(config.get('core.p2p.port'), config.get('externalIp'))
.catch(() => PortStateEnum.ERROR),
providers.insight(config.get('network')).status(),
]);

Expand Down
7 changes: 5 additions & 2 deletions packages/dashmate/src/status/scopes/platform.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import prettyMs from 'pretty-ms';
import { PortStateEnum } from '../enums/portState.js';
import DockerComposeError from '../../docker/errors/DockerComposeError.js';
import providers from '../providers.js';
import { DockerStatusEnum } from '../enums/dockerStatus.js';
Expand Down Expand Up @@ -90,8 +91,10 @@ export default function getPlatformScopeFactory(
// Collecting platform data fails if Tenderdash is waiting for core to sync
if (info.serviceStatus === ServiceStatusEnum.up) {
const portStatusResult = await Promise.allSettled([
providers.mnowatch.checkPortStatus(config.get('platform.gateway.listeners.dapiAndDrive.port'), config.get('externalIp')),
providers.mnowatch.checkPortStatus(config.get('platform.drive.tenderdash.p2p.port'), config.get('externalIp')),
providers.mnowatch.checkPortStatus(config.get('platform.gateway.listeners.dapiAndDrive.port'), config.get('externalIp'))
.catch(() => PortStateEnum.ERROR),
providers.mnowatch.checkPortStatus(config.get('platform.drive.tenderdash.p2p.port'), config.get('externalIp'))
.catch(() => PortStateEnum.ERROR),
]);
const [httpPortState, p2pPortState] = portStatusResult.map((result) => (result.status === 'fulfilled' ? result.value : null));

Expand Down
6 changes: 3 additions & 3 deletions packages/dashmate/test/unit/status/scopes/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('getCoreScopeFactory', () => {
});

mockGithubProvider.returns('v1337-dev');
mockMNOWatchProvider.returns('OPEN');
mockMNOWatchProvider.resolves('OPEN');
mockInsightProvider.returns({
status: this.sinon.stub().returns({
info: { blocks: 1337 },
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('getCoreScopeFactory', () => {
});

mockGithubProvider.returns('v1337-dev');
mockMNOWatchProvider.returns('OPEN');
mockMNOWatchProvider.resolves('OPEN');
mockInsightProvider.returns({
status: this.sinon.stub().returns({
info: { blocks: 1337 },
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('getCoreScopeFactory', () => {
});

mockGithubProvider.returns(Promise.reject());
mockMNOWatchProvider.returns(PortStateEnum.ERROR);
mockMNOWatchProvider.rejects(PortStateEnum.ERROR);
mockInsightProvider.returns({
status: () => Promise.reject(),
});
Expand Down
Loading