Comparing version 1.0.1 to 1.0.2
'use strict'; | ||
var path = require('path'); | ||
var spawn = require('child_process').spawn; | ||
var exec = require('child_process').exec; | ||
var through = require('through2'); | ||
var sf = require('slice-file'); | ||
var fs = require('fs'); | ||
var MAX_LINES = 10; // 最多10行数据 | ||
var buffered = []; | ||
exports.logdir = ''; // 日志路径 | ||
var tail; | ||
var xs; | ||
var createFileIfNotExists = function(path) { | ||
// TODO: blocking i/o | ||
var fd = fs.openSync(path, 'a'); | ||
fs.close(fd); | ||
}; | ||
var date = function(now) { | ||
@@ -25,3 +33,3 @@ if (!now) { | ||
time: time, | ||
ms: ms | ||
ms: ms | ||
}; | ||
@@ -45,4 +53,3 @@ }; | ||
current: path.join(exports.logdir, 'node-' + now.date + '.log'), | ||
next: path.join(exports.logdir, 'node-' + next.date + '.log'), | ||
ms_to_refresh: diff | ||
timeToRefresh: diff | ||
}; | ||
@@ -52,7 +59,11 @@ } | ||
function startTailf(path) { | ||
var tail = spawn('tail', ['-f', path]); | ||
tail.stdout.on("data", function (data) { | ||
buffered.push(data); | ||
}); | ||
return tail; | ||
var xs = sf(path); | ||
xs.follow(-1).pipe(through(function(line, _, next){ | ||
buffered.push(line); | ||
if (buffered.length > MAX_LINES) { | ||
buffered.shift(); // 删掉前面的 | ||
} | ||
next(); | ||
})); | ||
return xs; | ||
} | ||
@@ -63,7 +74,3 @@ | ||
function getNodeLog() { | ||
var msg = Buffer.concat(buffered).toString(); | ||
// empty buffered array | ||
buffered = []; | ||
function getNodeLog(msg) { | ||
var matched; | ||
@@ -90,15 +97,11 @@ var result = {ok: true, data: []}; | ||
var tail; | ||
var init = function () { | ||
var info = getLogFileInfo(); | ||
tail = startTailf(info.current); | ||
exec('touch ' + info.next, function (error, stdout, stderr) { | ||
if (error) { | ||
console.log('exec error: ' + error); | ||
} | ||
}); | ||
createFileIfNotExists(info.current); | ||
xs = startTailf(info.current); | ||
setTimeout(function() { | ||
tail.kill(); | ||
xs.close(); | ||
init(); | ||
}, info.ms_to_refresh); | ||
}, info.timeToRefresh); | ||
}; | ||
@@ -112,6 +115,10 @@ | ||
exports.run = function (callback) { | ||
var msg = Buffer.concat(buffered).toString(); | ||
// empty buffered array | ||
buffered = []; | ||
callback(null, { | ||
type: 'node_log', | ||
metrics: getNodeLog() | ||
metrics: getNodeLog(msg) | ||
}); | ||
}; |
{ | ||
"name": "agentx", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "agentx is powered by alinode", | ||
@@ -15,3 +15,5 @@ "scripts": { | ||
"ws": "^0.7.2", | ||
"debug": "*" | ||
"debug": "*", | ||
"slice-file": "*", | ||
"through2": "^0.6.5" | ||
}, | ||
@@ -18,0 +20,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15047
488
1
4
2
5
+ Addedslice-file@*
+ Addedthrough2@^0.6.5
+ Addedcore-util-is@1.0.3(transitive)
+ Addedinherits@1.0.22.0.4(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedreadable-stream@1.0.34(transitive)
+ Addedslice-file@1.0.0(transitive)
+ Addedsplit2@0.2.1(transitive)
+ Addedstring_decoder@0.10.31(transitive)
+ Addedthrough2@0.6.5(transitive)
+ Addedxtend@4.0.2(transitive)