Skip to content

Commit 5ed99cc

Browse files
committed
fix: daemon mode wasn't reporting progress
Signed-off-by: Morgan Epp <60796713+epmog@users.noreply.github.com>
1 parent 0c85c36 commit 5ed99cc

7 files changed

Lines changed: 224 additions & 24 deletions

File tree

‎src/openjd/adaptor_runtime/adaptors/_base_adaptor.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from typing import Type
1414
from typing import TypeVar
1515

16+
from ..process._logging import _ADAPTOR_OUTPUT_LEVEL
1617
from .._utils._constants import _OPENJD_PROGRESS_STDOUT_PREFIX, _OPENJD_STATUS_STDOUT_PREFIX
1718
from .configuration import AdaptorConfiguration, ConfigurationManager
1819
from .configuration._configuration_manager import (
@@ -194,16 +195,20 @@ def update_status(
194195

195196
if progress is not None:
196197
if math.isfinite(progress):
197-
sys.stdout.write(f"{cls._OPENJD_PROGRESS_STDOUT_PREFIX}{progress}{os.linesep}")
198-
sys.stdout.flush()
198+
_logger.log(
199+
_ADAPTOR_OUTPUT_LEVEL,
200+
f"{cls._OPENJD_PROGRESS_STDOUT_PREFIX}{progress}",
201+
)
199202
else:
200203
_logger.warning(
201204
f"Attempted to set progress to something non-finite: {progress}. "
202205
"Ignoring progress update."
203206
)
204207
if status_message is not None:
205-
sys.stdout.write(f"{cls._OPENJD_STATUS_STDOUT_PREFIX}{status_message}{os.linesep}")
206-
sys.stdout.flush()
208+
_logger.log(
209+
_ADAPTOR_OUTPUT_LEVEL,
210+
f"{cls._OPENJD_STATUS_STDOUT_PREFIX}{status_message}",
211+
)
207212

208213
@property
209214
def path_mapping_rules(self) -> list[PathMappingRule]:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
from .adaptor import ProgressReportingAdaptor
4+
5+
__all__ = ["ProgressReportingAdaptor"]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
import sys
4+
5+
from openjd.adaptor_runtime import EntryPoint
6+
from .adaptor import ProgressReportingAdaptor
7+
8+
9+
def main():
10+
package_name = vars(sys.modules[__name__])["__package__"]
11+
if not package_name:
12+
raise RuntimeError(f"Must be run as a module. Do not run {__file__} directly")
13+
14+
EntryPoint(ProgressReportingAdaptor).start()
15+
16+
17+
if __name__ == "__main__":
18+
main()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
from openjd.adaptor_runtime.adaptors import Adaptor, SemanticVersion
4+
5+
6+
class ProgressReportingAdaptor(Adaptor):
7+
"""
8+
A minimal adaptor that calls update_status to report progress during on_run.
9+
Used to test that progress messages propagate from the background adaptor
10+
to the frontend process's stdout.
11+
"""
12+
13+
@property
14+
def integration_data_interface_version(self) -> SemanticVersion:
15+
return SemanticVersion(major=0, minor=1)
16+
17+
def on_run(self, run_data: dict) -> None:
18+
progress = run_data.get("progress", 50.0)
19+
self.update_status(progress=progress)
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
"""
4+
Test to reproduce a bug where progress reporting via update_status() does not
5+
propagate from the background adaptor to the frontend process's stdout.
6+
7+
The flow is:
8+
1. Application outputs progress info
9+
2. Background adaptor (backend process) calls update_status(progress=X)
10+
3. update_status writes "openjd_progress: X" to sys.stdout
11+
4. Frontend process should emit "openjd_progress: X" on its own stdout
12+
so the worker agent can detect it
13+
14+
The bug: In daemon/background mode, the backend process replaces its stream
15+
handler with a LogBufferHandler. But update_status() writes directly to
16+
sys.stdout (not through the logging framework), so the progress message
17+
never reaches the log buffer, and thus never reaches the frontend.
18+
"""
19+
20+
from __future__ import annotations
21+
22+
import json
23+
import os
24+
import pathlib
25+
import sys
26+
from logging import INFO
27+
from unittest.mock import patch
28+
from pathlib import Path
29+
30+
import pytest
31+
32+
import openjd.adaptor_runtime._entrypoint as runtime_entrypoint
33+
from openjd.adaptor_runtime import EntryPoint
34+
35+
mod_path = Path(__file__).parent.resolve()
36+
sys.path.append(str(mod_path))
37+
38+
if (_pypath := os.environ.get("PYTHONPATH")) is not None:
39+
os.environ["PYTHONPATH"] = os.pathsep.join([_pypath, str(mod_path)])
40+
else:
41+
os.environ["PYTHONPATH"] = str(mod_path)
42+
43+
from ProgressReportingAdaptor import ProgressReportingAdaptor # noqa: E402
44+
45+
46+
class TestProgressReportingInDaemonMode:
47+
"""
48+
Tests that progress messages from update_status() in the background adaptor
49+
are propagated to the frontend process's stdout so the worker agent can
50+
detect them.
51+
"""
52+
53+
def test_progress_propagates_to_frontend_stdout(
54+
self,
55+
capfd: pytest.CaptureFixture,
56+
caplog: pytest.LogCaptureFixture,
57+
tmp_path: pathlib.Path,
58+
):
59+
"""
60+
Reproduces the bug: when running in daemon mode, calling
61+
update_status(progress=50) in the backend adaptor should result in
62+
"openjd_progress: 50" appearing on the frontend's stdout.
63+
64+
The worker agent monitors the frontend process's stdout for these
65+
messages. If they don't appear, the worker never knows about progress.
66+
"""
67+
caplog.set_level(INFO)
68+
connection_file = tmp_path / "connection.json"
69+
entrypoint = EntryPoint(ProgressReportingAdaptor)
70+
71+
# Start the daemon
72+
start_argv = [
73+
"program_filename.py",
74+
"daemon",
75+
"start",
76+
"--connection-file",
77+
str(connection_file),
78+
]
79+
with (
80+
patch.object(runtime_entrypoint.sys, "argv", start_argv),
81+
patch.object(runtime_entrypoint.logging.Logger, "setLevel"),
82+
):
83+
entrypoint.start()
84+
85+
# Run with progress=50
86+
run_argv = [
87+
"program_filename.py",
88+
"daemon",
89+
"run",
90+
"--connection-file",
91+
str(connection_file),
92+
"--run-data",
93+
json.dumps({"progress": 50.0}),
94+
]
95+
with (
96+
patch.object(runtime_entrypoint.sys, "argv", run_argv),
97+
patch.object(runtime_entrypoint.logging.Logger, "setLevel"),
98+
):
99+
entrypoint.start()
100+
101+
# Stop the daemon
102+
stop_argv = [
103+
"program_filename.py",
104+
"daemon",
105+
"stop",
106+
"--connection-file",
107+
str(connection_file),
108+
]
109+
with (
110+
patch.object(runtime_entrypoint.sys, "argv", stop_argv),
111+
patch.object(runtime_entrypoint.logging.Logger, "setLevel"),
112+
):
113+
entrypoint.start()
114+
115+
# Verify that "openjd_progress: 50" appeared on stdout.
116+
# The worker agent is watching stdout for this pattern.
117+
captured = capfd.readouterr()
118+
assert "openjd_progress: 50" in captured.out, (
119+
f"Expected 'openjd_progress: 50' on stdout but got:\n{captured.out}\n\n"
120+
f"Log output:\n{caplog.text}"
121+
)
122+
123+
def test_progress_works_in_run_mode(
124+
self,
125+
capfd: pytest.CaptureFixture,
126+
caplog: pytest.LogCaptureFixture,
127+
):
128+
"""
129+
Sanity check: progress reporting works fine in non-daemon 'run' mode
130+
because update_status writes directly to sys.stdout and the worker
131+
agent is directly reading from that process's stdout.
132+
"""
133+
caplog.set_level(INFO)
134+
entrypoint = EntryPoint(ProgressReportingAdaptor)
135+
136+
run_argv = [
137+
"program_filename.py",
138+
"run",
139+
"--run-data",
140+
json.dumps({"progress": 75.0}),
141+
]
142+
with (
143+
patch.object(runtime_entrypoint.sys, "argv", run_argv),
144+
patch.object(runtime_entrypoint.logging.Logger, "setLevel"),
145+
):
146+
entrypoint.start()
147+
148+
captured = capfd.readouterr()
149+
assert (
150+
"openjd_progress: 75" in captured.out
151+
), f"Expected 'openjd_progress: 75' on stdout but got:\n{captured.out}"

‎test/openjd/adaptor_runtime/unit/adaptors/test_base_adaptor.py‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -316,16 +316,17 @@ class TestStatusUpdate:
316316
param(1e-5),
317317
],
318318
)
319-
def test_progress_update(self, capsys, progress: float):
319+
def test_progress_update(self, caplog, progress: float):
320320
"""Tests just updating the progress"""
321321
# GIVEN
322322
expected = f"{self._OPENJD_PROGRESS_STDOUT_PREFIX}{progress}"
323323

324324
# WHEN
325-
BaseAdaptor.update_status(progress=progress)
325+
with caplog.at_level(0):
326+
BaseAdaptor.update_status(progress=progress)
326327

327328
# THEN
328-
assert expected in capsys.readouterr().out
329+
assert expected in caplog.text
329330

330331
@pytest.mark.parametrize(
331332
"status_message",
@@ -335,16 +336,17 @@ def test_progress_update(self, capsys, progress: float):
335336
param(""),
336337
],
337338
)
338-
def test_status_message_update(self, capsys, status_message: str):
339+
def test_status_message_update(self, caplog, status_message: str):
339340
"""Tests just updating the status message"""
340341
# GIVEN
341342
expected = f"{self._OPENJD_STATUS_STDOUT_PREFIX}{status_message}"
342343

343344
# WHEN
344-
BaseAdaptor.update_status(status_message=status_message)
345+
with caplog.at_level(0):
346+
BaseAdaptor.update_status(status_message=status_message)
345347

346348
# THEN
347-
assert expected in capsys.readouterr().out
349+
assert expected in caplog.text
348350

349351
@pytest.mark.parametrize(
350352
"progress,status_message",
@@ -360,30 +362,29 @@ def test_status_message_update(self, capsys, status_message: str):
360362
),
361363
],
362364
)
363-
def test_status_update(self, capsys, progress: float, status_message: str):
365+
def test_status_update(self, caplog, progress: float, status_message: str):
364366
"""Tests updating both progress and status messages"""
365367
# GIVEN
366368
expected_progress = f"{self._OPENJD_PROGRESS_STDOUT_PREFIX}{progress}"
367369
expected_status_message = f"{self._OPENJD_STATUS_STDOUT_PREFIX}{status_message}"
368370

369371
# WHEN
370-
BaseAdaptor.update_status(progress=progress, status_message=status_message)
372+
with caplog.at_level(0):
373+
BaseAdaptor.update_status(progress=progress, status_message=status_message)
371374

372375
# THEN
373-
result = capsys.readouterr().out
374-
assert expected_progress in result
375-
assert expected_status_message in result
376+
assert expected_progress in caplog.text
377+
assert expected_status_message in caplog.text
376378

377-
def test_ignore_status_update(self, capsys):
379+
def test_ignore_status_update(self, caplog):
378380
"""Tests we don't send any message if there's nothing to report"""
379-
# GIVEN
380-
expected = "" # nothing was captured in stdout
381-
382381
# WHEN
383-
BaseAdaptor.update_status(progress=None, status_message=None)
384-
BaseAdaptor.update_status(progress=float("NaN"))
385-
BaseAdaptor.update_status(progress=float("inf"))
382+
with caplog.at_level(0):
383+
BaseAdaptor.update_status(progress=None, status_message=None)
384+
BaseAdaptor.update_status(progress=float("NaN"))
385+
BaseAdaptor.update_status(progress=float("inf"))
386386

387387
# THEN
388-
result = capsys.readouterr().out
389-
assert expected == result
388+
# Only warning messages should be logged, no progress/status messages
389+
assert self._OPENJD_PROGRESS_STDOUT_PREFIX not in caplog.text
390+
assert self._OPENJD_STATUS_STDOUT_PREFIX not in caplog.text

0 commit comments

Comments
 (0)