我正在使用async.queue
以确保服务中的某些文件复制最多同时发生n
,但是有时观看文件复制时,我看到的内容比队列允许的更多。在下面的实现中,有没有人看到我可能错过的东西?
createQueue(limit: number) {
let self = this;
return async.queue(function(cmdObj, callback) {
console.log("Beginning copy");
let cmd = cmdObj.cmd;
let args = cmdObj.args;
let request = cmdObj.req;
request.state = State.IN_PROGRESS;
self.reportStatus(request.destination);
const proc = spawn(cmd, args); //uses an rsync command upstream
proc.on("close", code => {
if (code !== 0) {
request.state = State.ERRORED;
self.reportStatus(request.destination); // these just report to the caller
statusMap.delete(request.destination);
} else {
fs.rename(request.destination + ".part", request.destination);
request.state = State.COMPLETED;
self.reportStatus(request.destination); // same here
statusMap.delete(request.destination);
}
callback();
});
proc.on("error", err => {
console.error("COPY ERR: " + err);
});
}, limit); // limit here, for example, may be two, but I see four copies concurrently
}
编辑:我现在认为这是系统其余部分的副作用...队列被清除并在复制后开始重新初始化...因此当将新项目添加到重新初始化的队列时,它们立即启动,因为系统没有确定是否已将某些内容移交给用户区并且当前正在运行。
所以,这是用户错误... PEBCAK!将解决方案更多地发布为警告故事:
上面的队列按设计工作,但是我有一个端点供调用服务器根据需要清除队列;问题是我正在使用kill()
并重新初始化队列,丢失了所有正在进行的作业及其回调的所有记录。一旦有新项目进入新的队列,它将认为什么都没有发生,并产生了新的复制过程。我通过使用remove
清除队列而不是重新初始化来解决。