Update rule + flow hdfs new
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Run separately from the API: python -m idp_service.worker."""
|
||||
import hashlib
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import shutil
|
||||
@@ -16,7 +17,7 @@ from .config import Settings
|
||||
from .db import Store
|
||||
from .decrypt import prepare_pdf
|
||||
from .dla import build_outline
|
||||
from .ocr import InvalidOutput, infer_page
|
||||
from .ocr import InvalidOutput, infer_page, render_page
|
||||
from .storage import fetch_pdf
|
||||
|
||||
|
||||
@@ -122,7 +123,8 @@ class Worker:
|
||||
result_json = json.dumps(bundle, ensure_ascii=False, separators=(",", ":"))
|
||||
has_failures = any(p["status"] == "failed" for p in pages)
|
||||
if self.store.finalized(job_id, job["finalize_attempts"], result_json, has_failures):
|
||||
shutil.rmtree(self.job_dir(job_id), ignore_errors=True)
|
||||
if not job["callback_url"]:
|
||||
shutil.rmtree(self.job_dir(job_id), ignore_errors=True)
|
||||
self.cleanup_source(job)
|
||||
log.info("Finalized %s for callback", job_id)
|
||||
except Exception as exc:
|
||||
@@ -131,12 +133,28 @@ class Worker:
|
||||
|
||||
def callback(self, job: dict):
|
||||
try:
|
||||
canonical = json.loads(job["result_json"]) if job["result_json"] else None
|
||||
extraction = None
|
||||
if canonical is not None:
|
||||
source = self.job_dir(job["id"]) / "source.pdf"
|
||||
extraction = {"pages": []}
|
||||
for page_no in range(1, canonical["source"]["page_count"] + 1):
|
||||
image, _, _ = render_page(source, page_no)
|
||||
extraction["pages"].append({
|
||||
"page_no": page_no,
|
||||
"media_type": "image/png",
|
||||
"data_base64": base64.b64encode(image).decode("ascii"),
|
||||
})
|
||||
payload = {
|
||||
"request_id": job["request_id"], "job_id": job["id"],
|
||||
"batch_id": job["batch_id"],
|
||||
"status": "done" if job["status"] == "completed" else job["status"],
|
||||
"output": json.loads(job["result_json"]) if job["result_json"] else None,
|
||||
"error": job["error"],
|
||||
"dag_run_id": job["dag_run_id"] or job["id"],
|
||||
"conf": {
|
||||
"request_id": job["request_id"], "job_id": job["id"],
|
||||
"status": "done" if job["status"] == "completed" else job["status"],
|
||||
"output": {
|
||||
"extraction": extraction,
|
||||
"canonical": canonical,
|
||||
},
|
||||
},
|
||||
}
|
||||
request = Request(job["callback_url"], data=json.dumps(payload).encode("utf-8"),
|
||||
headers={"Content-Type": "application/json",
|
||||
@@ -145,10 +163,13 @@ class Worker:
|
||||
if not 200 <= response.status < 300:
|
||||
raise RuntimeError(f"Callback HTTP {response.status}")
|
||||
self.store.callback_done(job["id"], job["callback_attempts"])
|
||||
shutil.rmtree(self.job_dir(job["id"]), ignore_errors=True)
|
||||
except Exception as exc:
|
||||
log.warning("Callback failed for %s: %s", job["id"], exc)
|
||||
self.store.callback_failed(job["id"], job["callback_attempts"],
|
||||
str(exc), self.settings.max_callback_attempts)
|
||||
if job["callback_attempts"] >= self.settings.max_callback_attempts:
|
||||
shutil.rmtree(self.job_dir(job["id"]), ignore_errors=True)
|
||||
|
||||
def coordinator_once(self) -> bool:
|
||||
did_work = False
|
||||
|
||||
Reference in New Issue
Block a user