Nhận PDF upload trực tiếp qua POST /parse
API trước đây chỉ nhận đường dẫn HDFS từ Airflow. Thêm endpoint
multipart/form-data nhận thẳng file PDF, vẫn chạy bất đồng bộ: trả 202 kèm
job_id và result_url, worker xử lý nền, client lấy dla-document-bundle ở
GET /jobs/{job_id}/result hoặc qua callback.
- File upload ghi xuống IDP_DATA_DIR/uploads theo sha256 nội dung nên gửi lại
cùng request_id trả cùng job_id; file được xoá khi không còn job nào dùng.
- Thêm cột filename/source_kind để bundle giữ đúng tên file gốc thay vì tên
đã băm.
- IDP_RUN_WORKER=1 chạy worker trong process API cho triển khai một host.
- IDP_OCR_BACKEND=pdf_text dựng bundle từ text layer của PDF để chạy thử toàn
bộ luồng khi chưa serve được model.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -31,6 +31,7 @@ class Store:
|
||||
CREATE TABLE IF NOT EXISTS jobs (
|
||||
id TEXT PRIMARY KEY, request_id TEXT NOT NULL UNIQUE,
|
||||
file_path TEXT NOT NULL, callback_url TEXT, batch_id TEXT,
|
||||
filename TEXT, source_kind TEXT NOT NULL DEFAULT 'path',
|
||||
status TEXT NOT NULL, created_at REAL NOT NULL, updated_at REAL NOT NULL,
|
||||
total_pages INTEGER NOT NULL DEFAULT 0,
|
||||
attempts INTEGER NOT NULL DEFAULT 0,
|
||||
@@ -58,6 +59,10 @@ class Store:
|
||||
columns = {row[1] for row in db.execute("PRAGMA table_info(jobs)")}
|
||||
if "result_json" not in columns:
|
||||
db.execute("ALTER TABLE jobs ADD COLUMN result_json TEXT")
|
||||
if "filename" not in columns:
|
||||
db.execute("ALTER TABLE jobs ADD COLUMN filename TEXT")
|
||||
if "source_kind" not in columns:
|
||||
db.execute("ALTER TABLE jobs ADD COLUMN source_kind TEXT NOT NULL DEFAULT 'path'")
|
||||
|
||||
@contextmanager
|
||||
def tx(self):
|
||||
@@ -77,21 +82,26 @@ class Store:
|
||||
return dict(row) if row else None
|
||||
|
||||
def create_job(self, request_id: str, file_path: str, callback_url: str | None,
|
||||
batch_id: str | None, max_queued: int):
|
||||
batch_id: str | None, max_queued: int, filename: str | None = None,
|
||||
source_kind: str = "path"):
|
||||
now = time.time()
|
||||
filename = filename or Path(file_path).name
|
||||
with self.tx() as db:
|
||||
prior = db.execute("SELECT * FROM jobs WHERE request_id=?", (request_id,)).fetchone()
|
||||
if prior:
|
||||
if (prior["file_path"], prior["callback_url"], prior["batch_id"]) != (file_path, callback_url, batch_id):
|
||||
if (prior["file_path"], prior["callback_url"], prior["batch_id"],
|
||||
prior["filename"]) != (file_path, callback_url, batch_id, filename):
|
||||
raise ValueError("request_id already exists with different inputs")
|
||||
return self.as_dict(prior), False
|
||||
queued = db.execute("SELECT count(*) FROM jobs WHERE status IN ('queued','preparing','processing','finalizing')").fetchone()[0]
|
||||
if queued >= max_queued:
|
||||
raise OverflowError("Job queue is full")
|
||||
job_id = "job_" + uuid4().hex
|
||||
db.execute("""INSERT INTO jobs(id,request_id,file_path,callback_url,batch_id,status,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,'queued',?,?)""",
|
||||
(job_id, request_id, file_path, callback_url, batch_id, now, now))
|
||||
db.execute("""INSERT INTO jobs(id,request_id,file_path,callback_url,batch_id,filename,
|
||||
source_kind,status,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,?,?,'queued',?,?)""",
|
||||
(job_id, request_id, file_path, callback_url, batch_id, filename,
|
||||
source_kind, now, now))
|
||||
return self.as_dict(db.execute("SELECT * FROM jobs WHERE id=?", (job_id,)).fetchone()), True
|
||||
|
||||
def get_job(self, job_id: str):
|
||||
@@ -107,6 +117,13 @@ class Store:
|
||||
rows = db.execute("SELECT page_no,status,attempts,error FROM pages WHERE job_id=? ORDER BY page_no", (job_id,)).fetchall()
|
||||
return [self.as_dict(row) for row in rows]
|
||||
|
||||
def upload_in_use(self, file_path: str, exclude_job_id: str) -> bool:
|
||||
"""Một upload được dedupe theo sha256 nên nhiều job có thể trỏ vào cùng file."""
|
||||
with self.connect() as db:
|
||||
return db.execute("""SELECT count(*) FROM jobs WHERE file_path=? AND id<>?
|
||||
AND status IN ('queued','preparing','processing','finalizing')""",
|
||||
(file_path, exclude_job_id)).fetchone()[0] > 0
|
||||
|
||||
def claim_prepare(self, lease_seconds: int):
|
||||
now = time.time()
|
||||
with self.tx() as db:
|
||||
|
||||
Reference in New Issue
Block a user