This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
69 lines (62 loc) · 1.83 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
/* eslint-disable no-console */
const service = 'cp-organisations-service';
const store = require('seneca-postgres-store');
const util = require('util');
const dgram = require('dgram');
const config = require('./config/config.js')();
const seneca = require('./imports')(config);
const network = require('./network');
const senecaNR = require('seneca-newrelic');
const newrelic = process.env.NEW_RELIC_ENABLED === 'true' ? require('newrelic') : undefined;
const { isUndefined } = require('lodash');
seneca.use(store, config['postgresql-store']);
if (!isUndefined(newrelic)) {
seneca.use(senecaNR, {
newrelic,
roles: ['cd-organisations'],
filter(p) {
p.user = p.user ? p.user.id : undefined;
p.login = p.login ? p.login.id : undefined;
return p;
},
});
}
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
process.on('uncaughtException', shutdown);
process.on('SIGUSR2', shutdown);
function shutdown(err) {
if (err !== undefined) {
const error = {
date: new Date().toString(),
msg:
err.stack !== undefined
? `FATAL: UncaughtException, please report: ${util.inspect(err.stack)}`
: 'FATAL: UncaughtException, no stack trace',
err: util.inspect(err),
};
console.error(JSON.stringify(error));
process.exit(1);
}
process.exit(0);
}
require('./database/pg/migrate-psql-db.js')((err) => {
if (err) {
console.log(`Migrations Nok for ${service}`);
console.error(err);
process.exit(-1);
}
console.log(`Migrations ok for ${service}`);
network(seneca);
});
seneca.ready(() => {
const message = new Buffer(service);
const client = dgram.createSocket('udp4');
client.send(message, 0, message.length, 11404, 'localhost', (err) => {
if (err) {
console.error(err);
process.exit(-1);
}
client.close();
});
});