Skip to content

Commit

Permalink
bugfix: ensure that hero instances get closed every time
Browse files Browse the repository at this point in the history
  • Loading branch information
densumesh authored and cdxker committed Jan 3, 2025
1 parent e310690 commit 7f7b706
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions apps/puppeteer-service-ts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ const scrape = async (
throw new Error("Failed to get page content");
}

return { heroInstance, pageContent, pageStatusCode };
heroInstance.close().catch(console.error);

return { pageContent, pageStatusCode };
};

/**
Expand Down Expand Up @@ -228,7 +230,6 @@ app.post("/scrape", async (req: Request, res: Response) => {

let pageContent: string | null = null;
let pageStatusCode: number | null = null;
let heroInstance: Hero | undefined;
const startTime = Date.now();

const attemptScrape = async () => {
Expand Down Expand Up @@ -261,17 +262,13 @@ app.post("/scrape", async (req: Request, res: Response) => {
};

try {
({ heroInstance, pageContent, pageStatusCode } = await attemptScrape());
({ pageContent, pageStatusCode } = await attemptScrape());
} catch (error) {
console.error("Scraping error:", error);
return res.status(500).json({
error: "Failed to scrape the page",
details: error instanceof Error ? error.message : String(error),
});
} finally {
if (heroInstance) {
await heroInstance.close().catch(console.error);
}
}

const errorMessage = getError(pageStatusCode);
Expand Down

0 comments on commit 7f7b706

Please sign in to comment.