简体   繁体   English

我怎么知道 nestjs/bull 实际上连接到 redis?

[英]How do I know nestjs/bull is actually connected to redis?

I am using nestjs with bull queues.我正在使用带有公牛队列的 nestjs。 I am using @nestjs/bull package and everything works fine.我正在使用@nestjs/bull package,一切正常。 But if for some reason, my application is not connected to redis, I don't get any error.但是如果由于某种原因,我的应用程序没有连接到 redis,我不会收到任何错误。 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.即使 promise 也没有得到解决,它只是挂起。 But if my redis application is connected properly, then everything works.但如果我的 redis 应用程序连接正确,那么一切正常。 Now, I want to see whether the redis server is successfully connected to my application or not every time I start my server.现在,我想在每次启动服务器时查看 redis 服务器是否成功连接到我的应用程序。

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.谢谢。

It is possible to get access to the Redis client over the Queue instance可以通过 Queue 实例访问 Redis 客户端

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";通过这种方式,您可以访问 Redis 客户端实例,该实例的status属性type RedisStatus = "wait" | "reconnecting" | "connecting" | "connect" | "ready" | "close" | "end"; 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.您可以通过以下服务检查启动时Redis是否可用。 Note:笔记:

  • the service needs to be called in the AppModule该服务需要在AppModule中调用

  • the service is rudimentary where we use a delay function to wait one second before checking the RedisState .该服务是基本的,我们在检查RedisState之前使用delay function 等待一秒钟。 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 ->所以当你启动你的应用程序时,你会得到 -> 在此处输入图像描述

Or ->或者 -> 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM