Here I crawl email for every 10 minutes to updatemy DB, but i am facing problem with last 4 emails, during which my imap connection closes, and hence it do not perform that operation.
export const readAllEmailReports = async () => { try { let visitedData = {}; const imap = new Imap(imapConfig); const yesterday = moment().subtract(5, "hour").toDate(); imap.once("ready", () => { imap.openBox("INBOX", false, () => { imap.search( [ "ALL", // "UNSEEN", ["FROM", "[email protected]"], [ "OR", ["SUBJECT", "Complete_Report"], ["SUBJECT", "Partial_Report"], ], ["SINCE", yesterday], ], (err, results) => { if (err) { console.error(err); return; } if (results.length === 0) { console.log("No new messages to fetch."); imap.end(); return; } const f = imap.fetch(results, { bodies: "" }); f.on("message", async (msg) => { msg.on("body", async (stream) => { simpleParser(stream, async (err, parsed) => { if (err) { console.error("Error parsing email:", err); throw err; } const text = parsed.text ? parsed.text : parsed.html; const parsedText = extractVidInfo(text, parsed?.subject); if ( !visitedData[ parsedText.visitNumber + "-" + parsedText.reportType ] ) { // console.log("inserting"); // If the visit number doesn't exist in the map, create a new entry visitedData[ parsedText.visitNumber + "-" + parsedText.reportType ] = { visitNumber: parsedText?.visitNumber, reportType: parsedText?.reportType, subject: parsed?.subject, // text: text, attachments: [], }; if (parsed.attachments && parsed.attachments.length > 0) { for (let attachment of parsed.attachments) { if (isSupportedAttachment(attachment)) visitedData[ parsedText.visitNumber + "-" + parsedText.reportType ].attachments.push(attachment); } } } else { console.log( "not inserting", parsedText.visitNumber, "----", parsedText.reportType ); } }); }); }); f.once("error", (ex) => { console.error(ex); }); f.once("end", async () => { const processingTasks = Object.entries(visitedData).map(async ([key, visitData]) => { "some complex logic" }); if (processingTasks.length > 0) { await Promise.all(processingTasks); } console.log("Done fetching all messages!"); imap.end(); }); } ); }); }); imap.once("error", (err) => { console.error(err); }); imap.once("end", () => { console.log("Connection ended"); }); imap.connect(); } catch (ex) { console.error("An error occurred", ex); } };
Here I loop visiteData to perform some checks and update the db, but when it is at the end of operation, imap connection closes suddenly