-
-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(doctor): check memory before running memory-intensive tasks
- Loading branch information
1 parent
c4bc047
commit a852cdd
Showing
3 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); | ||
}); |