fix: report verified orbit capture outcomes
This commit is contained in:
@@ -6,16 +6,32 @@ use tessera_core::manifest::SnapshotManifest;
|
||||
use tessera_providers::provider_from_config;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn run_orbit_internal(state: HubState, orbit_id: Uuid) -> Result<()> {
|
||||
pub async fn run_orbit_internal(state: HubState, orbit_id: Uuid) -> Result<Option<Uuid>> {
|
||||
let orbits = state.db.list_orbits().await?;
|
||||
let orbit = orbits
|
||||
.into_iter()
|
||||
.find(|o| o.id == orbit_id)
|
||||
.context("orbit not found")?;
|
||||
run_orbit_row(state, orbit).await
|
||||
anyhow::ensure!(
|
||||
state.active_orbits.lock().await.insert(orbit_id),
|
||||
"orbit already running"
|
||||
);
|
||||
|
||||
// Keep capture and its status update together if the requesting client disconnects.
|
||||
tokio::spawn(async move {
|
||||
let result = run_orbit_row(&state, orbit).await;
|
||||
if result.is_err() {
|
||||
if let Err(error) = state.db.set_orbit_status(orbit_id, "failed").await {
|
||||
tracing::error!(%orbit_id, %error, "could not record failed capture");
|
||||
}
|
||||
}
|
||||
state.active_orbits.lock().await.remove(&orbit_id);
|
||||
result
|
||||
})
|
||||
.await?
|
||||
}
|
||||
|
||||
pub async fn run_orbit_row(state: HubState, orbit: OrbitRow) -> Result<()> {
|
||||
async fn run_orbit_row(state: &HubState, orbit: OrbitRow) -> Result<Option<Uuid>> {
|
||||
state.db.set_orbit_status(orbit.id, "running").await?;
|
||||
|
||||
let provider_kind = match orbit.provider.as_str() {
|
||||
@@ -42,13 +58,16 @@ pub async fn run_orbit_row(state: HubState, orbit: OrbitRow) -> Result<()> {
|
||||
.snapshot(manifest, &capture.payload, orbit.dry_run)
|
||||
.context("vault snapshot")?;
|
||||
|
||||
if !orbit.dry_run {
|
||||
let _pruned = vault.prune_with_decay(&orbit.decay_preset)?;
|
||||
let retained_snapshot = if orbit.dry_run {
|
||||
None
|
||||
} else {
|
||||
let pruned = vault.prune_orbit_with_decay(orbit.id, &orbit.decay_preset)?;
|
||||
state
|
||||
.db
|
||||
.audit("decay.pruned", &format!("orbit={}", orbit.name))
|
||||
.await?;
|
||||
}
|
||||
(!pruned.contains(&snap.id)).then_some(snap.id)
|
||||
};
|
||||
|
||||
let status = if orbit.dry_run {
|
||||
"rehearsed"
|
||||
@@ -67,5 +86,5 @@ pub async fn run_orbit_row(state: HubState, orbit: OrbitRow) -> Result<()> {
|
||||
&format!("snapshot={}", snap.id),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
Ok(retained_snapshot)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ use crate::db::OrbitRow;
|
||||
use crate::state::{HubState, PairingEntry};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::{header, StatusCode, Uri},
|
||||
http::{header, HeaderMap, HeaderValue, StatusCode, Uri},
|
||||
response::IntoResponse,
|
||||
routing::{delete, get, post},
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use base64::Engine;
|
||||
@@ -49,6 +49,7 @@ struct ApiDoc;
|
||||
#[derive(serde::Serialize, utoipa::ToSchema)]
|
||||
struct SnapshotSummary {
|
||||
id: Uuid,
|
||||
orbit_id: Uuid,
|
||||
taken_at: chrono::DateTime<chrono::Utc>,
|
||||
orbit_name: String,
|
||||
size_bytes: u64,
|
||||
@@ -147,6 +148,7 @@ async fn list_snapshots(State(state): State<HubState>) -> Result<Json<Vec<Snapsh
|
||||
.into_iter()
|
||||
.map(|s| SnapshotSummary {
|
||||
id: s.id,
|
||||
orbit_id: s.manifest.orbit_id,
|
||||
taken_at: s.taken_at,
|
||||
orbit_name: s.manifest.orbit_name,
|
||||
size_bytes: s.size_bytes,
|
||||
@@ -186,15 +188,24 @@ async fn create_orbit(
|
||||
Ok(Json(row))
|
||||
}
|
||||
|
||||
#[utoipa::path(post, path = "/api/v1/orbits/{id}/run", params(("id" = Uuid, Path, description = "orbit id")), responses((status = 204)))]
|
||||
#[utoipa::path(post, path = "/api/v1/orbits/{id}/run", params(("id" = Uuid, Path, description = "orbit id")), responses((status = 204, headers(("X-Tessera-Snapshot-Id" = Uuid, description = "New retained snapshot ID when a capture writes to the vault")))))]
|
||||
async fn trigger_orbit(
|
||||
State(state): State<HubState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<StatusCode, StatusCode> {
|
||||
crate::api::run_orbit_internal(state, id)
|
||||
) -> Result<(StatusCode, HeaderMap), StatusCode> {
|
||||
let snapshot_id = crate::api::run_orbit_internal(state, id)
|
||||
.await
|
||||
.map(|_| StatusCode::NO_CONTENT)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.map_err(|error| {
|
||||
tracing::warn!(orbit_id = %id, %error, "capture did not complete");
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})?;
|
||||
let mut headers = HeaderMap::new();
|
||||
if let Some(snapshot_id) = snapshot_id {
|
||||
let value = HeaderValue::from_str(&snapshot_id.to_string())
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
headers.insert("x-tessera-snapshot-id", value);
|
||||
}
|
||||
Ok((StatusCode::NO_CONTENT, headers))
|
||||
}
|
||||
|
||||
#[utoipa::path(post, path = "/api/v1/reconstitute", request_body = ReconstituteRequest, responses((status = 200, body = ReconstituteStartResponse)))]
|
||||
@@ -230,21 +241,14 @@ async fn start_reconstitute(
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
}
|
||||
let aborted = {
|
||||
let map = state_bg.reconstitute_sessions.read().await;
|
||||
map.get(&session_id)
|
||||
.map(|s| s.state == ReconstituteState::Aborted)
|
||||
.unwrap_or(true)
|
||||
};
|
||||
if aborted {
|
||||
return;
|
||||
}
|
||||
{
|
||||
let mut map = state_bg.reconstitute_sessions.write().await;
|
||||
if let Some(s) = map.get_mut(&session_id) {
|
||||
s.state = ReconstituteState::Restoring;
|
||||
s.seconds_remaining = 0;
|
||||
let Some(s) = map.get_mut(&session_id) else { return };
|
||||
if s.state == ReconstituteState::Aborted {
|
||||
return;
|
||||
}
|
||||
s.state = ReconstituteState::Restoring;
|
||||
s.seconds_remaining = 0;
|
||||
}
|
||||
let dest = std::path::PathBuf::from(&target);
|
||||
let result = {
|
||||
|
||||
Reference in New Issue
Block a user