gpt4 book ai didi

javascript - 如何通过许多同步 Sequelize 调用退出 Node 进程?

转载 作者:行者123 更新时间:2023-12-03 22:42:28 26 4
gpt4 key购买 nike

我有一些类似于下面的代码。

Post.findAll(){
.then(function (posts) {
var i;

for (i in posts) {

Comment.findAll({where: {post_id: posts[i].id}}){
.then(function (comments) {
// do some stuff.

return;
});
}

return;
});

它运行,但永远不会退出。 Comment 调用可以全部同步运行。通常我会按顺序运行,所以会有一系列 return 调用。但在这种情况下,这无关紧要。该过程只是挂起,永远不会退出。这是一个 cron 工作,所以我需要它在触发所有调用后退出。

有没有办法做到这一点?

最佳答案

我认为问题在于您没有调用 Sequelize::close 。因此连接保持打开状态(可能直到某个超时),因此该过程不会终止。

来自 lib/sequelize.js:

/**
* Close all connections used by this sequelize instance, and free all references so the instance can be garbage collected.
*
* Normally this is done on process exit, so you only need to call this method if you are creating multiple instances, and want
* to garbage collect some of them.
*/
Sequelize.prototype.close = function () {
this.connectionManager.close();
};

我有一些测试 here 证明了这一点:
  • tests/00-demo/run.sh - 正确终止
  • tests/01-demo-no-close/run.sh - 调用 close() 注释掉,挂起
  • 关于javascript - 如何通过许多同步 Sequelize 调用退出 Node 进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33565886/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com