Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
rework main module and add better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
toddtreece committed Sep 8, 2014
1 parent 23a9a90 commit dcc184a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 37 deletions.
133 changes: 97 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

/**** Module dependencies ****/
var events = require('events'),
bunyan = require('bunyan'),
util = require('util');

/**** Make Phant an event emitter ****/
util.inherits(Phant, events.EventEmitter);

/**** Phant prototype ****/
var app = Phant.prototype;
var proto = Phant.prototype;

/**** Expose Phant ****/
exports = module.exports = Phant;
Expand All @@ -34,35 +35,90 @@ Phant.FieldStream = require('./lib/field_stream');
Phant.LimitStream = require('./lib/limit_stream');
Phant.SampleStream = require('./lib/sample_stream');

/**** Initialize a new Phant ****/
function Phant() {
/**** Phant constructor ****/
function Phant(config) {

if (!(this instanceof Phant)) {
return new Phant();
return new Phant(config);
}

events.EventEmitter.call(this);
this.on('error', this.handleError);

this.managers = [];
this.inputs = [];
this.outputs = [];
config = config || {};

}
if (!config.metadata) {
this.log.fatal('You must specify a phant metadata module in the config');
return process.exit(1);
}

if (!config.keychain) {
this.log.fatal('You must specify a phant keychain module in the config');
return process.exit(1);
}

/**** Log errors to console ****/
app.handleError = function() {
this.metadata = config.metadata;
this.validator = Phant.Validator({
metadata: this.metadata
});

console.error.apply(console, arguments);
if (Array.isArray(config.managers)) {
config.managers.forEach(this.registerManager.bind(this));
}

};
if (Array.isArray(config.inputs)) {
config.inputs.forEach(this.registerInput.bind(this));
}

if (Array.isArray(config.outputs)) {
config.outputs.forEach(this.registerOutput.bind(this));
}

}

proto.metadata = false;
proto.validator = false;
proto.log = bunyan.createLogger({
name: 'phant',
streams: [{
stream: process.stdout,
level: 'trace'
}, {
stream: process.stdout,
level: 'debug'
}, {
stream: process.stdout,
level: 'info'
}, {
stream: process.stderr,
level: 'warn'
}, {
stream: process.stderr,
level: 'error'
}, {
stream: process.stderr,
level: 'fatal'
}]
});

/**
* registerManager
*
* adds a new manager to the list of managers
*/
app.registerManager = function(manager) {
proto.registerManager = function(manager) {

manager.log = this.log.child({
module: manager.moduleName
});
manager.metadata = this.metadata;
manager.validator = this.validator;
manager.keychain = this.keychain;

manager.on('error', manager.log.error);

// listen for clear events and tell the outputs
// to wipe data if they are storing it
manager.on('clear', this.clearStream.bind(this));

// push to list of managers
this.managers.push(manager);
Expand All @@ -76,10 +132,16 @@ app.registerManager = function(manager) {
* inputs, and listens for data and
* errors.
*/
app.registerInput = function(input) {
proto.registerInput = function(input) {

// push to list of inputs
this.inputs.push(input);
input.log = this.log.child({
module: input.moduleName
});
input.validator = this.validator;
input.keychain = this.keychain;

// pipe errors to input logger
input.on('error', input.log.error);

// listen for data, and pipe it to outputs
input.on('data', this.dataReceived.bind(this));
Expand All @@ -88,11 +150,8 @@ app.registerInput = function(input) {
// to wipe data if they are storing it
input.on('clear', this.clearStream.bind(this));

// pipe input errors to phant error handler
input.on('error', this.handleError.bind(
input,
input.moduleName + ':'
));
// push to list of inputs
this.inputs.push(input);

};

Expand All @@ -102,37 +161,39 @@ app.registerInput = function(input) {
* adds a new output to the list of
* outputs, and listens for output errors.
*/
app.registerOutput = function(output) {
proto.registerOutput = function(output) {

output.log = this.log.child({
module: output.moduleName
});
output.validator = this.validator;
output.keychain = this.keychain;
output.metadata = this.metadata;

// pipe errors to output logger
output.on('error', output.log.error);

// push to list of outputs
this.outputs.push(output);

// pipe output errors to phant error handler
output.on('error', this.handleError.bind(
output,
output.moduleName + ':'
));

};

/**
* dataReceived
*
* send data to all registered outputs
*/
app.dataReceived = function(id, data) {
proto.dataReceived = function(id, data) {

// loop through all outputs and give
// them the new data.
this.outputs.forEach(function(output) {
output.write(id, data);
});

// let the managers know data was received
// so they can update the last_push timestamp
this.managers.forEach(function(manager) {
manager.touch(id);
});
// let the metadata module know data was received
// so it can update the last_push timestamp
this.metadata.touch(id);

};

Expand All @@ -141,7 +202,7 @@ app.dataReceived = function(id, data) {
*
* wipe the data from all persistent stores
*/
app.clearStream = function(id) {
proto.clearStream = function(id) {

// loop through all outputs and give
// them the new data.
Expand Down
2 changes: 1 addition & 1 deletion lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ app.aliasExists = function(alias, id, callback) {
return callback('no alias supplied', false);
}

if(reserved.indexOf(alias) !== -1) {
if (reserved.indexOf(alias) !== -1) {
return callback(null, true);
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"JSONStream": "^0.8.0",
"async": "~0.9.0",
"bunyan": "^1.0.1",
"csv-write-stream": "^0.1.1",
"datejs": "^1.0.0-rc1",
"formidable": "~1.0.14",
Expand Down

0 comments on commit dcc184a

Please sign in to comment.