Skip to content

Commit 1797c93

Browse files
committed
Cleanup the code a little. Protect against pod being None in status_data
1 parent 3b66022 commit 1797c93

6 files changed

Lines changed: 23 additions & 13 deletions

File tree

serve_event_listener/el_types.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
# Canonical status/app types
66
Status = Literal["Running", "Pending", "Deleted", "Failed", "Unknown", "Succeeded"]
77
AppType = Literal["shiny", "shiny-proxy"]
8-
ProbeClass = Literal["Running", "Unknown", "NotFound"]
8+
ProbeStatus = Literal["Running", "Unknown", "NotFound"]
99

1010
# Result block produced by the AppAvailabilityProbe (optional on payload)
11-
ProbeBlock = TypedDict(
12-
"ProbeBlock",
11+
ProbeBlockDict = TypedDict(
12+
"ProbeBlockDict",
1313
{
14-
"status": ProbeClass, # e.g., "Running" | "Unknown" | "NotFound"
14+
"status": ProbeStatus, # e.g., "Running" | "Unknown" | "NotFound"
1515
"port80_status": Optional[int], # HTTP status code (if any)
1616
"note": str, # short diagnostic
1717
"url": str, # probed URL
@@ -34,7 +34,7 @@
3434
"message": NotRequired[str],
3535
"app-type": NotRequired[AppType],
3636
"app-url": NotRequired[str],
37-
"curl-probe": NotRequired[ProbeBlock],
37+
"curl-probe": NotRequired[ProbeBlockDict],
3838
},
3939
total=False,
4040
)

serve_event_listener/event_listener.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def status_data(self) -> StatusData:
100100
return self._status_data
101101

102102
@property
103-
def client_api_ping_endpoint(self) -> str:
103+
def client_api_health_endpoint(self) -> str:
104104
"""A URL of the client target API for checking the status of the API is UP or DOWN"""
105105
return BASE_URL + "/openapi/v1/are-you-there"
106106

@@ -133,8 +133,8 @@ def setup(self, **kwargs: Optional[Any]) -> None:
133133

134134
try:
135135
# Verify that the prober works for at least a known URL,
136-
test_url = self.client_api_ping_endpoint
137-
_ = self._prober.probe_url(test_url)
136+
health_url = self.client_api_health_endpoint
137+
_ = self._prober.probe_url(health_url)
138138
except Exception as e:
139139
# Otherwise disable the probing feature
140140
logger.warning("Probing disabled: baseline probe failed (%s)", e)
@@ -279,7 +279,7 @@ def check_serve_api_status(self) -> bool:
279279
Returns:
280280
- bool: True if the status is okay, False otherwise.
281281
"""
282-
url = self.client_api_ping_endpoint
282+
url = self.client_api_health_endpoint
283283
logger.debug("Verifying that the server API is up and available via %s", url)
284284

285285
# Using the new http get function

serve_event_listener/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def parse_args():
6767
"--mode",
6868
choices=["normal", "diagnostics", "probetest"],
6969
default="normal",
70-
help="run mode (default: normal)",
70+
help="run mode (default: normal). diagnostics prints out configuration info and exists. \
71+
probetest runs a probe against a specified URL.",
7172
)
7273
parser.add_argument(
7374
"--probe-url",

serve_event_listener/probing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import requests
1010

11+
from serve_event_listener.el_types import ProbeStatus
1112
from serve_event_listener.http_client import get as http_get
1213

1314
logger = logging.getLogger(__name__)
@@ -17,7 +18,7 @@
1718
class ProbeResult:
1819
"""Outcome of an availability probe."""
1920

20-
status: str # "Running" | "Unknown" | "NotFound"
21+
status: ProbeStatus # "Running" | "Unknown" | "NotFound"
2122
port80_status: Optional[int] = None
2223
note: str = "" # short diagnostic
2324

serve_event_listener/status_data.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@
3030
}
3131

3232

33-
def _detect_app_type(pod) -> Optional[AppType]:
33+
def _detect_app_type(pod: Optional[object]) -> Optional[AppType]:
34+
"""Best-effort app-type detection from a k8s Pod-like object.
35+
36+
Accepts None to match upstream callers.
37+
Returns None when the shape is incomplete or labels are missing.
38+
"""
39+
if pod is None:
40+
return None
41+
3442
labels = getattr(pod.metadata, "labels", None) or {}
3543
ann = getattr(pod.metadata, "annotations", None) or {}
3644
values = " ".join(

tests/integration/test_status_data_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_update_sets_app_url_for_shinyproxy(self):
7272
f"No shiny-proxy pod with a 'release' label found in namespace {namespace}"
7373
)
7474

75-
print("/nFound a shiny pod: ", pod.metadata.name)
75+
print("\nFound a shiny pod: ", pod.metadata.name)
7676
sd = StatusData(namespace=namespace)
7777
# Simulate a k8s watch event shape
7878
t0 = time.time()

0 commit comments

Comments
 (0)