Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

File-utils for Windows compat, improved logfile naming-format.

  • Loading branch information...
commit 57d54634debd8a537c3e6e97eb4a0c3cbcf9ad46 1 parent 29b7e6d
@mde mde authored
Showing with 68 additions and 114 deletions.
  1. +2 −1  bin/cli.js
  2. +15 −25 lib/app.js
  3. +28 −46 lib/cluster/master.js
  4. +23 −42 lib/init/i18n.js
View
3  bin/cli.js
@@ -5,6 +5,7 @@ require('../lib/geddy');
var fs = require('fs')
, exec = require('child_process').exec
+ , path = require('path')
, parseopts = require('../lib/parseopts')
, utils = require('../lib/utils/index')
, App = require('../lib/app.js').App
@@ -124,7 +125,7 @@ if (typeof opts.help != 'undefined') {
else {
// `geddy app foo` or `geddy resource bar` etc. -- run generators
if (cmds.length) {
- filepath = __dirname + '/../Jakefile';
+ filepath = path.normalize(path.join(__dirname, '../Jakefile'));
if (process.platform == 'win32') {
filepath = '"' + filepath + '"';
}
View
40 lib/app.js
@@ -19,13 +19,13 @@
var fs = require('fs')
, url = require('url')
, querystring = require('querystring')
- , exec = require('child_process').exec
, path = require('path')
, dir = process.cwd()
, errors = require('./errors')
, response = require('./response')
, model = require('./model')
, utils = require('./utils/index')
+ , fileUtils = require('./utils/file')
, inflection = require('../deps/inflection')
, i18n = require('./i18n')
, init = require('./init')
@@ -164,31 +164,21 @@ var App = function () {
next();
}
else {
- exec('find ' + viewsPath, function (err, stdout, stderr) {
- var templates
- , files
- , file
- , pat = /\.ejs$/;
- if (err) {
- throw err;
+ var templates = {}
+ , files
+ , file
+ , pat = /\.ejs$/;
+ files = fileUtils.readdirR(viewsPath);
+ for (var i = 0; i < files.length; i++) {
+ file = files[i];
+ if (pat.test(file)) {
+ file = file.replace(dir + '/', '');
+ file = path.normalize(file);
+ templates[file] = true;
}
- else if (stderr) {
- geddy.log.error('Error: ' + stderr);
- }
- else {
- templates = {};
- files = stdout.split('\n');
- for (var i = 0; i < files.length; i++) {
- file = files[i];
- if (pat.test(file)) {
- file = file.replace(dir + '/', '');
- templates[file] = true;
- }
- }
- self.templateRegistry = templates;
- next();
- }
- });
+ }
+ self.templateRegistry = templates;
+ next();
}
}
View
74 lib/cluster/master.js
@@ -1,10 +1,12 @@
var Master
, cluster
- , exec = require('child_process').exec
, fs = require('fs')
+ , path = require('path')
, watchFiles = require('../watch_files')
, Log = require('../../deps/log')
- , dispatch = require('./master_dispatch');
+ , dispatch = require('./master_dispatch')
+ , fileUtils = require('../utils/file')
+ , dateUtils = require('../utils/date').date;
if (!geddy.FD_HACK) {
cluster = require('cluster');
@@ -65,48 +67,14 @@ Master.prototype = new (function () {
, _initLogging = function (next) {
var self = this
- , callback
, levelsByType
, stdoutLevel
, types = ['stdout', 'stderr', 'access']
- , now = (new Date()).getTime()
+ , now = dateUtils.strftime(new Date(), '%FT%T')
, loggly
, dir = this.config.logDir
, rotateAndInitByType;
- callback = function () {};
-
- // Recursive function for rotating and initializing each of the log-types.
- // Calls the passed-in callback when the entire process is done
- rotateAndInitByType = function () {
- var type = types.shift()
- , cmd
- , nextLog;
- // Grab the next logger-type, if any
- if (type) {
- // Rename the log file, ex.: mv logs/access.log logs/access.<TIMESTAMP>.log
- cmd = 'mv ' + dir + '/' + type + '.log ' + dir + '/' +
- type + '.' + now + '.log';
- // After the file is renmaed, create the new logger with the original filename
- // e.g., access.log
- nextLog = function () {
- self[type + 'Log'] = new Log(levelsByType[type],
- fs.createWriteStream(dir +
- '/' + type + '.log'), true, loggly);
- self[type + 'Log'].type = type;
- // Go on to the next logger type until none are left
- rotateAndInitByType();
- };
- exec(cmd, nextLog);
- }
- // No logger-types left, continue on with the main init process
- else {
- self.stdoutLog.info('Server starting with config: ' +
- JSON.stringify(self.config, true, 2));
- next();
- }
- };
-
// Set the logging level for stdout
if (this.config.debug) {
stdoutLevel = Log.DEBUG;
@@ -126,15 +94,26 @@ Master.prototype = new (function () {
};
// Create the log directory if it doesn't exist
- exec('mkdir -p ' + dir, function (err) {
- if (err) {
- throw err;
+ fileUtils.mkdirP(dir);
+
+ types.forEach(function (type) {
+ var oldLog = dir + '/' + type + '.log'
+ , newLog = dir + '/' + type + '.' + now + '.log';
+ if (path.existsSync(oldLog)) {
+ fs.renameSync(oldLog, newLog);
}
+ // After the file is renmaed, create the new logger with the original filename
+ // e.g., access.log
+ self[type + 'Log'] = new Log(levelsByType[type],
+ fs.createWriteStream(dir +
+ '/' + type + '.log'), true, loggly);
+ self[type + 'Log'].type = type;
});
-
- // Kick the log-init process off
- rotateAndInitByType();
+ this.stdoutLog.info('Server starting with config: ' +
+ JSON.stringify(self.config, true, 2));
+ next();
}
+
, _startMetrics = function (next) {
var metrics
, port;
@@ -254,9 +233,12 @@ Master.prototype = new (function () {
this.config = {};
this.workers = new geddy.SortedCollection();
this.processMode = processModes.KEEP_ALIVE;
- process.addListener('SIGTERM', function () {
- self.startShutdown();
- });
+ // Don't bother with graceful shutdown for win32
+ if (!process.platform == 'win32') {
+ process.addListener('SIGTERM', function () {
+ self.startShutdown();
+ });
+ }
};
this.start = function (options) {
View
65 lib/init/i18n.js
@@ -1,7 +1,8 @@
var i18n = require('../i18n')
, exec = require('child_process').exec
, path = require('path')
- , fs = require('fs');
+ , fs = require('fs')
+ , fileUtils = require('../utils/file');
module.exports = new (function () {
var LOCALE_PAT = /([^\/]*).json$/;
@@ -14,53 +15,33 @@ module.exports = new (function () {
__dirname + '/../../templates/locales'
]
, loadLocaleData = function () {
- directory = localePaths.shift();
- if (directory) {
+ localePaths.forEach(function (directory) {
+ directory = path.normalize(directory);
if (path.existsSync(directory)) {
- exec('find ' + directory + ' | grep json$',
- function (err, stdout, stderr) {
- var files
- , file
- , locale
- , data;
- if (err) {
- throw err;
- }
- else if (stderr) {
- geddy.log.error('Error: ' + stderr);
- }
- else {
- files = stdout.split('\n');
- for (var i = 0; i < files.length; i++) {
- file = files[i];
- if (file) {
- // Extract the locale-name from the filename (e.g.,
- // foo/bar/baz/en-us.json => 'en-us'
- if ((locale = LOCALE_PAT.exec(file)) && locale[1]) {
- try {
- data = fs.readFileSync(file).toString();
- data = JSON.parse(data);
- }
- catch(e) {
- throw new Error('Could not parse locale-data in file: ' +
- file);
- }
- i18n.loadLocale(locale[1], data);
- }
+ var files = fileUtils.readdirR(directory);
+ for (var i = 0; i < files.length; i++) {
+ file = files[i];
+ if (file && /.json$/.test(file)) {
+ // Extract the locale-name from the filename (e.g.,
+ // foo/bar/baz/en-us.json => 'en-us'
+ if ((locale = LOCALE_PAT.exec(file)) && locale[1]) {
+ try {
+ data = fs.readFileSync(file).toString();
+ data = JSON.parse(data);
+ }
+ catch(e) {
+ throw new Error('Could not parse locale-data in file: ' +
+ file);
}
+ i18n.loadLocale(locale[1], data);
}
- loadLocaleData();
}
- });
+ }
}
- else {
- loadLocaleData();
- }
- }
- else {
- callback();
- }
+ });
+ callback();
};
+
localePaths = localePaths.concat(geddy.config.i18n.loadPaths);
loadLocaleData();
};
Please sign in to comment.
Something went wrong with that request. Please try again.