We put an ONNX background-removal container behind a Next.js Worker on Cloudflare
说明与用途
该 Web 应用使用 Cloudflare Workers、R2 和带有 ONNX 模型的 Python 容器进行背景移除。架构将请求拆分:POST 生成作业,客户端轮询 GET 在 CPU 限制内执行推理。模型在后台加载,权重已打包到镜像中,文件清理按计划执行。
问题
Background removal in production requires handling queueing, cold starts, and CPU limits while keeping latency low.
结果
The architecture reduces cold‑start latency, respects Cloudflare’s 30s CPU limit, and provides reliable background‑removal service with scheduled cleanup.
工作流
- 1. Client POST /api/tool/background-to-white uploads image, validates, stores in R2, creates job in D1, returns jobId.
- 2. Client polls GET /api/tool/background-to-white?jobId=… every 3s.
- 3. First poll that claims the job runs the ONNX container inference within a 20s long‑poll request.
- 4. Container loads model in background thread to satisfy health checks.
- 5. A warm endpoint (/api/warm) is called on page load to pre‑start the container during user idle time.
- 6. Cron jobs clean expired R2 uploads every 15min and resume orphaned jobs every minute.
证据
The container is a python:3.12-slim image with the model baked in at build time
We set our CPU budget to 30,000 ms in wrangler.jsonc
The port is ready in milliseconds, health checks pass, and the first real request just waits a little longer.
- 记录日期: 2026-09-21T11:45:26.642Z