Skip to content

Commit a852cdd

Browse files
vikaspotluri123acburdine
authored andcommitted
feat(doctor): check memory before running memory-intensive tasks
1 parent c4bc047 commit a852cdd

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const os = require('os');
3+
const SystemError = require('../../../errors').SystemError;
4+
5+
const MB_IN_BYTES = 1048576;
6+
const MIN_MEMORY = 150;
7+
8+
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();
14+
}
15+
16+
module.exports = {
17+
title: 'Checking memory availability',
18+
task: checkMemory,
19+
category: ['install', 'start', 'update']
20+
};

lib/commands/doctor/checks/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const validateConfig = require('./validate-config');
77
const folderPermissions = require('./folder-permissions');
88
const filePermissions = require('./file-permissions');
99
const contentFolder = require('./content-folder');
10+
const checkMemory = require('./check-memory');
1011

1112
module.exports = [
1213
nodeVersion,
@@ -16,5 +17,6 @@ module.exports = [
1617
validateConfig,
1718
folderPermissions,
1819
filePermissions,
19-
contentFolder
20+
contentFolder,
21+
checkMemory
2022
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
const expect = require('chai').expect;
3+
const sinon = require('sinon');
4+
5+
const os = require('os');
6+
const errors = require('../../../../../lib/errors');
7+
8+
const modulePath = '../../../../../lib/commands/doctor/checks/check-memory';
9+
10+
describe('Unit: Doctor Checks > Memory', function () {
11+
const sandbox = sinon.sandbox.create();
12+
13+
afterEach(() => {
14+
sandbox.restore();
15+
});
16+
17+
it('exports proper task', function () {
18+
const checkMem = require(modulePath);
19+
20+
expect(checkMem.title).to.equal('Checking memory availability');
21+
expect(checkMem.task).to.be.a('function');
22+
expect(checkMem.category).to.deep.equal(['install', 'start', 'update']);
23+
});
24+
25+
it('errors if not enough memory is available', function () {
26+
const osStub = sandbox.stub(os, 'freemem').returns(10);
27+
const memCheck = require(modulePath);
28+
29+
return memCheck.task().catch((error) => {
30+
expect(error).to.be.an.instanceof(errors.SystemError);
31+
expect(error.message).to.match(/MB of memory available for smooth operation/);
32+
expect(osStub.calledOnce).to.be.true;
33+
});
34+
});
35+
36+
it('works if there is enough memory', function () {
37+
const osStub = sandbox.stub(os, 'freemem').returns(157286400);
38+
const memCheck = require(modulePath);
39+
40+
return memCheck.task().then(() => {
41+
expect(osStub.calledOnce).to.be.true;
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)