Skip to content

Commit

Permalink
Drop connection after request ended
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Nov 5, 2011
1 parent dac6057 commit 832b5f3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
9 changes: 8 additions & 1 deletion lib/action_observer.js
Expand Up @@ -18,7 +18,14 @@ function ActionObserver(base_path, ctl_name, ctl_file) {
};
this.call = function (action_name, req, res) {
var controller = ActionObserver.loadController(base_path, ctl_name, ctl_file);
controller.perform(action_name, req, res);
if (app.disabled('model cache')) {
console.log('reloading models');
app.reloadModels(function () {
controller.perform(action_name, req, res);
});
} else {
controller.perform(action_name, req, res);
}
};
this.calling = function (action_name) {
var observer = this;
Expand Down
26 changes: 20 additions & 6 deletions lib/controller.js
Expand Up @@ -168,10 +168,6 @@ function Controller(name, file, basePath) {
this.path_to = Controller.getPathTo(actionName, req, res);
this.init();

if (app.disabled('model cache')) {
app.reloadModels();
}

log('');
log($((new Date).toString()).yellow);
log($(req.method).bold, $(req.url).grey, 'controller:', $(this.controllerFullName).green, 'action:', $(this.actionName).blue);
Expand All @@ -197,6 +193,10 @@ function Controller(name, file, basePath) {
queue.push(getCaller(actions[actionName]));
enqueue(afterFilters, queue);

if (app.disabled('model cache')) {
queue.push(getCaller(app.disconnectSchemas));
}

next();

function next() {
Expand All @@ -213,7 +213,18 @@ function Controller(name, file, basePath) {
}

function getCaller(method) {
return app.disabled('log actions') ? method : function (next) {
var wrapper = method;
if (method.isAction) {
// need to mark method as "in action"
wrapper = function (next) {
req.inAction = true;
method.call(this, next);
};
}
return app.disabled('log actions') ? wrapper : function (next) {
if (method.isAction) {
req.inAction = true;
}
if (method.customName) {
if (method.isAction) {
log('>>> perform ' + $(method.customName).bold.blue);
Expand All @@ -223,7 +234,7 @@ function Controller(name, file, basePath) {
}
timeStart = Date.now();
prevMethod = method;
method.call(this, next);
wrapper.call(this, next);
}
}

Expand Down Expand Up @@ -288,11 +299,13 @@ function Controller(name, file, basePath) {
Controller.prototype.send = function (x) {
log('Send to client: ' + x);
this.response.send.apply(this.response, Array.prototype.slice.call(arguments));
if (this.request.inAction) this.next();
};

Controller.prototype.redirect = function (path) {
log('Redirected to', $(path).grey);
this.response.redirect(path.toString());
if (this.request.inAction) this.next();
};

Controller.prototype.render = function (arg1, arg2) {
Expand Down Expand Up @@ -335,6 +348,7 @@ Controller.prototype.render = function (arg1, arg2) {
layout: layout ? 'layouts/' + layout : false,
debug: false
});
if (this.request.inAction) this.next();
};

Controller.prototype.flash = function () {
Expand Down
39 changes: 34 additions & 5 deletions lib/models.js
Expand Up @@ -9,10 +9,13 @@ var fs = require('fs'),
singularize = utils.singularize,
pluralize = utils.pluralize,
runCode = utils.runCode;
_schemas = [];

function prepareContext(exportModels, defSchema) {
function prepareContext(exportModels, defSchema, done) {
var ctx = {app: app},
models = {}, cname, schema;
models = {}, cname, schema, wait = connected = 0;

done = done || function () {};

/**
* Multiple schemas support
Expand All @@ -31,6 +34,12 @@ function prepareContext(exportModels, defSchema) {
var opts = argument('object') || {};
var def = argument('function') || function () {};
schema = new Schema(name || opts.driver || 'memory', opts);
_schemas.push(schema);
wait += 1;
ctx.gotSchema = true;
schema.on('connected', function () {
if (wait === ++connected) done();
});
def();
schema = false;
};
Expand Down Expand Up @@ -72,10 +81,10 @@ function prepareContext(exportModels, defSchema) {
/**
* Initialize models
*/
exports.init = function () {
exports.init = function (callback) {
var result = {}, ormDriver, context,
config, env = process.env.NODE_ENV || 'development',
app_root = app.root;
app_root = app.root, wait = 0;

if (!require('path').existsSync(app.root + '/config')) {
return;
Expand All @@ -89,8 +98,16 @@ exports.init = function () {
}

var schema = new Schema(config && config.driver || 'memory', config);
_schemas.push(schema);
wait += 1;
schema.on('connected', done);

context = prepareContext(result, schema);
wait += 1;
context = prepareContext(result, schema, done);

function done() {
if (--wait === 0 && callback) callback();
}

// code coverage support
if (process.cov) context.__cov = __cov;
Expand All @@ -104,6 +121,8 @@ exports.init = function () {
}
runCode(schemaFile, context);

if (!context.gotSchema) done();

// then run models
fs.readdirSync(app_root + '/app/models/').forEach(function (file) {
if (file.match(/^[^\.].*?\.(js|coffee)$/)) {
Expand All @@ -123,3 +142,13 @@ exports.init = function () {
app.models = result;
return result;
};

app.disconnectSchemas = function disconnectSchemas() {
if (_schemas.length) {
_schemas.forEach(function (schema) {
schema.disconnect();
});
_schemas = [];
}
}

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -15,7 +15,7 @@
},
"dependencies": {
"connect": ">= 0",
"jugglingdb": ">= 0.0.2",
"jugglingdb": ">= 0.0.3",
"express": ">= 2.2.2",
"yaml": ">= 0.1.2",
"coffee-script": ">= 1.1.1",
Expand Down

0 comments on commit 832b5f3

Please sign in to comment.