clean snapshot creation

This commit is contained in:
mpostma 2021-03-22 16:51:53 +01:00
parent a85e7abb0c
commit 44dcfe29aa
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
2 changed files with 23 additions and 8 deletions

View File

@ -7,7 +7,7 @@ use tar::{Archive, Builder};
use crate::error::Error;
pub fn to_tar_gz(src: &Path, dest: &Path) -> Result<(), Error> {
pub fn to_tar_gz(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> Result<(), Error> {
let f = File::create(dest)?;
let gz_encoder = GzEncoder::new(f, Compression::default());
let mut tar_encoder = Builder::new(gz_encoder);

View File

@ -1,10 +1,12 @@
use std::path::PathBuf;
use std::time::Duration;
use std::fs::create_dir_all;
use anyhow::bail;
use tokio::fs;
use tokio::task::spawn_blocking;
use tokio::time::interval;
use uuid::Uuid;
use crate::helpers::compression;
use super::index_actor::IndexActorHandle;
use super::update_actor::UpdateActorHandle;
use super::uuid_resolver::UuidResolverHandle;
@ -45,15 +47,28 @@ impl<B> SnapshotService<B> {
}
async fn perform_snapshot(&self) -> anyhow::Result<()> {
let temp_snapshot_path = self
.snapshot_path
.join(format!("tmp-{}", Uuid::new_v4()));
create_dir_all(&temp_snapshot_path)?;
if self.snapshot_path.file_name().is_none() {
bail!("invalid snapshot file path");
}
let temp_snapshot_dir = spawn_blocking(move || tempfile::tempdir_in(".")).await??;
let temp_snapshot_path = temp_snapshot_dir.path().to_owned();
fs::create_dir_all(&temp_snapshot_path).await?;
let uuids = self.uuid_resolver_handle.snapshot(temp_snapshot_path.clone()).await?;
for uuid in uuids {
self.update_handle.snapshot(uuid, temp_snapshot_path.clone()).await?;
println!("performed snapshot for index {}", uuid);
}
let temp_snapshot_file = temp_snapshot_path.with_extension("temp");
let temp_snapshot_file_clone = temp_snapshot_file.clone();
let temp_snapshot_path_clone = temp_snapshot_path.clone();
spawn_blocking(move || compression::to_tar_gz(temp_snapshot_path_clone, temp_snapshot_file_clone)).await??;
fs::rename(temp_snapshot_file, &self.snapshot_path).await?;
Ok(())
}
}