Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented log streaming

  • Loading branch information...
commit ed1a7d6f76c7a3974be7d6fb0fe7f18b9f73a78c 1 parent 5114563
@grownseed authored
Showing with 44 additions and 14 deletions.
  1. +1 −0  config.sample.json
  2. +4 −0 index.js
  3. +26 −7 lib/ishiki.js
  4. +13 −7 models/_base.js
View
1  config.sample.json
@@ -12,6 +12,7 @@
"port": "27017",
"database": "ishiki"
},
+ "logs-size": 100000,
"haibu": {
"env": "development",
"advanced-replies": true,
View
4 index.js
@@ -27,6 +27,7 @@ app.config.defaults({
port: 27017,
database: 'ishiki'
},
+ 'logs-size': 100000,
haibu: {
env: 'development',
'advanced-replies': true,
@@ -56,6 +57,9 @@ db.open(function(err, db) {
if (!err) {
console.log('Connected to ' + mongo_path);
+
+ //ensure log collection is a capped one
+ db.createCollection('log', {capped: true, size: app.config.get('logs-size')}, function(err, response) {});
}else{
console.log('Could not connect to ' + mongo_path);
}
View
33 lib/ishiki.js
@@ -275,9 +275,11 @@ module.exports = function(app, haibu, path, fs, drone, proxy) {
//return logs for given user/app
this.get('/:userid/:appid/logs', function(userid, appid) {
- var res = this.res,
+ var req = this.req,
+ res = this.res,
filter = {},
- options = {limit: 10, sort: {$natural: -1}};
+ options = {limit: 10, sort: {$natural: -1}},
+ stream = false;
options.user = userid;
options.app = appid;
@@ -287,14 +289,31 @@ module.exports = function(app, haibu, path, fs, drone, proxy) {
filter.type = this.req.body.type;
if (this.req.body.limit)
options.limit = this.req.body.limit.toInt();
+ if (this.req.body.stream)
+ stream = true;
}
- logModel.get(filter, options, function(err, results) {
- if (err)
- return haibu.sendResponse(res, 500, err);
+ logModel.get(filter, options, function(err, result) {
+ if (!stream) {
+ if (err)
+ return haibu.sendResponse(res, 500, err);
- haibu.sendResponse(res, 200, results);
- });
+ haibu.sendResponse(res, 200, result);
+ }else{
+ res.writeHead(200, {'Content-type': 'plain/text'});
+
+ result.on('data', function(item) {
+ var output = '[' + item.ts + '] ';
+
+ output += '[' + item.type + '] ';
+ output += item.msg;
+
+ res.write(output);
+ });
+
+ //need to find a solution to close stream when req cancelled
+ }
+ }, stream);
});
});
};
View
20 models/_base.js
@@ -7,7 +7,7 @@ var BaseModel = exports.BaseModel = function(options) {
};
//retrieve one entry if id passed, or several if {} passed
-BaseModel.prototype.get = function(filter, options, callback) {
+BaseModel.prototype.get = function(filter, options, callback, stream) {
if (!callback) {
callback = options;
options = {};
@@ -31,12 +31,18 @@ BaseModel.prototype.get = function(filter, options, callback) {
});
}
else if (typeof filter == 'object') {
- collection.find(filter, {}, options).toArray(function(err, data) {
- if (err)
- callback(err);
- else
- callback(null, data);
- });
+ if (!stream) {
+ collection.find(filter, {}, options).toArray(function(err, data) {
+ if (err)
+ callback(err);
+ else
+ callback(null, data);
+ });
+ }else{
+ var db_stream = collection.find(filter, {}, {tailable: true, timeout: false}).stream();
+
+ callback(null, db_stream);
+ }
}
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.