From 9a0477299aa9ec1a676746a037cc1e7ef495a295 Mon Sep 17 00:00:00 2001 From: Tjatse Date: Tue, 29 Dec 2015 22:23:41 +0800 Subject: [PATCH] fix: tail log --- lib/monitor.js | 24 +++++---- lib/pm.js | 98 +++++++++++++++-------------------- web/public/css/auth.css | 7 +++ web/public/js/index.html.js | 3 +- web/templates/views/auth.jade | 2 +- 5 files changed, 65 insertions(+), 69 deletions(-) diff --git a/lib/monitor.js b/lib/monitor.js index cad52e6..96f469c 100644 --- a/lib/monitor.js +++ b/lib/monitor.js @@ -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!'); } diff --git a/lib/pm.js b/lib/pm.js index 04ea1d0..680a3fe 100644 --- a/lib/pm.js +++ b/lib/pm.js @@ -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; }; diff --git a/web/public/css/auth.css b/web/public/css/auth.css index 2412c84..d248c73 100644 --- a/web/public/css/auth.css +++ b/web/public/css/auth.css @@ -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; diff --git a/web/public/js/index.html.js b/web/public/js/index.html.js index 40bc4f6..77f44c7 100644 --- a/web/public/js/index.html.js +++ b/web/public/js/index.html.js @@ -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]; } diff --git a/web/templates/views/auth.jade b/web/templates/views/auth.jade index 8f6253e..49e5e7c 100644 --- a/web/templates/views/auth.jade +++ b/web/templates/views/auth.jade @@ -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