Skip to main content

Asynchronous PDF Generation

Generating PDF documents requires running a headless browser, which is a resource-intensive process. To prevent blocking the main NestJS API thread, Kloyst delegates PDF generation to asynchronous background workers using BullMQ and a Puppeteer Docker sidecar.

🚀 Asynchronous Processing Pipeline


🛠️ Worker Resiliency & Fail-Safe Controls

To keep the message queue running smoothly and prevent stuck jobs, the PDF pipeline implements several safety controls:

  1. Strict Timeout Limits:
    • Jobs are terminated after 60 seconds to prevent slow connections from blocking workers.
  2. Exponential Backoff Retries:
    • Failed jobs are retried up to 3 times with an exponential delay (5s, 10s, 20s) to recover from temporary glitches.
  3. Automatic Queue Cleanup:
    • Successfully completed jobs are deleted after 24 hours.
    • Failed jobs are kept for 7 days to give administrators time to investigate.
  4. Concurrency Controls:
    • Worker concurrency is limited to 2 to prevent high CPU or memory usage.
  5. Worker-Level Failure Handling:
    • If all retries fail, the worker updates the quotation's status to FAILED. This ensures the frontend stops polling and displays an appropriate error message to the user.