This repository was archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.js
54 lines (50 loc) · 1.81 KB
/
bootstrap.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
const configIt = require('@hkube/config');
const Logger = require('@hkube/logger');
const { main, logger } = configIt.load();
const log = new Logger(main.serviceName, logger);
const { StorageManager } = require('@hkube/storage-manager');
const component = require('./lib/consts/componentNames').MAIN;
const cleanerManager = require('./lib/cleaners/cleaner-manager');
class Bootstrap {
async init() {
try {
this._handleErrors();
log.info(`running application with env: ${configIt.env()}, version: ${main.version}, node: ${process.versions.node}`, { component });
const storageManager = new StorageManager();
await storageManager.init(main, log);
await cleanerManager.init(storageManager, main, log);
await cleanerManager.start();
return main;
}
catch (error) {
this._onInitFailed(error);
}
return true;
}
_onInitFailed(error) {
log.error(error.message, { component }, error);
process.exit(1);
}
_handleErrors() {
process.on('exit', (code) => {
log.info(`exit code ${code}`, { component });
});
process.on('SIGINT', () => {
log.info('SIGINT', { component });
process.exit(0);
});
process.on('SIGTERM', () => {
log.info('SIGTERM', { component });
process.exit(0);
});
process.on('unhandledRejection', (error) => {
log.error(`unhandledRejection: ${error.message}`, { component }, error);
process.exit(1);
});
process.on('uncaughtException', (error) => {
log.error(`uncaughtException: ${error.message}`, { component }, error);
process.exit(1);
});
}
}
module.exports = new Bootstrap();