Skip to content

Commit 1395646

Browse files
vikaspotluri123acburdine
authored andcommitted
fix(doctor): Use systeminformation for memory availability
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
1 parent c8b6546 commit 1395646

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

lib/commands/doctor/checks/check-memory.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
'use strict';
2-
const os = require('os');
2+
const sysinfo = require('systeminformation');
33
const SystemError = require('../../../errors').SystemError;
44

55
const MB_IN_BYTES = 1048576;
66
const MIN_MEMORY = 150;
77

88
function checkMemory() {
9-
const availableMemory = os.freemem() / MB_IN_BYTES;
10-
if (availableMemory < MIN_MEMORY) {
11-
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.`));
12-
}
13-
return Promise.resolve();
9+
return sysinfo.mem().then((memoryInfo) => {
10+
const availableMemory = memoryInfo.available / MB_IN_BYTES;
11+
12+
if (availableMemory < MIN_MEMORY) {
13+
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.`));
14+
}
15+
return Promise.resolve();
16+
});
1417
}
1518

1619
module.exports = {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"stat-mode": "0.2.2",
8080
"strip-ansi": "4.0.0",
8181
"symlink-or-copy": "1.2.0",
82+
"systeminformation": "3.37.8",
8283
"tail": "1.2.3",
8384
"update-notifier": "2.3.0",
8485
"validator": "7.2.0",

test/unit/commands/doctor/checks/check-memory-spec.js

+18-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const expect = require('chai').expect;
33
const sinon = require('sinon');
44

5-
const os = require('os');
5+
const sysinfo = require('systeminformation');
66
const errors = require('../../../../../lib/errors');
77

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

37+
it('uses systeminformation to determine memory availability', function () {
38+
const memStub = sandbox.stub(sysinfo, 'mem').rejects(new Error('systeminformation'));
39+
const memCheck = require(modulePath);
40+
41+
return memCheck.task().catch(error => {
42+
expect(error).to.be.an('error');
43+
expect(error.message).to.equal('systeminformation');
44+
expect(memStub.calledOnce).to.be.true;
45+
});
46+
});
47+
3748
it('fails if not enough memory is available', function () {
38-
const osStub = sandbox.stub(os, 'freemem').returns(10);
49+
const memStub = sandbox.stub(sysinfo, 'mem').resolves({available: 10});
3950
const memCheck = require(modulePath);
40-
const ctx = {
41-
argv: {'check-mem': true}
42-
};
4351

44-
return memCheck.task(ctx).catch((error) => {
52+
return memCheck.task().catch((error) => {
4553
expect(error).to.be.an.instanceof(errors.SystemError);
4654
expect(error.message).to.match(/MB of memory available for smooth operation/);
47-
expect(osStub.calledOnce).to.be.true;
55+
expect(memStub.calledOnce).to.be.true;
4856
});
4957
});
5058

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

58-
return memCheck.task(ctx).then(() => {
59-
expect(osStub.calledOnce).to.be.true;
63+
return memCheck.task().then(() => {
64+
expect(memStub.calledOnce).to.be.true;
6065
});
6166
});
6267
});

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -4204,6 +4204,10 @@ [email protected]:
42044204
version "1.2.0"
42054205
resolved "https://registry.yarnpkg.com/symlink-or-copy/-/symlink-or-copy-1.2.0.tgz#5d49108e2ab824a34069b68974486c290020b393"
42064206

4207+
4208+
version "3.37.8"
4209+
resolved "https://registry.yarnpkg.com/systeminformation/-/systeminformation-3.37.8.tgz#0b42af1faf3c0b77788bbc5b56ca5f0e9f58ee73"
4210+
42074211
42084212
version "4.0.2"
42094213
resolved "https://registry.yarnpkg.com/table/-/table-4.0.2.tgz#a33447375391e766ad34d3486e6e2aedc84d2e36"

0 commit comments

Comments
 (0)