use rho_protocol::{
ErrorCode, RpcError, SessionInspectParams, SessionInspectResponse, SessionReport,
};
use super::Sessions;
use crate::app::daemon_host::jobs::SessionJob;
impl Sessions {
pub(in crate::app::daemon_host) async fn inspect(
&self,
params: SessionInspectParams,
) -> Result<SessionInspectResponse, RpcError> {
self.refuse_when_stopping()?;
let not_loaded = || {
RpcError::new(
ErrorCode::SessionNotLoaded,
"the daemon has not loaded the session; attach to it first",
)
};
let session = self
.loaded_session(¶ms.session)
.await?
.ok_or_else(not_loaded)?;
match self.jobs.session_job(&session.id) {
SessionJob::Busy(job) if job == session.job => {
return Err(RpcError::new(
ErrorCode::SessionBusy,
"the session is in a turn; ask again once it ends",
));
}
SessionJob::Busy(job) => {
return Err(RpcError::new(
ErrorCode::SessionBusy,
format!(
"job {job} is writing session {}; ask again once it ends",
session.id
),
));
}
SessionJob::New | SessionJob::Last(_) => {}
}
let runtime = session.runtime.lock().await;
let runtime = runtime.as_ref().ok_or_else(not_loaded)?;
let report = match params.report {
SessionReport::Context => serde_json::to_value(runtime.context_report()),
SessionReport::Compaction => {
serde_json::to_value(runtime.compaction_report(&session.config()))
}
SessionReport::ProviderContext => {
let auths = super::options::available_auths(runtime.credentials()).await;
serde_json::to_value(runtime.provider_context_report(&session.config(), &auths))
}
SessionReport::Hooks => {
let report = runtime.reload_hooks().map_err(|error| {
RpcError::new(ErrorCode::InternalError, format!("{error:#}"))
})?;
serde_json::to_value(report)
}
_ => {
return Err(RpcError::new(
ErrorCode::InvalidParams,
"this daemon does not know the report",
))
}
};
report
.map(|report| SessionInspectResponse { report })
.map_err(|error| RpcError::new(ErrorCode::InternalError, error.to_string()))
}
}
#[cfg(test)]
#[path = "inspect_tests.rs"]
mod tests;