Skip to content

Commit

Permalink
publish duration for notebook and cell event failure events
Browse files Browse the repository at this point in the history
  • Loading branch information
fajpunk committed Dec 16, 2024
1 parent 95adec8 commit 28c9943
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
51 changes: 19 additions & 32 deletions src/mobu/services/business/notebookrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,54 +336,39 @@ async def execute_code(self, session: JupyterLabSession) -> None:
if not await self.execution_idle():
break
except:
await self._publish_notebook_failure()
await self._publish_notebook_event(
duration=sw.elapsed, success=False
)
raise

self.logger.info(f"Success running notebook {self._notebook.name}")
await self._publish_notebook_success(sw.elapsed)
await self._publish_notebook_event(
duration=sw.elapsed, success=True
)
if not self._notebook_paths:
self.logger.info("Done with this cycle of notebooks")
if self.stopping:
break

async def _publish_notebook_success(self, duration: timedelta) -> None:
async def _publish_notebook_event(
self, duration: timedelta, *, success: bool
) -> None:
await ed.events.notebook_execution.publish(
NotebookExecution(
**self.common_notebook_event_attrs(),
duration=duration,
success=True,
)
)

async def _publish_notebook_failure(self) -> None:
await ed.events.notebook_execution.publish(
NotebookExecution(
**self.common_notebook_event_attrs(),
duration=None,
success=False,
success=success,
)
)

async def _publish_cell_success(
self, *, cell_id: str, contents: str, duration: timedelta
async def _publish_cell_event(
self, *, cell_id: str, duration: timedelta, success: bool
) -> None:
await ed.events.notebook_cell_execution.publish(
NotebookCellExecution(
**self.common_notebook_event_attrs(),
duration=duration,
success=True,
cell_id=cell_id,
)
)

async def _publish_cell_failure(
self, *, cell_id: str, contents: str
) -> None:
await ed.events.notebook_cell_execution.publish(
NotebookCellExecution(
**self.common_notebook_event_attrs(),
duration=None,
success=False,
success=success,
cell_id=cell_id,
)
)
Expand Down Expand Up @@ -416,14 +401,16 @@ async def execute_cell(
try:
reply = await session.run_python(code, context=context)
except:
await self._publish_cell_failure(
cell_id=cell_id, contents=code
await self._publish_cell_event(
cell_id=cell_id,
duration=sw.elapsed,
success=False,
)
raise
self._running_code = None
self.logger.info(f"Result:\n{reply}\n")
await self._publish_cell_success(
cell_id=cell_id, contents=code, duration=sw.elapsed
await self._publish_cell_event(
cell_id=cell_id, duration=sw.elapsed, success=True
)

def dump(self) -> NotebookRunnerData:
Expand Down
2 changes: 1 addition & 1 deletion tests/business/notebookrunner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ async def test_alert(
# Check events
common = {
"business": "NotebookRunner",
"duration": None,
"duration": NOT_NONE,
"flock": "test",
"notebook": AnySearch("exception.ipynb"),
"repo": AnySearch("/notebooks"),
Expand Down

0 comments on commit 28c9943

Please sign in to comment.