Code: Select all
## this is the background task
async def generate_pdf_task(order_id: str):
print("Generating PDF for order", order_id)
## This is a background task that takes around 30 seconds to generate a PDF
pdf_url = await upload_file(f"orders/{order_id}.zip", zip_buffer)
# Update order with PDF URL
await update_order(order_id, pdf_url)
print(f"✅ PDF available at: {pdf_url}")
## this is the endpoint that always causes 502 error
@router.post("/generate-pdf/{order_id}")
async def generate_pdf(order_id: str, background_tasks: BackgroundTasks):
print("Task started")
background_tasks.add_task(generate_pdf_task, order_id)
return {"message": "PDF generation started"}
## this function is for polling
@router.get("/get-pdf-url/{order_id}")
async def get_pdf_url(order_id: str):
print("Checking PDF status")
order = await fetch_order(order_id)
if not order:
raise HTTPException(status_code=404, detail="Order not found")
return {"pdf_url": order.get('pdf_url')}