Skip to content

Commit 8718a6e

Browse files
committed
feat: added Client.shutdown function for MySQL
1 parent a4895f2 commit 8718a6e

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

database/mysql/Client.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = (() => {
5151
*/
5252
async query(query, parameters, name) {
5353
if (this.getIsDisposed()) {
54-
return Promise.reject(`Unable to execute MySQL query, the ${this.toString()} has been disposed`);
54+
throw new Error(`Unable to execute query, the ${this.toString()} has been disposed`);
5555
}
5656

5757
assert.argumentIsRequired(query, 'query', String);
@@ -78,6 +78,68 @@ module.exports = (() => {
7878
});
7979
}
8080

81+
/**
82+
* Finalizes instance operations and disposes instance. If the graceful parameter is true, any outstanding
83+
* queries will be completed.
84+
*
85+
* @public
86+
* @async
87+
* @param {Boolean} graceful
88+
* @returns {Promise<void>}
89+
*/
90+
async shutdown(graceful) {
91+
if (this.getIsDisposed()) {
92+
throw new Error(`Unable to shutdown, the [ ${this.toString()} ] has been disposed`);
93+
}
94+
95+
if (this._connection === null) {
96+
throw new Error(`Unable to shutdown, the [ ${this.toString()} ] has been shutdown`);
97+
}
98+
99+
assert.argumentIsRequired(graceful, 'graceful', Boolean);
100+
101+
const connection = this._connection;
102+
this._connection = null;
103+
104+
this.dispose();
105+
106+
let shutdownPromise;
107+
108+
if (graceful) {
109+
shutdownPromise = new Promise((resolve, reject) => {
110+
connection.end((error) => {
111+
if (error) {
112+
reject(error);
113+
}
114+
115+
logger.info(`Shutdown [ ${this.toString()} ] [ ${this.id} ] gracefully`);
116+
117+
resolve();
118+
});
119+
});
120+
} else {
121+
shutdownPromise = new Promise((resolve) => {
122+
connection.destroy();
123+
124+
logger.info(`Shutdown [ ${this.toString()} ] [ ${this.id} ] immediately`);
125+
126+
resolve();
127+
});
128+
}
129+
130+
return shutdownPromise;
131+
}
132+
133+
_onDispose() {
134+
if (this._connection !== null) {
135+
this._connection.destroy();
136+
137+
this._connection = null;
138+
}
139+
140+
logger.info(`Disposed [ ${this.toString()} ] [ ${this.id} ]`);
141+
}
142+
81143
toString() {
82144
return '[Client]';
83145
}

database/mysql/DirectClientProvider.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,7 @@ module.exports = (() => {
6363

6464
class DirectClient extends Client {
6565
constructor(connection) {
66-
super(connection, {});
67-
}
68-
69-
_onDispose() {
70-
this._connection.end();
71-
this._connection = null;
72-
73-
logger.info('Disposed [DirectClient] [', this.id, ']');
66+
super(connection);
7467
}
7568

7669
toString() {

0 commit comments

Comments
 (0)