fix: tail log

This commit is contained in:
Tjatse 2015-12-29 22:23:41 +08:00
parent 06fe8a43cf
commit 9a0477299a
5 changed files with 65 additions and 69 deletions

View File

@ -32,7 +32,7 @@ function Monitor(options) {
Monitor.ACCEPT_KEYS = ['pm2', 'refresh', 'daemonize', 'max_restarts', 'port', 'log', 'agent', 'remotes'];
Monitor.DEF_CONF_FILE = 'pm2-gui.ini';
Monitor.PM2_DAEMON_PROPS = ['DAEMON_RPC_PORT', 'DAEMON_PUB_PORT', 'PM2_LOG_FILE_PATH'];
Monitor.PM2_DAEMON_PROPS = ['DAEMON_RPC_PORT', 'DAEMON_PUB_PORT'];
/**
* Run socket.io server.
@ -170,7 +170,6 @@ Monitor.prototype._init = function (options) {
* @private
*/
Monitor.prototype._connectSysSock = function (socket) {
console.log('connection')
var self = this;
// Still has one client connects to server at least.
self._noClient = false;
@ -251,13 +250,16 @@ Monitor.prototype._connectLogSock = function (socket) {
}).join(keepANSI ? '\n' : '')
};
self._broadcast.call(self, 'log', data, conf.NSP.LOG);
}, function (err, tails) {
}, function (err, tail) {
if (err) {
return emitError(err, pm_id, keepANSI);
}
if (!tail) {
return emitError(new Error('No log can be found.'), pm_id, keepANSI);
}
console.info('[pm2:' + pm_id + ']', 'tail starting...');
self._tails[pm_id] = tails;
self._tails[pm_id] = tail;
});
}
@ -489,14 +491,14 @@ Monitor.prototype._killTailProcess = function (pm_id) {
var self = this;
function killTail(id) {
if (!Array.isArray(self._tails[id])) {
return delete self._tails[id];
var tail = self._tails[id];
if (!tail) {
return;
}
self._tails[id].forEach(function (tail) {
try {
tail.kill('SIGTERM');
} catch (err) {}
});
try {
tail.kill('SIGTERM');
} catch (err) {}
delete self._tails[id];
console.info('[pm2:' + id + ']', 'tail destroyed!');
}

View File

@ -235,8 +235,7 @@ pm.tail = function (opts, each, cb) {
}
proc.pm2_log = opts.logPath;
// Tail logs.
var tails = pm._tailLogs(proc, each);
cb(null, tails);
cb(null, pm._tailLogs(proc, each));
});
};
/**
@ -247,71 +246,60 @@ pm.tail = function (opts, each, cb) {
* @private
*/
pm._tailLogs = function (proc, cb) {
var logs = [
['PM2', proc.pm2_log]
];
var logs = {
'pm2': proc.pm2_log
};
if (proc.pm_log_path) {
logs.push(['entire', proc.pm2_env.pm_log_path]);
logs.entire = proc.pm2_env.pm_log_path;
} else {
var paths = [];
if (proc.pm2_env.pm_out_log_path) {
paths.push(['out', proc.pm2_env.pm_out_log_path]);
logs.out = proc.pm2_env.pm_out_log_path;
}
if (proc.pm2_env.pm_err_log_path) {
paths.push(['err', proc.pm2_env.pm_err_log_path]);
logs.err = proc.pm2_env.pm_err_log_path;
}
paths = paths.sort(function (a, b) {
return (fs.existsSync(a[1]) ? fs.statSync(a[1]).mtime.valueOf() : 0) -
(fs.existsSync(b[1]) ? fs.statSync(b[1]).mtime.valueOf() : 0);
});
logs = logs.concat(paths);
}
var tails = [];
(function tailLog(ls) {
var log = ls.shift();
if (!log) {
return;
var logFiles = [];
for (var key in logs) {
var file = logs[key];
if (fs.existsSync(file)) {
logFiles.push(file);
}
var logPath = log[1];
if (!fs.existsSync(logPath)) {
return;
}
var key = log[0],
prefix = chalk[key == 'err' ? 'red' : 'green'].bold('[' + key + ']');
}
if (logFiles.length == 0) {
return null;
}
var tail = spawn('tail', ['-n', 20, '-f'].concat(logFiles), {
killSignal: 'SIGTERM',
detached: true,
stdio: ['ignore', 'pipe', 'pipe']
});
var tail = spawn('tail', ['-f', '-n', 10, logPath], {
killSignal: 'SIGTERM',
stdio: [null, 'pipe', 'pipe']
});
// Use utf8 encoding.
tail.stdio.forEach(function (stdio) {
stdio && stdio.setEncoding('utf8');
});
// Use utf8 encoding.
tail.stdio.forEach(function (stdio) {
stdio.setEncoding('utf8');
});
// stdout.
tail.stdout.on('data', function (data) {
var lines = [],
_lines = data.split(/\n/);
_lines.forEach(function (line) {
if (!re_blank.test(line)) {
lines.push(prefix + ' ' + line);
}
});
if (lines.length > 0) {
cb(null, lines);
// stdout.
tail.stdout.on('data', function (data) {
var lines = [];
data.split(/\n/).forEach(function (line) {
if (!re_blank.test(line)) {
lines.push(line);
}
});
if (lines.length > 0) {
cb(null, lines);
}
});
// handle error.
tail.stderr.on('data', function (data) {
tail.disconnect();
cb(new Error(data.toString().replace(/\n/, '')));
});
tails.push(tail);
tailLog(ls);
})(logs);
return tails;
// handle error.
tail.stderr.on('data', function (data) {
console.error(data.toString());
tail.disconnect();
cb(new Error(data.toString().replace(/\n/, '')));
});
tail.unref();
return tail;
};

View File

@ -87,6 +87,13 @@ body {
box-shadow: inset 0 1px 4px #000, 0 1px #444;
}
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
-webkit-box-shadow: 0 0 0px 1000px #ccc inset;
}
.auth-form a {
margin-left: -24px;
border-radius: 24px;

View File

@ -131,7 +131,6 @@ function connectSocketServer(ns) {
* @param {String} err
*/
function onError(err) {
this.io.close();
if (err == 'unauthorized') {
err = 'There was an error with the authentication: ' + err;
} else {
@ -463,7 +462,7 @@ function addChooser(options) {
function changeConnection(connection) {
for (var ns in sockets) {
sockets[ns].disconnect();
sockets[ns].close();
sockets[ns].io.close();
delete sockets[ns];
}

View File

@ -8,7 +8,7 @@ block variables
block content
.auth-form
#logo(title='PM2 GUI')
input(placeholder='Authorization', type='password', maxlength='20')
input(placeholder='Authorization', type='password', maxlength='20', autocomplete='off')
a(href='#')
span