简体   繁体   English

为什么我的 typescript 程序随机停止运行?

[英]Why does my typescript program randomly stop running?

I wrote a very simple typescript program, which does the following:我写了一个非常简单的 typescript 程序,它执行以下操作:

  1. Transform users.csv into an arrayusers.csv转换为数组
  2. For each element/user issue an API call to create that user on a 3rd party platform对于每个元素/用户发出 API 调用以在第 3 方平台上创建该用户
  3. Print any errors打印任何错误

The excel file has >160,000 rows and there is no way to create them all in one API call, so I wrote this program to run in the background of my computer for ~>20 hours. excel 文件有 >160,000 行,并且无法在一次 API 调用中创建它们,所以我编写了这个程序以在我的计算机后台运行 ~>20 小时。

The first time I ran this, the code stopped mid for loop without an exception or anything.我第一次运行它时,代码在for循环中停止,没有任何异常或任何东西。 So, I deleted the user rows from the csv file that were already uploaded and re-ran the code.因此,我从 csv 文件中删除了已经上传的用户行并重新运行代码。 Unfortunately, this kept happening .不幸的是,这种情况一直在发生

Interestingly, the code has stopped at non-deterministic iterations, one time it was at i=812 , another at i=27650 , and so on.有趣的是,代码在非确定性迭代中停止,一次在i=812 ,另一次在i=27650 ,依此类推。

This is the code:这是代码:

const main = async () => {
  const usersFile = await fsPromises.readFile("./users.csv", { encoding: "utf-8" });
  const usersArr = makeArray(usersFile);

  for (let i = 0; i < usersArr.length; i++) {
    const [ userId, email ] = usersArr[i];
    console.log(`uploading ${userId}. ${i}/${usersArr.length}`);
    try {
      await axios.post(/* create user */);
      await sleep(150);
    } catch (err) {
      console.error(`Error uploading ${userId} -`, err.message);
    }
  }
};

main();

I should mention that exceptions are within the for-loop because many rows will fail to upload with a 400 error code.我应该提到异常在 for 循环中,因为许多行将无法上传,并显示400错误代码。 As such, I've preferred to have the code run non-stop and print any errors onto a file, so that I could later re-run it for the users that failed to upload.因此,我更喜欢让代码不间断地运行并将任何错误打印到文件中,以便我以后可以为上传失败的用户重新运行它。 Otherwise I would have to check whether it halted because of an error every 10 minutes.否则,我必须每 10 分钟检查一次它是否因为错误而停止。

Why does this happen?为什么会这样? and What can I do?我能做什么?

I run after compiling as: node build/index.js 2>>errors.txt我编译后运行为: node build/index.js 2>>errors.txt

EDIT: There is no code after main() and no code outside the try... catch block within the loop.编辑: main()之后没有代码,循环内的try... catch块之外没有代码。 errors.txt only contains 400 errors. errors.txt仅包含400错误。 Even if it contained another run-time exception, it seems to me that this wouldn't/shouldn't halt execution, because it would execute catch and move on to the next iteration.即使它包含另一个运行时异常,在我看来这不会/不应该停止执行,因为它会执行catch并继续下一次迭代。

I think this may have been related to this post.我认为这可能与这篇文章有关。 The file I was reading was extremely large as noted, and it was saved into a runtime variable.如前所述,我正在阅读的文件非常大,它被保存到运行时变量中。 Undeterministically, the OS could have decided that the memory demanded was too high.不确定的是,操作系统可能会认为 memory 要求太高。 This is probably a situation to use a Readable Stream instead of a readFile .这可能是使用Readable Stream而不是readFile的情况。

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

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