feat(storage): require readable connections before adding servers
This commit is contained in:
1 parent
b34d51432d
commit
8417b1dff8
11 files changed
+100
-20
No files matched your search
+17
-3
@@ -4,9 +4,23 @@ const {SmbClient,segments,inside}=require('./storage-files.cjs');
|
||||
const fail=(message,status=400)=>Object.assign(new Error(message),{status});
|
||||
const safe=e=>e.status?e:fail(({ENOENT:'找不到文件或归档目录,请检查路径。',EACCES:'没有共享目录访问权限。',EPERM:'没有共享目录操作权限。',ENOSPC:'存储服务器空间不足。',ENOTDIR:'指定路径不是目录。'})[e.code]||'存储操作未完成,请检查服务器与网络。',502);
|
||||
class StorageService{
|
||||
constructor({store,jobs,outputDir,client=new SmbClient()}){this.store=store;this.jobs=jobs;this.outputDir=outputDir;this.client=client;this.running=new Map();this.stopped=false;}
|
||||
constructor({store,jobs,outputDir,client=new SmbClient()}){this.store=store;this.jobs=jobs;this.outputDir=outputDir;this.client=client;this.running=new Map();this.pendingChecks=new Set();this.stopped=false;}
|
||||
busy(id){return this.running.has(id);}
|
||||
saveServer(input){if(input?.id&&this.busy(input.id))throw fail('此服务器正在执行操作,请稍后保存。',409);return this.store.saveServer(input);}
|
||||
async saveServer(input){
|
||||
if(this.stopped)throw fail('存储服务正在停止。',503);
|
||||
const snapshot=structuredClone(input),{changed}=this.store.prepareServer(snapshot);
|
||||
if(snapshot.id&&this.busy(snapshot.id))throw fail('此服务器正在执行操作,请稍后保存。',409);
|
||||
const readState=changed?await this.checkConnection(snapshot):null;
|
||||
if(this.stopped)throw fail('存储服务正在停止。',503);
|
||||
if(snapshot.id&&this.busy(snapshot.id))throw fail('此服务器正在执行操作,请稍后保存。',409);
|
||||
return this.store.saveServer(snapshot,readState);
|
||||
}
|
||||
async checkConnection(input){
|
||||
if(this.stopped)throw fail('存储服务正在停止。',503);
|
||||
const {saved}=this.store.prepareServer(input);
|
||||
const task=(async()=>{try{const area=await this.client.area(saved);await area.list('');return {readable:true,checkedAt:new Date().toISOString(),message:'连接成功,目标目录可读取。'};}catch(error){throw safe(error);}})();
|
||||
this.pendingChecks.add(task);try{return await task;}finally{this.pendingChecks.delete(task);}
|
||||
}
|
||||
saveSettings(input){if(input?.enabled){const server=this.store.publicServer(input.serverId);if(!server.enabled||!server.state.readable||!server.state.writable)throw fail('请先启用服务器并检查读写成功。',409);}return this.store.saveSettings(input);}
|
||||
run(id,work){
|
||||
if(this.stopped)return Promise.reject(fail('存储服务正在停止。',503));
|
||||
@@ -31,6 +45,6 @@ class StorageService{
|
||||
});
|
||||
}
|
||||
async automatic(job){const settings=this.store.settings();if(!settings.enabled||job.status!=='success'||!job.createdAt||job.createdAt<settings.since)return null;return this.archive(settings.serverId,job.jobId,'','自动归档');}
|
||||
async stop(){this.stopped=true;await Promise.allSettled([...this.running.values()]);}
|
||||
async stop(){this.stopped=true;await Promise.allSettled([...this.running.values(),...this.pendingChecks]);}
|
||||
}
|
||||
module.exports={StorageService};
|
||||
Reference in new issue
Block a user