Skip to content

Commit

Permalink
Create Slack messages on nbexec failures
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansick committed Mar 11, 2024
1 parent 39ff808 commit e4f8e88
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/noteburst/worker/functions/nbexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, cast

from arq import Retry
from safir.slack.blockkit import SlackCodeBlock, SlackTextField

from noteburst.exceptions import NbexecTaskError
from noteburst.jupyterclient.jupyterlab import JupyterClient, JupyterError
Expand Down Expand Up @@ -64,11 +65,64 @@ async def nbexec(
"Authentication error to Jupyter. Forcing worker shutdown",
jupyter_status=e.status,
)

if "slack" in ctx and "slack_message_factory" in ctx:
slack_client = ctx["slack"]
message = ctx["slack_message_factory"](
"Noteburst worker shutting down due to Jupyter "
"authentication error during nbexec."
)
message.blocks.append(
SlackCodeBlock(heading="Exception", code=str(e))
)
message.fields.append(
SlackTextField(
heading="Jupyter response", text=str(e.status)
)
)
message.fields.append(
SlackTextField(
heading="Job ID", text=ctx.get("job_id", "unknown")
)
)
message.fields.append(
SlackTextField(
heading="Attempt", text=ctx.get("job_try", "unknown")
)
)
await slack_client.post(message)

sys.exit("400 class error from Jupyter")
elif enable_retry:
logger.warning("nbexec triggering retry")
raise Retry(defer=ctx["job_try"] * 5) from None
else:
if "slack" in ctx and "slack_message_factory" in ctx:
slack_client = ctx["slack"]
message = ctx["slack_message_factory"]("Nbexec failed.")
message.blocks.append(
SlackCodeBlock(heading="Exception", code=str(e))
)
message.fields.append(
SlackTextField(
heading="Jupyter response", text=str(e.status)
)
)
message.fields.append(
SlackTextField(
heading="Job ID", text=ctx.get("job_id", "unknown")
)
)
message.fields.append(
SlackTextField(
heading="Attempt", text=ctx.get("job_try", "unknown")
)
)
message.blocks.append(
SlackCodeBlock(heading="Notebook", code=ipynb)
)
await slack_client.post(message)

raise NbexecTaskError.from_exception(e) from e

return execution_result.model_dump_json()

0 comments on commit e4f8e88

Please sign in to comment.