-
Notifications
You must be signed in to change notification settings - Fork 303
/
index.ts
48 lines (46 loc) · 1.29 KB
/
index.ts
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
/**
* Entry point for the application.
* */
import axios from 'axios';
import cp from 'child_process';
import fs from 'fs';
async function start() {
if (process.env.PROVIDER === 'gce' && !fs.existsSync('/usr/src/.env')) {
const resp = await axios.get(
'http://metadata.google.internal/computeMetadata/v1/project/attributes/env',
{
headers: {
'Metadata-Flavor': 'Google',
},
responseType: 'arraybuffer',
},
);
fs.writeFileSync('/usr/src/.env', resp.data);
}
if (process.env.ROLE) {
// if role variable is set just run that script
import('./svc/' + process.env.ROLE + '.js');
} else if (process.env.GROUP) {
// or run the group with pm2
cp.execSync('pm2 start ecosystem.config.js');
setInterval(
() => {
cp.execSync('pm2 flush');
},
24 * 60 * 60 * 1000,
);
} else {
// Block indefinitely (keep process alive for Docker)
process.stdin.resume();
}
}
start();
process.on('uncaughtException', async (err) => {
console.error(err);
process.exit(1);
});
process.on('unhandledRejection', async (reason, p) => {
// In production pm2 doesn't appear to auto restart unless we exit the process here
console.error('Unhandled Rejection at: Promise', p, 'reason:', reason);
process.exit(1);
});