为什么不在保存到数据库的node.js中有一个Job
对象。
var Job = {
id: long,
task: String,
configuration: JSON,
dueDate: Date,
finished: bit
};
我建议你只在RAM中存储该ID并将所有其他Job
数据留在数据库中。当你的超时功能最终运行时,只需要知道.id
就可以获得其他数据。
var job = createJob(...); // create from async data somewhere.
job.save(); // save the job.
var id = job.id // only store the id in RAM
// ask the job to be run in the future.
setTimeout(Date.now - job.dueDate, function() {
// load the job when you want to run it
db.load(id, function(job) {
// run it.
run(job);
// mark as finished
job.finished = true;
// save your finished = true state
job.save();
});
});
// remove job from RAM now.
job = null;
如果服务器崩溃过你所查询的是有[finished=false]
所有作业,它们加载到RAM中,然后再次启动一个定时器。
如果出现任何错误,你应该能够干净利落重启像这样:
db.find("job", { finished: false }, function(jobs) {
each(jobs, function(job) {
var id = job.id;
setTimeout(Date.now - job.dueDate, function() {
// load the job when you want to run it
db.load(id, function(job) {
// run it.
run(job);
// mark as finished
job.finished = true;
// save your finished = true state
job.save();
});
});
job = null;
});
});
很好的点。谢谢! – Josh 2011-04-10 03:39:50
我同意KISS说投票是好的。 OTOH,通知者/观察者没有任何投票。当有事情需要发生时,所有订户都会收到通知。唯一的网络流量是keepalive或heartbeats,可以在一些守护进程/系统/软件中尽可能少地发生。 – squarism 2012-03-13 18:58:49