如何等待应用程序关闭,然后使用WebPack热模块重新加载新实例



我正在使用webpack热模块重新加载(HMR)作为我的nest.js应用程序。重新加载起作用,但在启动新实例之前,不等待旧实例完全关闭(数据库连接,Telegram Bot,...)。这使得typeorm抛出以下错误:

AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session.

在我的 main.ts中,我有一个关闭旧实例的处置处理程序:

if (module && module.hot) {
  module.hot.accept();
  module.hot.dispose(async () => {
    console.log('disposing module');
    await app.close();
    console.log('has closed app');
  });
}

当我运行HMR并更改我的应用程序时,我可以看到它调用Dispose处理程序并立即启动新应用程序。如何使WebPack等待在启动新实例之前返回的承诺解决的承诺?

根据此问题,您可以将keepConnectionAlive设置为typeorm选项中的true,以重复使用该连接。

TypeOrmModule.forRoot({
  // ...
  keepConnectionAlive: true,
})

这确实解决了Typeorm误差,但是Telegram bot的问题仍然存在:

Error: 409: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running

我认为我可以重复使用bot实例,所以这只是一个部分解决方案。

最新更新