Skip to content

Commit 5979ac8

Browse files
author
Dan McGhan
committed
Added files for part 2 - database basics
1 parent 19237fa commit 5979ac8

File tree

7 files changed

+604
-0
lines changed

7 files changed

+604
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
hrPool: {
3+
user: process.env.HR_USER,
4+
password: process.env.HR_PASSWORD,
5+
connectString: process.env.HR_CONNECTIONSTRING,
6+
poolMin: 10,
7+
poolMax: 10,
8+
poolIncrement: 0
9+
}
10+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
port: process.env.HTTP_PORT || 3000
3+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const webServer = require('./services/web-server.js');
2+
const database = require('./services/database.js');
3+
const dbConfig = require('./config/database.js');
4+
const defaultThreadPoolSize = 4;
5+
6+
// Increase thread pool size by poolMax
7+
process.env.UV_THREADPOOL_SIZE = dbConfig.hrPool.poolMax + defaultThreadPoolSize;
8+
9+
async function startup() {
10+
console.log('Starting application');
11+
12+
try {
13+
console.log('Initializing database module');
14+
15+
await database.initialize();
16+
} catch (err) {
17+
console.error(err);
18+
19+
process.exit(1); // Non-zero failure code
20+
}
21+
22+
try {
23+
console.log('Starting web server');
24+
25+
await webServer.start();
26+
} catch (err) {
27+
console.error(err);
28+
29+
process.exit(1); // Non-zero failure code
30+
}
31+
}
32+
33+
startup();
34+
35+
async function shutdown(e) {
36+
let err = e;
37+
38+
console.log('Shutting down application');
39+
40+
try {
41+
console.log('Closing web server');
42+
43+
await webServer.stop();
44+
} catch (e) {
45+
console.error(e);
46+
47+
err = err || e;
48+
}
49+
50+
try {
51+
console.log('Closing database module');
52+
53+
await database.close();
54+
} catch (e) {
55+
console.error(e);
56+
57+
err = err || e;
58+
}
59+
60+
console.log('Exiting process');
61+
62+
if (err) {
63+
process.exit(1);
64+
} else {
65+
process.exit(0);
66+
}
67+
}
68+
69+
process.on('SIGTERM', () => {
70+
console.log('Received SIGTERM');
71+
72+
shutdown();
73+
});
74+
75+
process.on('SIGINT', () => {
76+
console.log('Received SIGINT');
77+
78+
shutdown();
79+
});
80+
81+
process.on('uncaughtException', err => {
82+
console.log('Uncaught exception', err);
83+
84+
shutdown(err);
85+
});

0 commit comments

Comments
 (0)