|
| 1 | +import codecs |
1 | 2 | import os |
2 | 3 | from datetime import UTC, datetime |
3 | 4 |
|
@@ -48,42 +49,59 @@ def _run_ingest( |
48 | 49 | add_log(entity, LogLevel.INFO, "worker", f"Running command: {' '.join(cmd)}") |
49 | 50 | print(f"Starting ingestion with command: {' '.join(cmd)} ...") |
50 | 51 |
|
51 | | - exec_result = container.exec_run( |
52 | | - cmd=cmd, |
53 | | - workdir=workdir, |
| 52 | + entity.command = " ".join(cmd) |
| 53 | + session.add(entity) |
| 54 | + session.commit() |
| 55 | + session.refresh(entity) |
| 56 | + |
| 57 | + exec_id = client.api.exec_create( |
| 58 | + container.id, |
| 59 | + cmd, |
54 | 60 | stdout=True, |
55 | 61 | stderr=True, |
56 | | - stream=False, |
| 62 | + workdir=workdir, |
| 63 | + environment={"PYTHONUNBUFFERED": "1"}, |
57 | 64 | ) |
| 65 | + stream = client.api.exec_start(exec_id["Id"], stream=True) |
| 66 | + decoder = codecs.getincrementaldecoder("utf-8")() |
| 67 | + buffer = "" |
| 68 | + |
| 69 | + for chunk in stream: |
| 70 | + text = decoder.decode(chunk) |
| 71 | + buffer += text |
| 72 | + while "\n" in buffer: |
| 73 | + line, buffer = buffer.split("\n", 1) |
| 74 | + line = line.rstrip("\r") |
| 75 | + |
| 76 | + if not line.strip(): |
| 77 | + continue |
| 78 | + |
| 79 | + add_log(entity, LogLevel.INFO, "docker", line) |
| 80 | + session.add(entity) |
| 81 | + session.commit() |
| 82 | + |
| 83 | + session.add(entity) |
| 84 | + session.commit() |
| 85 | + session.refresh(entity) |
| 86 | + |
| 87 | + inspect = client.api.exec_inspect(exec_id["Id"]) |
| 88 | + exit_code = inspect["ExitCode"] |
58 | 89 |
|
59 | 90 | container.reload() |
60 | | - entity.command = " ".join(cmd) |
61 | 91 | entity.cbioportal_version = ( |
62 | 92 | getattr(container, "attrs", {}).get("Config", {}).get("Image", "unknown") |
63 | 93 | ) |
64 | 94 | session.add(entity) |
65 | 95 | session.commit() |
66 | 96 | session.refresh(entity) |
67 | 97 |
|
68 | | - logs = exec_result.output.decode("utf-8").split("\n") |
69 | | - exit_code = exec_result.exit_code |
70 | | - |
71 | 98 | print(f"Finished ingestion with exit code {exit_code} (0 = success)") |
72 | 99 |
|
73 | | - log_level = LogLevel.INFO if exit_code == 0 else LogLevel.ERROR |
74 | | - |
75 | | - for log in logs: |
76 | | - stripped_log = log.strip() |
77 | | - if stripped_log: |
78 | | - add_log(entity, log_level, "docker", stripped_log) |
79 | | - |
80 | 100 | if exit_code == 0: |
81 | 101 | print("Restarting container to apply changes ...") |
82 | 102 | container.restart() |
83 | 103 | print("Finished restarting container") |
84 | | - add_log( |
85 | | - entity, LogLevel.INFO, "worker", "Container restarted to apply changes." |
86 | | - ) |
| 104 | + add_log(entity, LogLevel.INFO, "worker", "Container restarted to apply changes.") |
87 | 105 | mark_completed(entity, session) |
88 | 106 | entity.date_ingested = datetime.now(UTC) |
89 | 107 | session.add(entity) |
@@ -117,7 +135,7 @@ def ingest_study(study_id: int) -> None: |
117 | 135 | cmd = [ |
118 | 136 | "metaImport.py", |
119 | 137 | "-u", |
120 | | - os.getenv("CBIOPORTAL_URL", "http://cbioportal:8080"), |
| 138 | + "http://cbioportal:8080", |
121 | 139 | "-s", |
122 | 140 | f"/study/{validated_name}", |
123 | 141 | "-o", |
|
0 commit comments