2

I am using nestjs with bull queues. I am using @nestjs/bull package and everything works fine. But if for some reason, my application is not connected to redis, I don't get any error. The application runs properly, doesn't get any errors but doesn't process the queues. It just hangs. Even the promise doesn't get resolved, it just hangs. But if my redis application is connected properly, then everything works. Now, I want to see whether the redis server is successfully connected to my application or not every time I start my server.

How can I do that?

I have been searching for that for a while now but couldn't get anything from google. Any information or resource will be helpful. Thanks.

Pranta
  • 2,928
  • 7
  • 24
  • 37
  • You can check information about clients connected to your Redis server using [client-list](https://redis.io/commands/client-list/) command. – usuario Aug 25 '22 at 06:16
  • Actually I can see the data in redis insight. But I want my application to throw an error if redis is not connected. – Pranta Aug 25 '22 at 06:35

1 Answers1

4

It is possible to get access to the Redis client over the Queue instance

export class SomeService {
  constructor(@InjectQueue("some-queue") private someQueue: Queue) {}

  async getQueueStatus(): RedisStatus {
    return this.someQueue.client.status;
  }
}

In this way you can access Redis client instance which has the status property of type RedisStatus = "wait" | "reconnecting" | "connecting" | "connect" | "ready" | "close" | "end";

The queue we refer to needs to be imported into a module

BullModule.registerQueue({
  name: "some-queue",
})

You can check if Redis is available at startup by the service below. Note:

  • the service needs to be called in the AppModule
  • the service is rudimentary where we use a delay function to wait one second before checking the RedisState. The logic can be extended to fit your needs.
@Injectable()
export class RedisQueue {
  logger = new Logger("RedisQueue");

  constructor(@InjectQueue("some-queue") private someQueue: Queue) {
    this.init();
  }

  async init() {
    try {
      await this.delay(1000, 1);
      this.checkQueueAvailability();
    } catch (e) {
      this.logger.error(e);
    }
  }

  private checkQueueAvailability(): void {
    if (this.someQueue.client.status === "ready") {
      this.logger.log("Redis is ready");
    } else {
      throw new Error("Redis not available");
    }
  }

  delay(t: number, val: any) {
    return new Promise(function (resolve) {
      setTimeout(function () {
        resolve(val);
      }, t);
    });
  }
}

So when you start your app you will get -> enter image description here

Or -> enter image description here

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
Džan Operta
  • 399
  • 2
  • 11
  • Thanks a lot, that was really helpful, but is there a way to validate if redis is connected or not? – Pranta Nov 23 '22 at 05:44
  • 1
    You can create a singleton 'Redis' service that injects the Queue(s) and checks the RedisStatus on initialization or retries the check on a timeout. From the documentation, it seems that the RedisStatus is only available on the Queue level. – Džan Operta Nov 24 '22 at 08:23
  • 1
    could you please provide that line to the question (maybe with a little line of codes if you can) so that I can accept this answer. – Pranta Nov 24 '22 at 09:00