Skip to content

Commit

Permalink
fix(doctor): Use systeminformation for memory availability
Browse files Browse the repository at this point in the history
os.freemem() doesn't account for memory used in buffers / caches in
linux, so it doesn't give an accurate reading of the available memory of
a system
  • Loading branch information
vikaspotluri123 authored and acburdine committed Apr 10, 2018
1 parent c8b6546 commit 1395646
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
15 changes: 9 additions & 6 deletions lib/commands/doctor/checks/check-memory.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
'use strict';
const os = require('os');
const sysinfo = require('systeminformation');
const SystemError = require('../../../errors').SystemError;

const MB_IN_BYTES = 1048576;
const MIN_MEMORY = 150;

function checkMemory() {
const availableMemory = os.freemem() / MB_IN_BYTES;
if (availableMemory < MIN_MEMORY) {
return Promise.reject(new SystemError(`Ghost recommends you have at least ${MIN_MEMORY} MB of memory available for smooth operation. It looks like you have ${parseInt(availableMemory)} MB available.`));
}
return Promise.resolve();
return sysinfo.mem().then((memoryInfo) => {
const availableMemory = memoryInfo.available / MB_IN_BYTES;

if (availableMemory < MIN_MEMORY) {
return Promise.reject(new SystemError(`Ghost recommends you have at least ${MIN_MEMORY} MB of memory available for smooth operation. It looks like you have ~${parseInt(availableMemory)} MB available.`));
}
return Promise.resolve();
});
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"stat-mode": "0.2.2",
"strip-ansi": "4.0.0",
"symlink-or-copy": "1.2.0",
"systeminformation": "3.37.8",
"tail": "1.2.3",
"update-notifier": "2.3.0",
"validator": "7.2.0",
Expand Down
31 changes: 18 additions & 13 deletions test/unit/commands/doctor/checks/check-memory-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const expect = require('chai').expect;
const sinon = require('sinon');

const os = require('os');
const sysinfo = require('systeminformation');
const errors = require('../../../../../lib/errors');

const modulePath = '../../../../../lib/commands/doctor/checks/check-memory';
Expand Down Expand Up @@ -34,29 +34,34 @@ describe('Unit: Doctor Checks > Memory', function () {
expect(memCheck.enabled(ctx)).to.be.true;
});

it('uses systeminformation to determine memory availability', function () {
const memStub = sandbox.stub(sysinfo, 'mem').rejects(new Error('systeminformation'));
const memCheck = require(modulePath);

return memCheck.task().catch(error => {
expect(error).to.be.an('error');
expect(error.message).to.equal('systeminformation');
expect(memStub.calledOnce).to.be.true;
});
});

it('fails if not enough memory is available', function () {
const osStub = sandbox.stub(os, 'freemem').returns(10);
const memStub = sandbox.stub(sysinfo, 'mem').resolves({available: 10});
const memCheck = require(modulePath);
const ctx = {
argv: {'check-mem': true}
};

return memCheck.task(ctx).catch((error) => {
return memCheck.task().catch((error) => {
expect(error).to.be.an.instanceof(errors.SystemError);
expect(error.message).to.match(/MB of memory available for smooth operation/);
expect(osStub.calledOnce).to.be.true;
expect(memStub.calledOnce).to.be.true;
});
});

it('passes if there is enough memory', function () {
const osStub = sandbox.stub(os, 'freemem').returns(157286400);
const memStub = sandbox.stub(sysinfo, 'mem').resolves({available: 157286400});
const memCheck = require(modulePath);
const ctx = {
argv: {'check-mem': true}
};

return memCheck.task(ctx).then(() => {
expect(osStub.calledOnce).to.be.true;
return memCheck.task().then(() => {
expect(memStub.calledOnce).to.be.true;
});
});
});
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4204,6 +4204,10 @@ [email protected]:
version "1.2.0"
resolved "https://registry.yarnpkg.com/symlink-or-copy/-/symlink-or-copy-1.2.0.tgz#5d49108e2ab824a34069b68974486c290020b393"

[email protected]:
version "3.37.8"
resolved "https://registry.yarnpkg.com/systeminformation/-/systeminformation-3.37.8.tgz#0b42af1faf3c0b77788bbc5b56ca5f0e9f58ee73"

[email protected]:
version "4.0.2"
resolved "https://registry.yarnpkg.com/table/-/table-4.0.2.tgz#a33447375391e766ad34d3486e6e2aedc84d2e36"
Expand Down

0 comments on commit 1395646

Please sign in to comment.