Tom Klino
Tom Klino

Reputation: 2504

How to close ioredis (nodejs module) when connected to a cluster?

I've written a module that uses ioredis and some tests to go along with it.

The problem is, that when I it when ioredis is connected in cluster mode, mocha hangs after finish, even though I call redis.disconnect().

It seems that something is still bound to the event loop.

Here is my test:

it('connects to redis cluster mode', async () => {
  //redisClientFactroy is my module
  let redisClientFactory = redisClientFactoryInit({
    host: 'localhost',
    port: 7000
  })
  // it returns a redis.cluster instance if it recognizes it
  // 'redis' here is a cluster client
  let redis = await redisClientFactory.createClient()
  await redis.set('took123', 1);
  let took = await redis.get('took123')
  expect(took).to.eql('1')
  let nodes = redis.nodes()
  await Promise.all(nodes.map((node) => {
    node.disconnect()
    console.log("node disconnected")
  }))
  redis.disconnect()

  //tests are marked as done and passing, but mocha does not exit
})

Upvotes: 3

Views: 13662

Answers (2)

Jeffrey Wang
Jeffrey Wang

Reputation: 1

I got the same problem, and it fixed by call redis.disconnect(), every client should be disconnected.

Upvotes: 0

Nick Bondarenko
Nick Bondarenko

Reputation: 6351

You do not need to use promises or close node connections by hands. Just call

redis.disconnect()

In cluster mode it will close all open connection themself. Look at disconnect method in sources.

If you still need to close in by hands, do not uses promises — disconnect do not return them. Just

nodes.map((node) => node.disconnect())

Upvotes: 4

Related Questions