Skip to content

Commit c93e39e

Browse files
committed
🐛 修复删除脚本resource数据未删除的问题
1 parent e3b6604 commit c93e39e

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

src/app/service/service_worker/resource.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { isText } from "@App/pkg/utils/istextorbinary";
99
import { blobToBase64 } from "@App/pkg/utils/script";
1010
import { ResourceBackup } from "@App/pkg/backup/struct";
1111
import { subscribeScriptDelete } from "../queue";
12+
import Cache from "@App/app/cache";
1213

1314
export class ResourceService {
1415
logger: Logger;
@@ -306,20 +307,26 @@ export class ResourceService {
306307

307308
// 删除相关资源
308309
subscribeScriptDelete(this.mq, (data) => {
309-
this.resourceDAO
310-
.find((key, value) => {
310+
// 使用事务当锁,避免并发删除导致数据不一致
311+
Cache.getInstance().tx("resource_lock", async (start) => {
312+
const resources = await this.resourceDAO.find((key, value) => {
311313
return value.link[data.uuid];
312-
})
313-
.then((resources) => {
314-
resources.forEach((res) => {
315-
// 删除link
316-
delete res.link[data.uuid];
317-
// 如果没有关联脚本了,删除资源
318-
if (Object.keys(res.link).length === 0) {
319-
this.resourceDAO.delete(res.url);
320-
}
321-
});
322314
});
315+
resources.forEach((res) => {
316+
// 删除link
317+
delete res.link[data.uuid];
318+
});
319+
await Promise.all(
320+
resources.map((res) => {
321+
if (Object.keys(res.link).length > 0) {
322+
return this.resourceDAO.update(res.url, res);
323+
}
324+
// 如果没有关联脚本了,删除资源
325+
return this.resourceDAO.delete(res.url);
326+
})
327+
);
328+
return true;
329+
});
323330
});
324331
}
325332
}

0 commit comments

Comments
 (0)