forked from hyperledger-labs/blockchain-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.js
77 lines (66 loc) · 1.67 KB
/
sync.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
/**
* SPDX-License-Identifier: Apache-2.0
*/
const Synchronizer = require('./app/Synchronizer');
const helper = require('./app/common/helper');
const logger = helper.getLogger('Sync');
const ExplorerError = require('./app/common/ExplorerError');
const args = process.argv.slice(2);
let synchronizer;
async function start() {
logger.debug('Start synchronizer');
synchronizer = new Synchronizer(args);
await synchronizer.initialize();
logger.info(`Synchronizer pid is ${process.pid}`);
}
start();
/*
* This function is called when you want the server to die gracefully
* i.e. wait for existing connections
*/
const shutDown = function() {
logger.info(
'<<<<<<<<<<<<<<<<<<<<<<<<<< Closing client processor >>>>>>>>>>>>>>>>>>>>>'
);
if (synchronizer) {
synchronizer.close();
}
setTimeout(() => {
process.exit(0);
setTimeout(() => {
logger.error(
'Could not close child connections in time, forcefully shutting down'
);
if (synchronizer) {
synchronizer.close();
}
process.exit(1);
}, 5000);
}, 2000);
};
process.on('unhandledRejection', up => {
logger.error(
'<<<<<<<<<<<<<<<<<<<<<<<<<< Synchronizer Error >>>>>>>>>>>>>>>>>>>>>'
);
if (up instanceof ExplorerError) {
logger.error('Error : ', up.message);
} else {
logger.error(up);
}
shutDown();
});
process.on('uncaughtException', up => {
logger.error(
'<<<<<<<<<<<<<<<<<<<<<<<<<< Synchronizer Error >>>>>>>>>>>>>>>>>>>>>'
);
if (up instanceof ExplorerError) {
logger.error('Error : ', up.message);
} else {
logger.error(up);
}
shutDown();
});
// Listen for TERM signal .e.g. kill
process.on('SIGTERM', shutDown);
// Listen for INT signal e.g. Ctrl-C
process.on('SIGINT', shutDown);