Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate error when failing to create directories on startup #1017

Merged
merged 1 commit into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vows framework is not supported in modern IDEs, it's a hassle to keep supporting it. Building new stuff in it makes no sense.

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