Problem
The puppeteer-worker has repeated error handling, retry, and flush patterns across multiple modules:
Repeated patterns
-
File capture lifecycle (file-capture.ts): Memory and Redis providers both implement the same store/retrieve/delete/cleanup cycle with identical error logging (console.error("Failed to ...")).
-
Event shipping (event-shipper.ts, puppeteer-runner.ts): Both contain flush/backoff logic for batching events. Retry logic, timeout handling, and "flush on shutdown" are duplicated between the two files.
-
Page event handlers (puppeteer-runner.ts): page.on("console") and page.on("pageerror") both wrap identical try/catch + logging patterns.
Impact
- Shutdown safety: If flush behavior changes (e.g., adding a final drain step), it must be updated in multiple places.
- Error visibility: Different modules log errors differently, making debugging inconsistent.
- Test coverage: Each module's cleanup path is tested separately but shares the same failure modes.
Proposed fix
-
Extract a SafeAsync helper: wraps async ops with standardized error logging and swallowed shutdown errors.
-
Consolidate flush/backoff into EventShipper and have PuppeteerRunner delegate to it rather than duplicating retry logic.
-
Extract page event handler wrapper:
function onPageEvent(page, event, handler) {
page.on(event, (...args) => {
try {
handler(...args);
} catch (err) {
console.error(`Error in ${event} handler`, err);
}
});
}
Files to change
services/puppeteer-worker/src/file-capture.ts
services/puppeteer-worker/src/puppeteer-runner.ts
services/puppeteer-worker/src/event-shipper.ts
- New:
services/puppeteer-worker/src/helpers.ts (for shared utilities)
Acceptance criteria
- Single source of truth for flush/backoff behavior
- Consistent error logging across file capture providers
- Page event handlers wrapped with standardized try/catch
- Lint + tests pass
Problem
The puppeteer-worker has repeated error handling, retry, and flush patterns across multiple modules:
Repeated patterns
File capture lifecycle (
file-capture.ts): Memory and Redis providers both implement the same store/retrieve/delete/cleanup cycle with identical error logging (console.error("Failed to ...")).Event shipping (
event-shipper.ts,puppeteer-runner.ts): Both contain flush/backoff logic for batching events. Retry logic, timeout handling, and "flush on shutdown" are duplicated between the two files.Page event handlers (
puppeteer-runner.ts):page.on("console")andpage.on("pageerror")both wrap identical try/catch + logging patterns.Impact
Proposed fix
Extract a
SafeAsynchelper: wraps async ops with standardized error logging and swallowed shutdown errors.Consolidate flush/backoff into
EventShipperand havePuppeteerRunnerdelegate to it rather than duplicating retry logic.Extract page event handler wrapper:
Files to change
services/puppeteer-worker/src/file-capture.tsservices/puppeteer-worker/src/puppeteer-runner.tsservices/puppeteer-worker/src/event-shipper.tsservices/puppeteer-worker/src/helpers.ts(for shared utilities)Acceptance criteria