首页 > 解决方案 > Sequelize 查询随机挂起

问题描述

我正在调用一个使用 Sequelize 更新 Postgres 模型中的一些数据的方法。调用来自异步生成器函数,如下所示:

文件夹结构:

  1. 文件夹1
    • initialCallFile(生成器函数)
    • anotherFileThatCalls(非生成器函数)
  2. 文件夹2
    • 更新机器人方法文件

初次通话:

async function* function() {
    await storage.updateBot({id: player.id, handsPlayed: 1, isActive: true});
}

updateBot 方法:

function updateBot(data) {
  return new Promise(async resolve => {
    try {
      const bot = await Bot.findOne({
        where: {
          id: data.id,
        },
      });

      if (bot) {
        if (data.handsPlayed)
          bot.handsPlayed += data.handsPlayed;
        if (data.handsWon)
          bot.handsWon += data.handsWon;
        if (data.totalWinnings)
          bot.totalWinnings = data.totalWinnings;
        if (data.isActive !== null && data.isActive !== undefined)
          bot.isActive = data.isActive;
      } else {
        logger.log('debug', 'No Bot found with id: %s', data.id);
        resolve(true);
      }

      await bot.save();

      resolve(true);
    } catch (err) {
      logger.log('error', 'Error updating Bot with id: %s', data.id);
      resolve(true);
    }
  });
}

updateBot 方法在初始 Bot.fineOne 查询中挂起。没有抛出错误,即使在 5-10 分钟后,也没有超时。

我也在从其他地方调用 updateBot,它们不在生成器函数内,它们按预期工作而不会挂起。但是,我不认为这是导致问题的生成器函数,因为它是一个完全独立的函数,当我用简单的超时替换查询时,它不会挂起。

我也尝试过直接在我的生成器函数中调用更新以及使用 yield 而不是 await ,但仍然存在挂起。由于没有错误或超时,我不确定如何最好地调试问题。该代码在其他地方调用时按预期工作,所以我很困惑。

任何建议将不胜感激。

标签: javascriptnode.jsasync-awaitsequelize.jsgenerator

解决方案


推荐阅读