Skip to content

Commit

Permalink
Propagate error when failing to create directories on startup (#1017)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Feb 26, 2019
1 parent 6ff9344 commit 01fb537
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
24 changes: 6 additions & 18 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var fs = require('fs'),
utile = require('utile'),
winston = require('winston'),
mkdirp = utile.mkdirp,
async = utile.async;
async = utile.async,
configUtils = require('./util/config-utils');

var forever = exports;

Expand Down Expand Up @@ -49,7 +50,7 @@ forever.initialized = false;
forever.kill = require('forever-monitor').kill;
forever.checkProcess = require('forever-monitor').checkProcess;
forever.root = process.env.FOREVER_ROOT || path.join(process.env.HOME || process.env.USERPROFILE || '/root', '.forever');
forever.config = new nconf.File({ file: path.join(forever.root, 'config.json') });
forever.config = configUtils.initConfigFile(forever.root);
forever.Forever = forever.Monitor = require('forever-monitor').Monitor;
forever.Worker = require('./forever/worker').Worker;
forever.cli = require('./forever/cli');
Expand Down Expand Up @@ -334,22 +335,9 @@ forever.load = function (options) {
forever._debug();
}

//
// Syncronously create the `root` directory
// and the `pid` directory for forever. Although there is
// an additional overhead here of the sync action. It simplifies
// the setup of forever dramatically.
//
function tryCreate(dir) {
try {
fs.mkdirSync(dir, '0755');
}
catch (ex) { }
}

tryCreate(forever.config.get('root'));
tryCreate(forever.config.get('pidPath'));
tryCreate(forever.config.get('sockPath'));
configUtils.tryCreateDir(forever.config.get('root'));
configUtils.tryCreateDir(forever.config.get('pidPath'));
configUtils.tryCreateDir(forever.config.get('sockPath'));

//
// Attempt to save the new `config.json` for forever
Expand Down
29 changes: 29 additions & 0 deletions lib/util/config-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var path = require('path');
var fs = require('fs');
var nconf = require('nconf');

function initConfigFile(foreverRoot) {
return new nconf.File({file: path.join(foreverRoot, 'config.json')});
}

//
// Synchronously create the `root` directory
// and the `pid` directory for forever. Although there is
// an additional overhead here of the sync action. It simplifies
// the setup of forever dramatically.
//
function tryCreateDir(dir) {
try {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, '0755');
}
}
catch (error) {
throw new Error('Failed to create directory '+dir+":" +error.message);
}
}

module.exports = {
initConfigFile: initConfigFile,
tryCreateDir: tryCreateDir
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
},
"devDependencies": {
"broadway": "~0.3.6",
"chai": "^4.2.0",
"eventemitter2": "0.4.x",
"mocha": "^3.5.3",
"moment": "^2.23.0",
"request": "2.x.x",
"vows": "0.7.x"
Expand Down
30 changes: 30 additions & 0 deletions test/util/config-utils-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var fs = require('fs');
var configUtils = require('../../lib/util/config-utils');
var expect = require('chai').expect;

describe('config-utils', () => {
describe('tryCreateDir', () => {
it('happy path', () => {
expect(() => {
configUtils.tryCreateDir('happypath');
}).to.not.throw();

expect(fs.existsSync('happypath')).to.equal(true);
fs.rmdirSync('happypath');
});

it('throws an error on invalid directory', () => {
expect(() => {
configUtils.tryCreateDir('');
}).to.throw(/Failed to create directory :ENOENT: no such file or directory, mkdir/);
});

it('does not fail when creating directory that already exists', () => {
expect(() => {
configUtils.tryCreateDir('dummy');
configUtils.tryCreateDir('dummy');
}).to.not.throw();
fs.rmdirSync('dummy');
});
});
});

0 comments on commit 01fb537

Please sign in to comment.