Skip to content

Commit

Permalink
refactor: lower the memory requirements for node updates (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik authored May 7, 2024
1 parent 1fb6fb1 commit 35b1c1a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
28 changes: 17 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import * as fs from 'node:fs';
import { execSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';

import { isV1HardwareDevice } from './lib/util.js';
import { looksLikeV1HardwareDevice } from './lib/util.js';

const WANTED_VERSION = 'v18.19.1';
const MIN_NODE_UPDATE_MEMORY = 1e9;
const MIN_NODE_UPDATE_MEMORY = 400 * 1e6;
const MIN_NODE_UPDATE_DISK_SPACE_MB = 1000;
const dirname = path.dirname(fileURLToPath(import.meta.url));

function updateEntrypoint () {
Expand Down Expand Up @@ -43,19 +44,24 @@ function updateNode () {
return;
}

if (os.totalmem() < MIN_NODE_UPDATE_MEMORY) {
console.log(`Total system memory ${os.totalmem()} below the required threshold. Not updating.`);
try {
const IS_HW_PROBE = process.env['GP_HOST_HW'] || looksLikeV1HardwareDevice();

if (isV1HardwareDevice() || process.env['GP_HOST_HW']) {
if (IS_HW_PROBE) {
console.log(`Hardware probe detected. Not updating.`);
logUpdateFirmwareMessage();
} else {
logUpdateContainerMessage();
return;
}

return;
}
const PROBE_MEMORY = os.totalmem();
const PROBE_DISK_SPACE_MB = parseInt(execSync('df --block-size=MB --output=avail / | tail -1').toString());

if (PROBE_MEMORY < MIN_NODE_UPDATE_MEMORY || PROBE_DISK_SPACE_MB < MIN_NODE_UPDATE_DISK_SPACE_MB) {
console.log(`Total system memory (${PROBE_MEMORY}) or disk space (${PROBE_DISK_SPACE_MB}MB} below the required threshold. Not updating.`);
logUpdateContainerMessage();
return;
}

try {
const NODE_MODULES_NVM = '/app/node_modules/nvm';
const NVM_DIR = '/nvm';

Expand Down Expand Up @@ -109,7 +115,7 @@ function logUpdateContainerMessage () {
Current probe container is out of date and we couldn't update it automatically.
Please either:
- update it manually: https://github.com/jsdelivr/globalping-probe#optional-container-update
- increase container RAM to >1GB
- increase the available RAM to >= 500MB and disk size to >= 1GB
`);

setTimeout(logUpdateContainerMessage, 10 * 60 * 1000);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const callbackify = (
cb(undefined, result);
}) as (...args: unknown[]) => never;

export const isV1HardwareDevice = () => {
export const looksLikeV1HardwareDevice = () => {
const cpus = os.cpus();

return cpus.length === 4
Expand Down
4 changes: 2 additions & 2 deletions src/probe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import { FakeMtrCommand } from './command/fake/fake-mtr-command.js';
import { run as runStatsAgent } from './lib/stats/client.js';
import { initStatusManager } from './lib/status-manager.js';
import { logAdoptionCode } from './lib/log-adoption-code.js';
import { isV1HardwareDevice } from './lib/util.js';
import { looksLikeV1HardwareDevice } from './lib/util.js';
import { VERSION } from './constants.js';

// Set the expected variables on HW probes with older firmware
// https://github.com/jsdelivr/globalping-hwprobe/issues/27
// https://github.com/jsdelivr/globalping-probe/issues/206
if (isV1HardwareDevice()) {
if (looksLikeV1HardwareDevice()) {
process.env['GP_HOST_HW'] = 'true';
process.env['GP_HOST_DEVICE'] = 'v1';
}
Expand Down
14 changes: 7 additions & 7 deletions test/unit/lib/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { expect } from 'chai';
import * as td from 'testdouble';
import * as sinon from 'sinon';

describe('isV1HardwareDevice', () => {
describe('looksLikeV1HardwareDevice', () => {
const sandbox = sinon.createSandbox();
const cpusStub = sinon.stub();
const hostnameStub = sinon.stub();
const totalmemStub = sinon.stub();
let isV1HardwareDevice: () => boolean;
let looksLikeV1HardwareDevice: () => boolean;

const mockRealHardwareValues = () => {
// Based on https://github.com/jsdelivr/globalping-hwprobe/issues/37#issuecomment-2002039986
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('isV1HardwareDevice', () => {
totalmem: totalmemStub,
});

({ isV1HardwareDevice } = await import('../../../src/lib/util.js'));
({ looksLikeV1HardwareDevice } = await import('../../../src/lib/util.js'));
});

afterEach(() => {
Expand All @@ -51,7 +51,7 @@ describe('isV1HardwareDevice', () => {

it('should return true for HW probes', () => {
mockRealHardwareValues();
expect(isV1HardwareDevice()).to.be.true;
expect(looksLikeV1HardwareDevice()).to.be.true;
});

it('should return false for different CPUs', () => {
Expand All @@ -72,20 +72,20 @@ describe('isV1HardwareDevice', () => {
},
]);

expect(isV1HardwareDevice()).to.be.false;
expect(looksLikeV1HardwareDevice()).to.be.false;
});

it('should return false for different hostnames', () => {
mockRealHardwareValues();
hostnameStub.returns('gp-probe');

expect(isV1HardwareDevice()).to.be.false;
expect(looksLikeV1HardwareDevice()).to.be.false;
});

it('should return false for unexpected memory values', () => {
mockRealHardwareValues();
totalmemStub.returns(1000 * 1e6);

expect(isV1HardwareDevice()).to.be.false;
expect(looksLikeV1HardwareDevice()).to.be.false;
});
});

0 comments on commit 35b1c1a

Please sign in to comment.