Skip to content
This repository has been archived by the owner on Jun 15, 2019. It is now read-only.

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
adikus committed Nov 9, 2013
1 parent cde2d50 commit fa74f75
Show file tree
Hide file tree
Showing 27 changed files with 1,420 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013 Andrej Hoos

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/wotcs-api-system');
6 changes: 6 additions & 0 deletions lib/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var cluster = require('cluster');

module.exports = function (rootDir, config) {
var App = cluster.isMaster ? require('./master_app.js') : require('./worker_app.js');
return new App(rootDir, config);
}
80 changes: 80 additions & 0 deletions lib/app/master_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var Eventer = require('../base/eventer');
var DB = require('../db');
var Server = require('../server');
var WebsocketServer = require('../server/ws');
var _ = require('underscore');
var Router = require('../router');
var WorkerManager = require('../workers');
var chokidar = require('chokidar');
var path = require('path');

module.exports = Eventer.extend({

init: function(dir, config) {
this.rootDir = dir;
this.config = config;
this.isMaster = true;
var self = this;
this.once('db.connected', function() {
self.models = require('../models')(self);
self.distributeModels();
self.queue.fillQueue();

if(self.dev){
var watcher = chokidar.watch(path.join(this.rootDir, 'models'));
watcher.on('change', function(path) {
console.log('Models reloaded');
delete require.cache[require.resolve('../models')];
self.models = require('../models')(self, true);
self.distributeModels();
});
}
});
this.router = new Router(require('../controllers')(this.rootDir), this.rootDir);
this.dev = process.env.ENV != 'production';
if(this.dev){
var watcher = chokidar.watch(path.join(this.rootDir, 'controllers'));
watcher.on('change', function() {
console.log('Controllers reloaded');
delete require.cache[require.resolve('../controllers')];
self.router.controllers = require('../controllers')(self.rootDir, true);
});
}

this.setupDatabases(this.config.db);
this.setupServer(this.config.server);
},

distributeModels: function() {
_.each(this.models, function(model, name) {
this[name] = model;
},this);
this.queue.setModels(this.models);
this.workerManager.setModels(this.models);
this.router.setModels(this.models);
},

setupDatabases: function(databases) {
this.db = new DB(databases);
this.propagateEvents('db');
},

setupServer: function(config) {
var self = this;
this.server = new Server(config, this.rootDir);
this.executeNowOrOnce(this.db.ready, 'db.connected', function(){
self.server.configureRoutes(self.router);
self.websocketServer = new WebsocketServer(self.server, self.workerManager, self.config.ws);
});
},

setupWorkers: function(count, file, queue){
this.queue = queue;
this.workerManager = new WorkerManager(queue, this.rootDir);
this.router.workerManager = this.workerManager;
for(var i = 0; i < count; i++){
this.workerManager.addWorker(file);
}
}

});
77 changes: 77 additions & 0 deletions lib/app/worker_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var Eventer = require('../base/eventer');
var DB = require('../db');
var _ = require('underscore');
var cluster = require('cluster');
var path = require('path');

module.exports = Eventer.extend({

init: function(dir, config) {
this.rootDir = dir;
this.config = config;
this.isMaster = false;

var self = this;
this.once('db.connected', function() {
self.models = require('../models')(self);
_.each(self.models, function(model, name) {
self[name] = model;
});
process.send(['waiting']);
});
process.on('message', function(msg) {
self.handleMessage(msg);
});

this.setupDatabases(config.db);
},

handleMessage: function(msg) {
var action = msg.shift();
if(action == 'emit'){
this.emit.apply(this, msg);
}else if(action == 'assign-worker'){
this.createWorker(msg.shift());
}else if(action == 'set-task'){
this.worker.setTask(msg.shift());
}else if(action == 'execute'){
var ID = msg.shift();
var method = msg.shift();
var data = this.worker ? this.worker[method].apply(this.worker, msg): {error: 'Worker not initialised'};
process.send(['emit', 'executed.'+ID, data]);
}else if(action == 'executeAsync'){
var ID = msg.shift();
var method = msg.shift();

msg.push(function() {
var args = _.toArray(arguments) ;
args.unshift('emit', 'executed.'+ID);
process.send(args);
});
if(this.worker){
this.worker[method].apply(this.worker, msg);
}else{
process.send(['emit', 'executed.'+ID, {error: 'Worker not initialised'}]);
}
}
},

createWorker: function(file){
this.workerFile = path.join(this.rootDir, file);
var Worker = require(this.workerFile);
this.worker = new Worker();
this.worker.on('*',function(event) {
var args = _.toArray(arguments);
args.unshift('emit');
process.send(args);
});
this.worker.setModels(this.models);
process.send(['emit', 'ready']);
},

setupDatabases: function(databases) {
this.db = new DB(databases);
this.propagateEvents('db');
}

});
68 changes: 68 additions & 0 deletions lib/base/class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

/* Simple JavaScript Inheritance
* By John Resig http://ejohn.org/
* MIT Licensed.
*/
// Inspired by base2 and Prototype
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

// The base Class implementation (does nothing)
Class = function() {};

// Create a new Class that inherits from this class
Class.extend = function(prop) {
var _super = this.prototype;

// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;

// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function(name, fn){
return function() {
var tmp = this._super;

// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];

// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;

return ret;
};
})(name, prop[name]) :
prop[name];
}

// The dummy class constructor
Class = function () {
// All construction is actually done in the init method
if ( !initializing && this.init )
this.init.apply(this, arguments);
};

// Populate our constructed prototype object
Class.prototype = prototype;

// Enforce the constructor to be what we expect
Class.constructor = Class;

// And make this class extendable
Class.extend = arguments.callee;

return Class;
};

if(!(typeof exports === 'undefined')) {
exports.Class = Class;
}

Loading

0 comments on commit fa74f75

Please sign in to comment.