Skip to content

Commit

Permalink
feat(doctor): check memory before running memory-intensive tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
vikaspotluri123 authored and acburdine committed Mar 25, 2018
1 parent c4bc047 commit a852cdd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lib/commands/doctor/checks/check-memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const os = require('os');
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();
}

module.exports = {
title: 'Checking memory availability',
task: checkMemory,
category: ['install', 'start', 'update']
};
4 changes: 3 additions & 1 deletion lib/commands/doctor/checks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const validateConfig = require('./validate-config');
const folderPermissions = require('./folder-permissions');
const filePermissions = require('./file-permissions');
const contentFolder = require('./content-folder');
const checkMemory = require('./check-memory');

module.exports = [
nodeVersion,
Expand All @@ -16,5 +17,6 @@ module.exports = [
validateConfig,
folderPermissions,
filePermissions,
contentFolder
contentFolder,
checkMemory
];
44 changes: 44 additions & 0 deletions test/unit/commands/doctor/checks/check-memory-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';
const expect = require('chai').expect;
const sinon = require('sinon');

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

const modulePath = '../../../../../lib/commands/doctor/checks/check-memory';

describe('Unit: Doctor Checks > Memory', function () {
const sandbox = sinon.sandbox.create();

afterEach(() => {
sandbox.restore();
});

it('exports proper task', function () {
const checkMem = require(modulePath);

expect(checkMem.title).to.equal('Checking memory availability');
expect(checkMem.task).to.be.a('function');
expect(checkMem.category).to.deep.equal(['install', 'start', 'update']);
});

it('errors if not enough memory is available', function () {
const osStub = sandbox.stub(os, 'freemem').returns(10);
const memCheck = require(modulePath);

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;
});
});

it('works if there is enough memory', function () {
const osStub = sandbox.stub(os, 'freemem').returns(157286400);
const memCheck = require(modulePath);

return memCheck.task().then(() => {
expect(osStub.calledOnce).to.be.true;
});
});
});

0 comments on commit a852cdd

Please sign in to comment.