use std::path::Path;
use ito_domain::backend::{
AllocateResult, ArchiveResult, ArtifactBundle, BackendArchiveClient, BackendLeaseClient,
BackendSyncClient, ClaimResult, PushResult, ReleaseResult,
};
use crate::backend_sync::map_backend_error;
use crate::errors::{CoreError, CoreResult};
pub fn claim_change(
lease_client: &dyn BackendLeaseClient,
change_id: &str,
) -> CoreResult<ClaimResult> {
lease_client
.claim(change_id)
.map_err(|e| map_backend_error(e, "claim"))
}
pub fn release_change(
lease_client: &dyn BackendLeaseClient,
change_id: &str,
) -> CoreResult<ReleaseResult> {
lease_client
.release(change_id)
.map_err(|e| map_backend_error(e, "release"))
}
pub fn allocate_change(lease_client: &dyn BackendLeaseClient) -> CoreResult<AllocateResult> {
lease_client
.allocate()
.map_err(|e| map_backend_error(e, "allocate"))
}
pub fn sync_pull(
sync_client: &dyn BackendSyncClient,
ito_path: &std::path::Path,
change_id: &str,
backup_dir: &std::path::Path,
) -> CoreResult<ArtifactBundle> {
crate::backend_sync::pull_artifacts(sync_client, ito_path, change_id, backup_dir)
}
pub fn sync_push(
sync_client: &dyn BackendSyncClient,
ito_path: &std::path::Path,
change_id: &str,
backup_dir: &std::path::Path,
) -> CoreResult<PushResult> {
crate::backend_sync::push_artifacts(sync_client, ito_path, change_id, backup_dir)
}
#[derive(Debug)]
pub struct BackendArchiveOutcome {
pub specs_updated: Vec<String>,
pub archive_name: String,
pub backend_result: ArchiveResult,
}
pub fn archive_with_backend(
sync_client: &dyn BackendSyncClient,
archive_client: &dyn BackendArchiveClient,
ito_path: &Path,
change_id: &str,
backup_dir: &Path,
skip_specs: bool,
) -> CoreResult<BackendArchiveOutcome> {
crate::backend_sync::pull_artifacts(sync_client, ito_path, change_id, backup_dir)?;
let specs_updated = if skip_specs {
Vec::new()
} else {
let spec_names = crate::archive::discover_change_specs(ito_path, change_id)?;
crate::archive::copy_specs_to_main(ito_path, change_id, &spec_names)?
};
let archive_name = crate::archive::generate_archive_name(change_id);
crate::archive::move_to_archive(ito_path, change_id, &archive_name)?;
let backend_result = archive_client
.mark_archived(change_id)
.map_err(|e| map_backend_error(e, "archive"))?;
Ok(BackendArchiveOutcome {
specs_updated,
archive_name,
backend_result,
})
}
pub fn is_backend_unavailable(err: &CoreError) -> bool {
match err {
CoreError::Process(msg) => msg.contains("Backend unavailable"),
_ => false,
}
}
#[cfg(test)]
#[path = "backend_coordination_tests.rs"]
mod backend_coordination_tests;