From e7a45cecb7493130370d8128c359d805137d2d1d Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Tue, 11 Apr 2017 12:26:25 -0400 Subject: [PATCH] Adds graceful shutdown handler for mongodb (#3706) --- src/Adapters/Storage/Mongo/MongoStorageAdapter.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js index 6f1fc59f..0d185ed5 100644 --- a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js +++ b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js @@ -97,6 +97,17 @@ export class MongoStorageAdapter { // MaxTimeMS is not a global MongoDB client option, it is applied per operation. this._maxTimeMS = mongoOptions.maxTimeMS; + process.on('SIGTERM', this.handleShutdown(this)); + process.on('SIGINT', this.handleShutdown(this)); + } + + handleShutdown(storageAdapter) { + return () => { + if (!storageAdapter.database) { + return; + } + storageAdapter.database.close(false); + } } connect() {