Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Improve error handling and pipe flow for `just ps` (#2072)

Madison Swain-Bowdenc0ef50c4fd26
diff --git a/justfile b/justfile
index 205845dd6..35833bea4 100644
--- a/justfile
+++ b/justfile
@@ -149,8 +149,10 @@ build *args:
just dc build {{ args }}
# List all services and their URLs and ports
-ps:
- python3 utilities/ps.py
+@ps:
+ # ps is a helper command & intermediate dependency, so it should not fail the whole
+ # command if it fails
+ python3 utilities/ps.py || true
# Also see `up` recipe in sub-justfiles
# Bring all Docker services up, in all profiles
diff --git a/utilities/ps.py b/utilities/ps.py
index 919a5aea7..cdf1d7a66 100644
--- a/utilities/ps.py
+++ b/utilities/ps.py
@@ -1,4 +1,5 @@
import subprocess
+import traceback
from dataclasses import dataclass
@@ -63,7 +64,10 @@ def parse_ps() -> list[Service]:
lines = get_ps()
lines = [stripped for line in lines if (stripped := line.strip())]
for line in lines:
- service, image, ports = line.split("\t")
+ parts = line.split("\t")
+ if len(parts) != 3:
+ raise ValueError(f"Unexpected line format from `docker ps`: {line}")
+ service, image, ports = parts
# Services are usually of the form openverse-<name>-1 or openverse_<name>_1,
# but we can't always tell which one is used.
strip_char = service.removeprefix("openverse")[0]
@@ -96,8 +100,13 @@ def print_ps():
print("=" * 80)
print(f"{'Service Ports':^80}")
print("=" * 80)
- for service in parse_ps():
- service.print()
+ try:
+ for service in parse_ps():
+ service.print()
+ except Exception as err:
+ print(traceback.format_exc())
+ print("=" * 80)
+ print(f"Failed to get service info ({err.__class__.__name__}): \n{err}")
print("=" * 80)