为什么下面的代码在运行全部3行之后不会导致节点退出进程?Node.js脚本不退出
var monq = require('monq')
var client = monq('mongodb://remote.db.com:27017/mydB')
var queue = client.queue('users')
如果未执行第3行,脚本会退出。
为什么下面的代码在运行全部3行之后不会导致节点退出进程?Node.js脚本不退出
var monq = require('monq')
var client = monq('mongodb://remote.db.com:27017/mydB')
var queue = client.queue('users')
如果未执行第3行,脚本会退出。
看起来到Mongo的底层连接在client.queue('users')
被调用之前并没有建立。 monq有一个client.close()
方法关闭它的数据库连接。有几个MOD可以测试这个。
var monq = require('monq')
var client = monq('mongodb://localhost:27017/mydB')
var queue;
setTimeout(function() {
// create the queue which establishes the connections
queue = client.queue('users');
setTimeout(function() {
// close connection
client.close();
}, 5000)
}, 5000);
必须等到异步操作(处理队列)将完成,然后,打电话process.exit()
结束脚本的执行。请参阅:https://nodejs.org/api/process.html#process_process_exit_code
看起来您已将作业添加到队列中,并且继续等待被告知执行作业。 – michaelAdam