import Inject from "@entity-access/entity-access/dist/di/di.js"; import Workflow, { UniqueActivity } from "@entity-access/entity-access/dist/workflows/Workflow.js"; import SocialMailContext from "../../../model/SocialMailContext.js"; import BaseStorage from "../../../storage/BaseStorage.js"; export default class RepairCacheClearWorkflow extends Workflow { async run() { let start = 0; do { start = await this.repair(start); } while(start); } @UniqueActivity async repair(start: number, @Inject db?: SocialMailContext, @Inject storage?: BaseStorage ) { const files = await db.cachedItems.where({ start }, (x, p) => x.itemID > p.start) .orderBy((x) => x.itemID) .limit(100) .toArray(); if (!files.length) { return 0; } for (const iterator of files) { start = iterator.itemID; try { await using tx = await db.connection.createTransaction(); const cloudPath = `cache/${iterator.itemID}`; await storage.deleteFile(cloudPath); await db.cachedItems.statements.delete(iterator); await tx.commit(); } catch { // move to next... and ignore the error... } } return start; } }