Skip to content

Commit

Permalink
Merge pull request #19 from CartoDB/drain
Browse files Browse the repository at this point in the history
Fix node fails to exit
  • Loading branch information
simon-contreras-deel authored Aug 20, 2020
2 parents 8476670 + e0a3067 commit 9417078
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.8.1 (2020-08-20)

### Added
- New `destroy` method in order to drain all pools and clear internal interval

# 0.8.0 (2020-05-13)

### Breaking changes
Expand Down
34 changes: 22 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ module.exports = class RedisPool extends EventEmitter {
this.logger = logger;

this._addCommands();
this._emitStatus();
this._statusInterval = this._emitStatus();
}

/**
* Acquire Redis client
*
* @param {String|Number} database redis database name
* @returns {Promise} with the Redis client
*/
* Acquire Redis client
*
* @param {String|Number} database redis database name
* @returns {Promise} with the Redis client
*/
async acquire (database) {
let pool = this.pools[database];
if (!pool) {
Expand Down Expand Up @@ -80,11 +80,11 @@ module.exports = class RedisPool extends EventEmitter {
}

/**
* Release resource.
*
* @param {String|Number} database redis database name
* @param {Object} resource resource object to release
*/
* Release resource.
*
* @param {String|Number} database redis database name
* @param {Object} resource resource object to release
*/
async release (database, resource) {
if (this.options.unwatchOnRelease) {
resource.UNWATCH();
Expand All @@ -97,14 +97,24 @@ module.exports = class RedisPool extends EventEmitter {
}
}

/**
* Closing all connection pools
*/
async destroy () {
clearInterval(this._statusInterval)

// https://github.com/coopernurse/node-pool/blob/v3.7.1/README.md#draining
return await Promise.all(Object.values(this.pools).map(p => p.drain().then(() => p.clear())) )
}

_addCommands () {
if (this.options.commands.length) {
this.options.commands.forEach(newCommand => redis.add_command(newCommand));
}
}

_emitStatus () {
setInterval(() => {
return setInterval(() => {
for (const [poolKey, pool] of Object.entries(this.pools)) {
this.emit('status', {
name: this.options.name,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redis-mpool",
"version": "0.8.0",
"version": "0.8.1",
"main": "./index.js",
"description": "Provides db pools for redis",
"url": "https://github.com/cartodb/node-redis-mpool",
Expand Down

0 comments on commit 9417078

Please sign in to comment.