如何退出具有许多同步 Sequelize 调用的节点进程?
How to exit node process with many sychronous Sequelize calls?
我有一些代码如下所示。
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;
});
它 运行s,但永远不会退出。 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() 的调用被注释掉,挂起
我有一些代码如下所示。
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;
});
它 运行s,但永远不会退出。 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() 的调用被注释掉,挂起