From 9f0926f72b24ccb30f020b5b4e5c8ac085feb62a Mon Sep 17 00:00:00 2001 From: Potenz Date: Fri, 3 Oct 2025 22:26:54 +0530 Subject: [PATCH] fix(empty temp files, if error) --- apps/Backend/src/routes/insuranceStatus.ts | 34 +++++++++---------- .../selenium_claimStatusCheckWorker.py | 17 ++++++++++ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/apps/Backend/src/routes/insuranceStatus.ts b/apps/Backend/src/routes/insuranceStatus.ts index 2b7198d..681f4b8 100644 --- a/apps/Backend/src/routes/insuranceStatus.ts +++ b/apps/Backend/src/routes/insuranceStatus.ts @@ -201,6 +201,8 @@ router.post( return res.status(401).json({ error: "Unauthorized: user info missing" }); } + let result: any = undefined; + async function imageToPdfBuffer(imagePath: string): Promise { return new Promise((resolve, reject) => { try { @@ -249,8 +251,7 @@ router.post( massdhpPassword: credentials.password, }; - const result = - await forwardToSeleniumInsuranceClaimStatusAgent(enrichedData); + result = await forwardToSeleniumInsuranceClaimStatusAgent(enrichedData); let createdPdfFileId: number | null = null; @@ -329,21 +330,8 @@ router.post( } // Clean up temp files: - try { - // remove generated PDF file (if it was created during conversion) - 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); + if (result.pdf_path) { + await emptyFolderContainingFile(result.pdf_path); } result.pdfUploadStatus = `PDF saved to group: ${group.title}`; @@ -360,6 +348,18 @@ router.post( return; } catch (err: any) { 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({ error: err.message || "Failed to forward to selenium agent", }); diff --git a/apps/SeleniumService/selenium_claimStatusCheckWorker.py b/apps/SeleniumService/selenium_claimStatusCheckWorker.py index 3644c9d..728491b 100644 --- a/apps/SeleniumService/selenium_claimStatusCheckWorker.py +++ b/apps/SeleniumService/selenium_claimStatusCheckWorker.py @@ -169,6 +169,23 @@ class AutomationMassHealthClaimStatusCheck: except Exception as 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)} finally: