-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
170 lines (142 loc) · 3.94 KB
/
app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var pmx = require('pmx');
var pm2 = require('pm2');
var async = require('async');
var vizion = require('vizion');
var child = require('child_process');
/******************************
* ______ _______ ______
* | __ \ | |__ |
* | __/ | __|
* |___| |__|_|__|______|
*
* PM2 Module Sample
*
******************************/
/**
* Module system documentation
* http://bit.ly/1hnpcgu
*
* Start module in development mode
* $ cd to my-module
* $ pm2 install .
*
* Official modules are published here
* https://github.com/pm2-hive
*/
/**
* Module Entry Point
*
* We first initialize the module by calling
* pmx.initModule({}, cb);
*
*
* More options: http://bit.ly/1EpagZS
*
*/
pmx.initModule({
// Options related to the display style on Keymetrics
widget: {
// Logo displayed
logo: 'https://app.keymetrics.io/img/logo/keymetrics-300.png',
// Module colors
// 0 = main element
// 1 = secondary
// 2 = main border
// 3 = secondary border
theme: ['#141A1F', '#222222', '#3ff', '#3ff'],
// Section to show / hide
el: {
probes: true,
actions: true
},
// Main block to show / hide
block: {
actions: false,
issues: true,
meta: true,
}
}
}, function (err, conf) {
pm2.connect(function () {
console.log((new Date()).toISOString() + ': Module pm2-pull-reload connected to PM2');
var running = false;
setInterval(function () {
if (running == true) return false;
running = true;
pull(function () {
running = false;
});
}, conf.interval || 60000);
});
});
var exec = function (cmd, cb) {
var output = '';
var c = child.exec(cmd, {}, function (err) {
if (cb) cb(err ? err.code : 0, output);
});
c.stdout.on('data', function (data) {
output += data;
});
c.stderr.on('data', function (data) {
output += data;
});
};
var execSeries = function (cmds, cb) {
var stdout = '';
async.eachSeries(cmds, function (cmd, cb) {
stdout += '\n' + cmd;
console.log((new Date()).toISOString() + `: Running command "${cmd}"`);
exec(cmd, function (code, output) {
stdout += '\n' + output;
if (code === 0) return cb()
return cb('"' + cmd + '" failed');
});
}, function (err) {
if (err) return cb(stdout + '\n' + err);
return cb(null, stdout);
});
}
var groupBy = function (items, key) {
return items.reduce(function (result, item) {
//(result[item[key]] = result[item[key]] || []).push(item);
result[item[key]] = item;
return result;
}, {});
};
var pull = function (cb) {
pm2.list(function (err, procs) {
if (err) {
console.error((new Date()).toISOString() + ': ' + err);
return cb();
}
async.eachSeries(groupBy(procs, 'name'), function (proc, next) {
if (proc.pm2_env && proc.pm2_env.versioning) {
vizion.update({ folder: proc.pm2_env.pm_cwd }, function (err, meta) {
if (err) return next(err);
if (meta && meta.success) {
console.log((new Date()).toISOString() + `: Started reloading process "${proc.name}"`);
var commands = [
'cd ' + proc.pm2_env.pm_cwd + ' && npm install',
'cd ' + proc.pm2_env.pm_cwd + ' && npm run build --if-present',
]
execSeries(commands, function (err) {
if (err) return next(err);
if (proc.pm2_env.status !== 'online') return next();
pm2.reload(proc.name, function () {
console.log((new Date()).toISOString() + `: Successfully reloaded process "${proc.name}"`);
return next();
});
})
} else {
return next();
}
});
} else {
return next();
}
}, function (err) {
if (err) console.error((new Date()).toISOString() + ': ' + err);
return cb();
});
});
}