waratah/notebook.rs
1//! Notebook sidecar session owned by `waratah start`.
2
3use std::sync::Arc;
4
5use crate::dev::DevOptions;
6use crate::error::DevError;
7
8/// Running notebook sidecar: the live state shared with the HTTP surface,
9/// plus the supervisor handle. Its `stop()` is the clean path (sidecar
10/// SIGTERM → kernel cleanup → group SIGKILL); a bare Drop hard-kills the
11/// group and sweeps any surviving kernels via their `JPY_PARENT_PID` env.
12pub(crate) struct NotebookSession {
13 /// Readiness/token view used by the `/jupyter` proxy and the
14 /// `/waratah/notebooks` + cluster capability endpoints.
15 pub(crate) state: Arc<waratah_notebook::NotebookState>,
16 supervisor: waratah_notebook::SupervisorHandle,
17}
18
19impl NotebookSession {
20 /// Start the sidecar supervisor when the feature is enabled.
21 ///
22 /// Only cheap validation runs inline (interpreter present, Python 3.11+);
23 /// venv provisioning and readiness happen in the background, so engine
24 /// startup is not gated on slow Python work. `None` when disabled.
25 pub(crate) fn start(options: &DevOptions) -> Result<Option<Self>, DevError> {
26 let Some(notebooks) = &options.notebooks else {
27 return Ok(None);
28 };
29 let state = Arc::new(waratah_notebook::NotebookState::new(
30 notebooks.token.clone(),
31 notebooks.repo.clone(),
32 notebooks.bind,
33 ));
34 let supervisor =
35 waratah_notebook::SupervisorHandle::spawn(notebooks.clone(), state.clone())
36 .map_err(DevError::Notebooks)?;
37 println!(
38 "notebook sidecar starting (python {})",
39 notebooks
40 .python
41 .as_ref()
42 .map(|path| path.display().to_string())
43 .unwrap_or_else(|| "auto-provisioned venv".to_owned())
44 );
45 Ok(Some(Self { state, supervisor }))
46 }
47
48 /// Stop the sidecar process group (kernels included) and reap.
49 pub(crate) async fn stop(self) {
50 self.supervisor.shutdown().await;
51 }
52}