forked from acuminous/bosco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtail.js
70 lines (61 loc) · 1.63 KB
/
tail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var _ = require('lodash');
var async = require('async');
var pm2 = require('pm2');
var Tail = require('tail').Tail;
module.exports = {
name: 'tail',
description: 'Tails the logs from pm2',
usage: '[out|err] [-r <repoPattern>]',
};
function cmd(bosco, args) {
var repoPattern = bosco.options.repo;
var repoRegex = new RegExp(repoPattern);
// Connect or launch PM2
pm2.connect(function(err) {
if (err) {
bosco.error(err);
return;
}
function describeRunningServices(running) {
async.map(running, function(repo, next) {
if (repo.match(repoRegex)) {
pm2.describe(repo, function(err, list) {
if (err) {
bosco.error(err);
return;
}
var file = list[0].pm2_env.pm_out_log_path;
if (args[0] === 'err') {
file = list[0].pm2_env.pm_err_log_path;
}
bosco.log('Tailing ' + file);
var tail = new Tail(file);
tail.on('line', function(data) {
bosco.console.log(repo + ' ' + data);
});
tail.on('error', function(error) {
bosco.error(error);
});
});
} else {
next();
}
}, function(err) {
if (err) {
bosco.error(err);
process.exit(1);
}
process.exit(0);
});
}
function getRunningServices(next) {
pm2.list(function(err, list) {
next(err, _.pluck(list, 'name'));
});
}
getRunningServices(function(err, running) {
describeRunningServices(running);
});
});
}
module.exports.cmd = cmd;