Skip to content

Commit 99d1794

Browse files
committed
Use a specific exception for worker start-up
This lets the exception class deal with sorting into tags and context for Sentry.
1 parent b25f1c3 commit 99d1794

2 files changed

Lines changed: 42 additions & 15 deletions

File tree

src/noteburst/exceptions.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"NoteburstClientRequestError",
1616
"NoteburstError",
1717
"NoteburstWorkerError",
18+
"NoteburstWorkerStartupError",
1819
"TaskError",
1920
]
2021

@@ -38,6 +39,38 @@ def __init__(
3839
self.contexts = contexts
3940

4041

42+
class NoteburstWorkerStartupError(NoteburstWorkerError):
43+
"""Error raised when the worker fails to start."""
44+
45+
def __init__(
46+
self,
47+
msg: str,
48+
*,
49+
last_username: str,
50+
attempted_usernames: list[str],
51+
image_selector: str,
52+
image_reference: str | None,
53+
user_token_scopes: list[str],
54+
) -> None:
55+
super().__init__(
56+
msg,
57+
tags={
58+
"username": last_username,
59+
"image_selector": image_selector,
60+
"image_reference": image_reference or "N/A",
61+
},
62+
contexts={
63+
"noteburst_worker": {
64+
"username": last_username,
65+
"attempted_usernames": attempted_usernames,
66+
"user_token_scopes": user_token_scopes,
67+
"image_selector": image_selector,
68+
"image_reference": image_reference or "N/A",
69+
}
70+
},
71+
)
72+
73+
4174
class TaskError(Exception):
4275
"""Error related to Arq task execution."""
4376

src/noteburst/worker/main.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
WorkerConfig,
2929
WorkerKeepAliveSetting,
3030
)
31-
from noteburst.exceptions import NoteburstWorkerError
31+
from noteburst.exceptions import NoteburstWorkerStartupError
3232

3333
from .functions import keep_alive, nbexec, ping, run_python
3434
from .identity import IdentityClaimError, IdentityManager
@@ -85,8 +85,10 @@ async def startup(ctx: dict[Any, Any]) -> None:
8585

8686
# Loop with different identities until we get a successful spawn
8787
spawn_exception: Exception | None = None
88+
attempted_usernames: list[str] = []
8889
while True:
8990
try:
91+
attempted_usernames.append(identity.username)
9092
nublado_pod = await NubladoPod.spawn(
9193
identity=identity,
9294
nublado_image=config.nublado_image,
@@ -117,22 +119,14 @@ async def startup(ctx: dict[Any, Any]) -> None:
117119
identity = await identity_manager.get_next_identity(identity)
118120
except IdentityClaimError:
119121
# No more identities available, so we can't spawn a pod
120-
raise NoteburstWorkerError(
122+
raise NoteburstWorkerStartupError(
121123
"Failed to start up Noteburst worker. Could not spawn a "
122124
"Nublado pod with any identity.",
123-
tags={
124-
"username": identity.username,
125-
"image_selector": config.image_selector,
126-
"image_reference": config.image_reference or "N/A",
127-
},
128-
contexts={
129-
"nublado": {
130-
"username": identity.username,
131-
"user_token_scopes": config.parsed_worker_token_scopes,
132-
"image_selector": config.image_selector,
133-
"image_reference": config.image_reference,
134-
}
135-
},
125+
last_username=identity.username,
126+
attempted_usernames=attempted_usernames,
127+
image_selector=config.image_selector,
128+
image_reference=config.image_reference,
129+
user_token_scopes=config.parsed_worker_token_scopes,
136130
) from spawn_exception
137131

138132
ctx["nublado_client"] = nublado_pod.nublado_client

0 commit comments

Comments
 (0)