Skip to content

Commit

Permalink
feat: hold monitor when serve app fail to start
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotzh committed May 11, 2024
1 parent a5a9d4a commit d62fceb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,8 @@ def start_chat_ui_service_monitor(
try:
monitor.start_monitor()
except KeyboardInterrupt:
print("Stopping monitor...")
print("Stopping monitor and attached serve app...")
serve_app_helper = monitor.last_callback_result
if serve_app_helper is not None:
serve_app_helper.terminate()
print("Stopped monitor and attached serve app.")
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,8 @@ def resolve_flow_language(
if flow_path is not None and yaml_dict is not None:
raise UserErrorException("Only one of flow_path and yaml_dict should be provided.")
if flow_path is not None:
flow_path, flow_file = resolve_flow_path(flow_path, base_path=working_dir)
# flow path must exist
flow_path, flow_file = resolve_flow_path(flow_path, base_path=working_dir, check_flow_exist=True)
file_path = flow_path / flow_file
if file_path.is_file() and file_path.suffix.lower() in (".yaml", ".yml"):
yaml_dict = load_yaml(file_path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ def __init__(
self._last_callback_result = None
self._inject_last_callback_result = inject_last_callback_result

@property
def last_callback_result(self):
return self._last_callback_result

def start_monitor(self, interval=0.1):
"""Start monitoring the targets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def _update_stat(self, key: str, cache: dict) -> bool:
return True


def write_json(file_path, data):
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f)


@pytest.mark.unittest
class TestMonitorUtils:
def test_directory_modification_monitor_target(self):
Expand All @@ -48,7 +53,7 @@ def test_directory_modification_monitor_target(self):
(temp_dir / "test.txt").touch()
assert monitor_target.update_cache(cache) is True, "file added"
assert monitor_target.update_cache(cache) is False, "same content"
(temp_dir / "test.txt").write_text("test")
write_json(temp_dir / "test.txt", "test")
assert monitor_target.update_cache(cache) is True, "content changed"
assert monitor_target.update_cache(cache) is False, "same content"

Expand Down Expand Up @@ -83,8 +88,8 @@ def test_content_modification_monitor_target(self):
cache = {}

with tempfile.TemporaryDirectory() as temp_dir:
temp_dir = Path(temp_dir)
monitor_target = JsonContentMonitorTarget(target=temp_dir / "test.json", node_path=["key1", "key2"])
target_file = Path(temp_dir) / "test.json"
monitor_target = JsonContentMonitorTarget(target=target_file, node_path=["key1", "key2"])

assert monitor_target.update_cache(cache) is True, "first time"
assert monitor_target.update_cache(cache) is False, "still not exist"
Expand All @@ -94,23 +99,23 @@ def test_content_modification_monitor_target(self):
"key1": "value1",
}
}
(temp_dir / "test.json").write_text(json.dumps(data))
write_json(target_file, data)
assert monitor_target.update_cache(cache) is False, "no related content found"

data["key1"]["key2"] = "value2"
(temp_dir / "test.json").write_text(json.dumps(data))
write_json(target_file, data)
assert monitor_target.update_cache(cache) is True, "new content"

data["key1"]["key2"] = "value3"
(temp_dir / "test.json").write_text(json.dumps(data))
write_json(target_file, data)
assert monitor_target.update_cache(cache) is True, "change related"

data["key1"]["key1"] = "value2"
(temp_dir / "test.json").write_text(json.dumps(data))
write_json(target_file, data)
assert monitor_target.update_cache(cache) is False, "change not related"

del data["key1"]["key2"]
(temp_dir / "test.json").write_text(json.dumps(data))
write_json(target_file, data)
assert monitor_target.update_cache(cache) is True, "delete related"

def test_monitor(self, capsys):
Expand All @@ -125,7 +130,7 @@ def test_monitor(self, capsys):

try:
# give monitor some time to start
time.sleep(0.2)
time.sleep(0.1)
stdout, _ = capsys.readouterr()
assert stdout == "callback\n", "callback for first run"

Expand Down Expand Up @@ -181,7 +186,7 @@ def callback_accept_last_result(last_result, *, step):
future = executor.submit(monitor.start_monitor, interval=0.05)
try:
# give monitor some time to start
time.sleep(0.2)
time.sleep(0.1)
assert global_var_2 == 0, "callback for first run"

global_var = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def test_resolve_flow_language(self):

with pytest.raises(UserErrorException) as ex:
resolve_flow_language(flow_path="mock_path")
assert "must exist and of suffix yaml, yml or prompty." in ex.value.message
assert "mock_path does not exist." in ex.value.message


@pytest.mark.unittest
Expand Down

0 comments on commit d62fceb

Please sign in to comment.