Handle shutdown on grid adapters (#5943)

* Handle shutdown on grid adapters

* Add tests

* Fix postgres test
This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-08-19 00:35:06 -07:00
committed by Diamond Lewis
parent f5ac94ddb2
commit c951e08f63
6 changed files with 81 additions and 21 deletions

View File

@@ -32,7 +32,10 @@ export class GridFSBucketAdapter extends FilesAdapter {
this._connectionPromise = MongoClient.connect(
this._databaseURI,
this._mongoOptions
).then(client => client.db(client.s.options.dbName));
).then(client => {
this._client = client;
return client.db(client.s.options.dbName);
});
}
return this._connectionPromise;
}
@@ -98,6 +101,13 @@ export class GridFSBucketAdapter extends FilesAdapter {
const bucket = await this._getBucket();
return bucket.openDownloadStreamByName(filename);
}
handleShutdown() {
if (!this._client) {
return Promise.resolve();
}
return this._client.close(false);
}
}
export default GridFSBucketAdapter;

View File

@@ -33,7 +33,10 @@ export class GridStoreAdapter extends FilesAdapter {
this._connectionPromise = MongoClient.connect(
this._databaseURI,
this._mongoOptions
).then(client => client.db(client.s.options.dbName));
).then(client => {
this._client = client;
return client.db(client.s.options.dbName);
});
}
return this._connectionPromise;
}
@@ -99,6 +102,13 @@ export class GridStoreAdapter extends FilesAdapter {
});
});
}
handleShutdown() {
if (!this._client) {
return Promise.resolve();
}
return this._client.close(false);
}
}
export default GridStoreAdapter;

View File

@@ -114,18 +114,22 @@ class ParseServer {
}
handleShutdown() {
const { adapter } = this.config.databaseController;
if (adapter && typeof adapter.handleShutdown === 'function') {
const promise = adapter.handleShutdown();
if (promise instanceof Promise) {
return promise.then(() => {
if (this.config.serverCloseComplete) {
this.config.serverCloseComplete();
}
});
}
const promises = [];
const { adapter: databaseAdapter } = this.config.databaseController;
if (
databaseAdapter &&
typeof databaseAdapter.handleShutdown === 'function'
) {
promises.push(databaseAdapter.handleShutdown());
}
return Promise.resolve().then(() => {
const { adapter: fileAdapter } = this.config.filesController;
if (fileAdapter && typeof fileAdapter.handleShutdown === 'function') {
promises.push(fileAdapter.handleShutdown());
}
return (promises.length > 0
? Promise.all(promises)
: Promise.resolve()
).then(() => {
if (this.config.serverCloseComplete) {
this.config.serverCloseComplete();
}