我在写一个简单的Youtube上传器。用户指定一个URL,服务器将带有该ID的记录上传到服务器。写入一个上传函数,如果进程死亡将会恢复
的代码是这样的:
app.post('/video/publish/:vid', function (req, res, next) {
// Check that the template is valid
stores.videos.dbLayer.selectById(req.params.vid, function(err, record){
if(err){
console.log("Ah!", err);
return next(err);
}
if(record === null){
err = new e.NotFoundError();
err.message = "Video lookup failed";
return next(err);
}
// ...Lots of async code to make Youtube upload happening...
res.status(200).send({ status: "uploaded" });
});
});
麻烦的是,无论什么原因,服务器宕机,目前所有的上传请求在水中死了(它们只是停止)。
所以,我的想法是,对于每个上传请求,我所做的只是向表中添加一条记录,并且每隔(例如)5分钟唤醒另一个函数,这将通过所有待处理的请求并启动它们。上传过程中,它只是在不同的表上引发一个标志。所以,这应该是这样的:
app.post('/video/publish/:vid', function (req, res, next) {
// Check that the template is valid
stores.videos.dbLayer.selectById(req.params.vid, function(err, record){
if(err){
console.log("Ah!", err);
return next(err);
}
if(record === null){
err = new e.NotFoundError();
err.message = "Video lookup failed";
return next(err);
}
// ... add the request to a queue ...
res.status(200).send({ status: "initiated" });
});
});
问题:
- 这是在节点的通用模式? (我似乎使用它很多)
- 任何陷阱我需要小心?
- 如果您的流程死亡时该怎么办?
你可以排队请求 –
看看'Automattic的/ kue'。 –