Files
cobol-java-v3/web/worker.py
T

62 lines
2.2 KiB
Python

"""Worker process — polls task queue, executes run_pipeline()."""
import json, sys, time, shutil
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
TASKS_DIR = Path("tasks")
def main():
print("Worker started. Watching tasks/ ...")
while True:
for tf in sorted(TASKS_DIR.glob("*.json")):
try:
data = json.loads(tf.read_text())
if data.get("status") != "queued":
continue
data["status"] = "running"
tf.write_text(json.dumps(data))
from config import Config
from orchestrator import run_pipeline
cfg = Config()
cfg.runner_mode = data.get("runner", "native")
if cfg.runner_mode == "spark" and not shutil.which("spark-submit"):
data["status"] = "blocked"
data["result"] = "spark-submit not installed"
tf.write_text(json.dumps(data))
continue
vr = run_pipeline(cfg, data["copybook"], data["cobol_src"],
data["java_src"], data["mapping"])
fields = [{"name":fr.field_name,"status":fr.status,
"cobol":fr.cobol_value,"java":fr.java_value,
"suggestion":fr.suggestion} for fr in vr.field_results]
data["status"] = "done"
data["fields"] = fields
data["debug"] = vr.debug
data["result"] = {
"program": vr.program, "status": vr.status,
"matched": vr.fields_matched, "mismatched": vr.fields_mismatched,
"duration": vr.duration_s, "runner": vr.runner,
}
if vr.report_path and "BLOCKED" in vr.status:
data["build_log"] = vr.report_path
tf.write_text(json.dumps(data))
except Exception as e:
data = json.loads(tf.read_text()) if tf.exists() else {}
data["status"] = "error"
data["result"] = str(e)[:500]
tf.write_text(json.dumps(data))
time.sleep(2)
if __name__ == "__main__":
main()