Handle shutdown on grid adapters (#5943)
* Handle shutdown on grid adapters * Add tests * Fix postgres test
This commit is contained in:
committed by
Diamond Lewis
parent
f5ac94ddb2
commit
c951e08f63
@@ -60,4 +60,19 @@ describe('GridFSBucket and GridStore interop', () => {
|
|||||||
await gfsAdapter.deleteFile('myFileName');
|
await gfsAdapter.deleteFile('myFileName');
|
||||||
await expectMissingFile(gfsAdapter, 'myFileName');
|
await expectMissingFile(gfsAdapter, 'myFileName');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('handleShutdown, close connection', done => {
|
||||||
|
const databaseURI = 'mongodb://localhost:27017/parse';
|
||||||
|
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
|
||||||
|
|
||||||
|
gfsAdapter._connect().then(db => {
|
||||||
|
expect(db.serverConfig.connections().length > 0).toEqual(true);
|
||||||
|
expect(db.serverConfig.s.connected).toEqual(true);
|
||||||
|
gfsAdapter.handleShutdown().then(() => {
|
||||||
|
expect(db.serverConfig.connections().length > 0).toEqual(false);
|
||||||
|
expect(db.serverConfig.s.connected).toEqual(false);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -96,4 +96,19 @@ describe_only_db('mongo')('GridStoreAdapter', () => {
|
|||||||
})
|
})
|
||||||
.catch(fail);
|
.catch(fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('handleShutdown, close connection', done => {
|
||||||
|
const databaseURI = 'mongodb://localhost:27017/parse';
|
||||||
|
const gridStoreAdapter = new GridStoreAdapter(databaseURI);
|
||||||
|
|
||||||
|
gridStoreAdapter._connect().then(db => {
|
||||||
|
expect(db.serverConfig.connections().length > 0).toEqual(true);
|
||||||
|
expect(db.serverConfig.s.connected).toEqual(true);
|
||||||
|
gridStoreAdapter.handleShutdown().then(() => {
|
||||||
|
expect(db.serverConfig.connections().length > 0).toEqual(false);
|
||||||
|
expect(db.serverConfig.s.connected).toEqual(false);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -66,14 +66,20 @@ describe('Server Url Checks', () => {
|
|||||||
const newConfiguration = Object.assign({}, defaultConfiguration, {
|
const newConfiguration = Object.assign({}, defaultConfiguration, {
|
||||||
databaseAdapter,
|
databaseAdapter,
|
||||||
serverStartComplete: () => {
|
serverStartComplete: () => {
|
||||||
parseServer.handleShutdown();
|
let promise = Promise.resolve();
|
||||||
parseServer.server.close(err => {
|
if (process.env.PARSE_SERVER_TEST_DB !== 'postgres') {
|
||||||
if (err) {
|
promise = parseServer.config.filesController.adapter._connect();
|
||||||
done.fail('Close Server Error');
|
}
|
||||||
}
|
promise.then(() => {
|
||||||
reconfigureServer({}).then(() => {
|
parseServer.handleShutdown();
|
||||||
expect(close).toBe(true);
|
parseServer.server.close(err => {
|
||||||
done();
|
if (err) {
|
||||||
|
done.fail('Close Server Error');
|
||||||
|
}
|
||||||
|
reconfigureServer({}).then(() => {
|
||||||
|
expect(close).toBe(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -32,7 +32,10 @@ export class GridFSBucketAdapter extends FilesAdapter {
|
|||||||
this._connectionPromise = MongoClient.connect(
|
this._connectionPromise = MongoClient.connect(
|
||||||
this._databaseURI,
|
this._databaseURI,
|
||||||
this._mongoOptions
|
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;
|
return this._connectionPromise;
|
||||||
}
|
}
|
||||||
@@ -98,6 +101,13 @@ export class GridFSBucketAdapter extends FilesAdapter {
|
|||||||
const bucket = await this._getBucket();
|
const bucket = await this._getBucket();
|
||||||
return bucket.openDownloadStreamByName(filename);
|
return bucket.openDownloadStreamByName(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleShutdown() {
|
||||||
|
if (!this._client) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
return this._client.close(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default GridFSBucketAdapter;
|
export default GridFSBucketAdapter;
|
||||||
|
|||||||
@@ -33,7 +33,10 @@ export class GridStoreAdapter extends FilesAdapter {
|
|||||||
this._connectionPromise = MongoClient.connect(
|
this._connectionPromise = MongoClient.connect(
|
||||||
this._databaseURI,
|
this._databaseURI,
|
||||||
this._mongoOptions
|
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;
|
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;
|
export default GridStoreAdapter;
|
||||||
|
|||||||
@@ -114,18 +114,22 @@ class ParseServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleShutdown() {
|
handleShutdown() {
|
||||||
const { adapter } = this.config.databaseController;
|
const promises = [];
|
||||||
if (adapter && typeof adapter.handleShutdown === 'function') {
|
const { adapter: databaseAdapter } = this.config.databaseController;
|
||||||
const promise = adapter.handleShutdown();
|
if (
|
||||||
if (promise instanceof Promise) {
|
databaseAdapter &&
|
||||||
return promise.then(() => {
|
typeof databaseAdapter.handleShutdown === 'function'
|
||||||
if (this.config.serverCloseComplete) {
|
) {
|
||||||
this.config.serverCloseComplete();
|
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) {
|
if (this.config.serverCloseComplete) {
|
||||||
this.config.serverCloseComplete();
|
this.config.serverCloseComplete();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user