@mojaloop/database-lib 11.1.3 → 11.1.4-snapshot.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/database.js +21 -0
- package/test/unit/database.test.js +51 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mojaloop/database-lib",
|
|
3
|
-
"version": "11.1.
|
|
3
|
+
"version": "11.1.4-snapshot.1",
|
|
4
4
|
"description": "Shared database code for central services",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"release": "standard-version --releaseCommitMessageFormat 'chore(release): {{currentTag}} [skip ci]'"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@mojaloop/central-services-error-handling": "13.0.
|
|
46
|
+
"@mojaloop/central-services-error-handling": "13.0.7",
|
|
47
47
|
"knex": "3.1.0",
|
|
48
48
|
"lodash": "4.17.21",
|
|
49
49
|
"mysql": "2.18.1"
|
|
@@ -70,9 +70,9 @@
|
|
|
70
70
|
"yargs-parser": "21.1.1"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
|
-
"@mojaloop/sdk-standard-components": "
|
|
73
|
+
"@mojaloop/sdk-standard-components": "19.10.3",
|
|
74
74
|
"audit-ci": "^7.1.0",
|
|
75
|
-
"npm-check-updates": "17.1.
|
|
75
|
+
"npm-check-updates": "17.1.15",
|
|
76
76
|
"nyc": "17.1.0",
|
|
77
77
|
"pre-commit": "1.2.2",
|
|
78
78
|
"proxyquire": "2.1.3",
|
package/src/database.js
CHANGED
|
@@ -29,6 +29,7 @@ class Database {
|
|
|
29
29
|
this._knex = null
|
|
30
30
|
this._tables = []
|
|
31
31
|
this._schema = null
|
|
32
|
+
this._signalHandlersRegistered = false // Track if signal handlers are already registered
|
|
32
33
|
|
|
33
34
|
this._listTableQueries = {
|
|
34
35
|
mysql: (knex) => {
|
|
@@ -78,14 +79,27 @@ class Database {
|
|
|
78
79
|
this._knex = await configureKnex(config)
|
|
79
80
|
this._tables = await this._listTables()
|
|
80
81
|
await this._setTableProperties()
|
|
82
|
+
|
|
83
|
+
// Register signal handlers only once
|
|
84
|
+
if (!this._signalHandlersRegistered) {
|
|
85
|
+
process.on('SIGINT', async () => {
|
|
86
|
+
await this._handleShutdown('SIGINT')
|
|
87
|
+
})
|
|
88
|
+
process.on('SIGTERM', async () => {
|
|
89
|
+
await this._handleShutdown('SIGTERM')
|
|
90
|
+
})
|
|
91
|
+
this._signalHandlersRegistered = true
|
|
92
|
+
}
|
|
81
93
|
}
|
|
82
94
|
|
|
83
95
|
async disconnect () {
|
|
84
96
|
if (this._knex) {
|
|
97
|
+
console.log('Closing database connection...')
|
|
85
98
|
this._removeTableProperties()
|
|
86
99
|
this._tables = []
|
|
87
100
|
await this._knex.destroy()
|
|
88
101
|
this._knex = null
|
|
102
|
+
console.log('Database connection closed.')
|
|
89
103
|
}
|
|
90
104
|
}
|
|
91
105
|
|
|
@@ -135,6 +149,13 @@ class Database {
|
|
|
135
149
|
}
|
|
136
150
|
return this._knex.client.pool.numPendingAcquires()
|
|
137
151
|
}
|
|
152
|
+
|
|
153
|
+
async _handleShutdown (signal) {
|
|
154
|
+
console.log(`Received ${signal}, shutting down database...`)
|
|
155
|
+
await this.disconnect()
|
|
156
|
+
console.log('Cleanup complete. Exiting process.')
|
|
157
|
+
process.exit(0)
|
|
158
|
+
}
|
|
138
159
|
}
|
|
139
160
|
|
|
140
161
|
const configureKnex = async ({ maxPendingAcquire, ...config }) => {
|
|
@@ -382,5 +382,56 @@ Test('database', databaseTest => {
|
|
|
382
382
|
getPendingAcquiresTest.end()
|
|
383
383
|
})
|
|
384
384
|
|
|
385
|
+
databaseTest.test('connect should', connectTest => {
|
|
386
|
+
connectTest.test('register signal handlers only once', async test => {
|
|
387
|
+
const processOnStub = sandbox.stub(process, 'on')
|
|
388
|
+
|
|
389
|
+
await dbInstance.connect(connectionConfig)
|
|
390
|
+
await dbInstance.connect(connectionConfig)
|
|
391
|
+
|
|
392
|
+
test.ok(processOnStub.calledTwice)
|
|
393
|
+
test.ok(processOnStub.calledWith('SIGINT'))
|
|
394
|
+
test.ok(processOnStub.calledWith('SIGTERM'))
|
|
395
|
+
test.equal(dbInstance._signalHandlersRegistered, true)
|
|
396
|
+
|
|
397
|
+
processOnStub.restore()
|
|
398
|
+
test.end()
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
connectTest.test('handle SIGINT signal', async test => {
|
|
402
|
+
const processOnStub = sandbox.stub(process, 'on')
|
|
403
|
+
const handleShutdownStub = sandbox.stub(dbInstance, '_handleShutdown')
|
|
404
|
+
|
|
405
|
+
await dbInstance.connect(connectionConfig)
|
|
406
|
+
const sigintHandler = processOnStub.firstCall.args[1]
|
|
407
|
+
|
|
408
|
+
await sigintHandler()
|
|
409
|
+
|
|
410
|
+
test.ok(handleShutdownStub.calledOnceWith('SIGINT'))
|
|
411
|
+
|
|
412
|
+
processOnStub.restore()
|
|
413
|
+
handleShutdownStub.restore()
|
|
414
|
+
test.end()
|
|
415
|
+
})
|
|
416
|
+
|
|
417
|
+
connectTest.test('handle SIGTERM signal', async test => {
|
|
418
|
+
const processOnStub = sandbox.stub(process, 'on')
|
|
419
|
+
const handleShutdownStub = sandbox.stub(dbInstance, '_handleShutdown')
|
|
420
|
+
|
|
421
|
+
await dbInstance.connect(connectionConfig)
|
|
422
|
+
const sigtermHandler = processOnStub.secondCall.args[1]
|
|
423
|
+
|
|
424
|
+
await sigtermHandler()
|
|
425
|
+
|
|
426
|
+
test.ok(handleShutdownStub.calledOnceWith('SIGTERM'))
|
|
427
|
+
|
|
428
|
+
processOnStub.restore()
|
|
429
|
+
handleShutdownStub.restore()
|
|
430
|
+
test.end()
|
|
431
|
+
})
|
|
432
|
+
|
|
433
|
+
connectTest.end()
|
|
434
|
+
})
|
|
435
|
+
|
|
385
436
|
databaseTest.end()
|
|
386
437
|
})
|