fix(empty temp files, if error)

This commit is contained in:
2025-10-03 22:26:54 +05:30
parent 8dd53f2d6a
commit 9f0926f72b
2 changed files with 34 additions and 17 deletions

View File

@@ -201,6 +201,8 @@ router.post(
return res.status(401).json({ error: "Unauthorized: user info missing" }); return res.status(401).json({ error: "Unauthorized: user info missing" });
} }
let result: any = undefined;
async function imageToPdfBuffer(imagePath: string): Promise<Buffer> { async function imageToPdfBuffer(imagePath: string): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => { return new Promise<Buffer>((resolve, reject) => {
try { try {
@@ -249,8 +251,7 @@ router.post(
massdhpPassword: credentials.password, massdhpPassword: credentials.password,
}; };
const result = result = await forwardToSeleniumInsuranceClaimStatusAgent(enrichedData);
await forwardToSeleniumInsuranceClaimStatusAgent(enrichedData);
let createdPdfFileId: number | null = null; let createdPdfFileId: number | null = null;
@@ -329,21 +330,8 @@ router.post(
} }
// Clean up temp files: // Clean up temp files:
try { if (result.pdf_path) {
// remove generated PDF file (if it was created during conversion) await emptyFolderContainingFile(result.pdf_path);
if (
generatedPdfPath &&
fsSync.existsSync(generatedPdfPath) &&
generatedPdfPath !== result.pdf_path
) {
await fs.unlink(generatedPdfPath);
}
// remove screenshot (if provided by Selenium) to avoid lingering temp files
if (result.ss_path && fsSync.existsSync(result.ss_path)) {
await fs.unlink(result.ss_path);
}
} catch (cleanupErr) {
console.warn("Cleanup error (non-fatal):", cleanupErr);
} }
result.pdfUploadStatus = `PDF saved to group: ${group.title}`; result.pdfUploadStatus = `PDF saved to group: ${group.title}`;
@@ -360,6 +348,18 @@ router.post(
return; return;
} catch (err: any) { } catch (err: any) {
console.error(err); console.error(err);
try {
if (result && result.pdf_path) {
await emptyFolderContainingFile(result.pdf_path);
} else {
console.log(`claim-status-check] no pdf_path available to cleanup`);
}
} catch (cleanupErr) {
console.error(
`[claim-status-check cleanup failed for ${result?.pdf_path}`,
cleanupErr
);
}
return res.status(500).json({ return res.status(500).json({
error: err.message || "Failed to forward to selenium agent", error: err.message || "Failed to forward to selenium agent",
}); });

View File

@@ -169,6 +169,23 @@ class AutomationMassHealthClaimStatusCheck:
except Exception as e: except Exception as e:
print("ERROR in step2:", e) print("ERROR in step2:", e)
# Empty the download folder (remove files / symlinks only)
try:
dl = os.path.abspath(self.download_dir)
if os.path.isdir(dl):
for name in os.listdir(dl):
item = os.path.join(dl, name)
try:
if os.path.isfile(item) or os.path.islink(item):
os.remove(item)
print(f"[cleanup] removed: {item}")
except Exception as rm_err:
print(f"[cleanup] failed to remove {item}: {rm_err}")
print(f"[cleanup] emptied download dir: {dl}")
else:
print(f"[cleanup] download dir does not exist: {dl}")
except Exception as cleanup_exc:
print(f"[cleanup] unexpected error while cleaning downloads dir: {cleanup_exc}")
return {"status": "error", "message": str(e)} return {"status": "error", "message": str(e)}
finally: finally: