This repository has been archived by the owner on May 29, 2021. It is now read-only.
forked from smartprix/pm2-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (70 loc) · 2.03 KB
/
index.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
/**
* Copyright 2018 rohit-smpx, vmarchaud. All rights reserved.
* Use of this source code is governed by a license that
* can be found in the LICENSE file.
*/
const pmx = require('pmx');
const pm2 = require('pm2');
const {URL} = require('url');
const path = require('path');
const version = require('./package.json').version;
const Worker = require('./lib/Worker');
const slack = require('./lib/slack');
const logger = require('./lib/logger');
const db = require('./lib/db');
function updateConf(conf, worker) {
pmx.configureModule({
human_info: [
['Status', 'Launched'],
['Version', version],
['Port', conf.port],
['Apps', worker && Object.keys(worker.apps || {}).toString()],
['Tests', worker && Object.keys(worker.apps || {}).filter(app => ((worker.apps || {})[app] || {}).tests || '').toString()],
['Slack Channel', conf.slackChannel || 'N/A'],
['Host', (conf.wwwUrl || {}).origin],
['Queue Size', worker && worker.queue.size()]
],
});
}
/**
* Init pmx module
*/
pmx.initModule({}, async (err, conf) => {
conf.wwwUrl = new URL(conf.host);
if (conf.host.lastIndexOf(':') < 7) {
conf.wwwUrl.port = conf.port;
}
conf.dataDir = path.resolve(conf.dataDir);
// logger.init(`${conf.dataDir}/logs`);
slack.init(conf);
db.setPath(`${conf.dataDir}/db`);
process.on('uncaughtException', (error) => {
logger.error('UncaughtException:', error.message);
logger.error(error.stack);
process.exit(1);
});
// init only if we can connect to pm2
await new Promise((resolve, reject) => {
pm2.connect(async (err2) => {
if (err || err2) {
logger.error('Startup Error: %s', JSON.stringify(err || err2));
reject(err || err2);
return;
}
// Compact db and delete older reports data & files (2 weeks) at start
await db.optimiseDbs(conf.dataDir);
conf.apps = await Worker.getApps();
const worker = new Worker(conf, updateConf);
updateConf(conf, worker);
try {
await worker.start();
}
catch (err3) {
logger.error(err3);
reject(err3);
return;
}
resolve();
});
});
});