并发的大型删除操作在 NodeJS 中挂起

Concurrent large delete operations hang in NodeJS

我在 Centos 7.2 服务器上的 NodeJS 应用程序 运行 中有一个工作区清理功能,它获取目录路径列表并将其删除。当函数接收到路径列表时,它使用 Promise.all() 通过以下方式同时执行这些删除:

/**
 * Deletes directories.
 *
 * @param   {Array} directories Array of directories to be deleted.
 *
 * @returns {Object} Response object with the status code and data.
 */
const cleanup = async (directories) => {
  if (!Array.isArray(directories)) {
    return await responseStatementHandler(
      400,
      `Provide a list of directories as an array - [dir1, dir2].`,
      null,
      console.error
    );
  }

  if (!directories.length > 0) {
    return await responseStatementHandler(
      400,
      `Directory list cannot be empty.`,
      null,
      console.error
    );
  }

  try {
    const promisesList = await Promise.all(
      directories.map((d) => deleteDirectory(d))
    );
    return await responseStatementHandler(
      207,
      await promisesList,
      null,
      console.log
    );
  } catch (err) {
    return await responseStatementHandler(
      500,
      `Directory cleanup failed.`,
      err,
      console.error
    );
  }
};

/**
 * Performs a force delete on a provided directory.
 *
 * @param   {String} directory  Path of the directory to be deleted.
 *
 * @returns {Object} Response object with the status code and data.
 */
const deleteDirectory = async (directory) => {
  console.log(`Deleting directory: ${directory}`);
  try {
    if ((await fs.stat(directory)).isDirectory()) {
      await fs.rm(directory, { recursive: true, force: true });
      return await generateIndividualResponseObj(
        directory,
        200,
        `Successfully deleted directory: ${directory}`,
        null,
        console.log
      );
    }
  } catch (err) {
    if (err.message.includes("no such file or directory")) {
      return await generateIndividualResponseObj(
        directory,
        404,
        `Could not find directory: ${directory}`,
        null,
        console.error
      );
    }
    return await generateIndividualResponseObj(
      directory,
      500,
      `Failed to delete directory: ${directory}`,
      err,
      console.error
    );
  }
};

这里的问题是目录很大;大小约 1G。所以当有多个这样的删除时(>10 所以>10G),操作就挂了。

我知道这是因为只要我手动删除目录,应用程序就可以正常运行。

这是 fs 模块的限制还是逻辑编写方式的限制?在这种情况下,删除操作超时会有帮助吗?如果是这样,我将如何使用 fs 模块实现这样的超时?

这里的问题与 Node.js 无关。服务器的硬盘驱动器速度很慢,执行大量删除操作需要很长时间。应用程序只是在等待删除过程完成,并没有挂起。使用更快的存储连接服务器解决了这个问题。