我有一个在 NodeJS 中启动一些后台作业的模块(这些作业是 HTTP 请求),这是它的简化版本:
var
util = require('util'),
EE = require('events').EventEmitter,
Winston = require('winston');
var logger = new Winston.Logger({transports: [new Winston.transports.Console()]});
function Bot() {
EE.call(this);
this.model = ['job1','job2','job3'];
// null = never had a job, false = job started, true = job finished
this.jobs = {
'job1': null,
'job2': null,
'job3': null };
var mirror = this;
function start_job(job) {
var t = Math.floor(Math.random() * 10 + 1) * 1000;
logger.info('calling ' + job + ' for t=' + t);
setTimeout(function() {
logger.info('finished ' + job);
mirror.jobs[job] = true;
}, t);
}
this.on('start', function() {
logger.info('Starting');
while (mirror.isRunning()) {
for (var i=0; i<mirror.model.length; i++) {
var job = mirror.model[i];
var oldJob = mirror.jobs[job];
if (oldJob === null || oldJob === true) {
mirror.jobs[job] = false;
start_job(job);
}
}
// mirror.shutdown();
}
logger.info('Shutting down');
});
}
util.inherits(Bot, EE);
Bot.prototype.shutdown = function() {
this.running = false;
};
Bot.prototype.start = function() {
this.running = true;
this.emit('start');
};
Bot.prototype.isRunning = function() {
return this.running;
};
var bot = new Bot();
bot.start();
问题是这些作业从未真正运行过。start_job()
被调用,但中的匿名函数setTimeout()
永远不会触发。这是输出:
info: Starting
info: calling job1 for t=6000
info: calling job2 for t=5000
info: calling job3 for t=9000
脚本此时挂起,陷入无限循环。
现在,如果我在第一次循环迭代后停止,通过取消注释mirror.shutdown()
,作业按预期开始:
info: Starting
info: calling job1 for t=6000
info: calling job2 for t=9000
info: calling job3 for t=1000
info: Shutting down
info: finished job3
info: finished job1
info: finished job2
但这不好。我不知道为什么会发生这种情况,但我是 Node 新手,仍然在为一些概念而苦苦挣扎。
我知道一个名为Background.js的模块,但我不知道这是否适用于此处或稳定性如何(似乎不是一个活跃的项目)。我可以尝试一下,但在继续之前我仍然想了解这个问题。