2021-03-17 18:53:23 +08:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::time::Duration;
|
2021-03-20 03:08:00 +08:00
|
|
|
use std::fs::create_dir_all;
|
2021-03-17 18:53:23 +08:00
|
|
|
|
|
|
|
use tokio::time::interval;
|
2021-03-20 03:08:00 +08:00
|
|
|
use uuid::Uuid;
|
2021-03-17 18:53:23 +08:00
|
|
|
|
|
|
|
use super::index_actor::IndexActorHandle;
|
|
|
|
use super::update_actor::UpdateActorHandle;
|
|
|
|
use super::uuid_resolver::UuidResolverHandle;
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct SnapshotService<B> {
|
|
|
|
index_handle: IndexActorHandle,
|
|
|
|
uuid_resolver_handle: UuidResolverHandle,
|
|
|
|
update_handle: UpdateActorHandle<B>,
|
|
|
|
snapshot_period: Duration,
|
|
|
|
snapshot_path: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B> SnapshotService<B> {
|
|
|
|
pub fn new(
|
|
|
|
index_handle: IndexActorHandle,
|
|
|
|
uuid_resolver_handle: UuidResolverHandle,
|
|
|
|
update_handle: UpdateActorHandle<B>,
|
|
|
|
snapshot_period: Duration,
|
|
|
|
snapshot_path: PathBuf,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
index_handle,
|
|
|
|
uuid_resolver_handle,
|
|
|
|
update_handle,
|
|
|
|
snapshot_period,
|
|
|
|
snapshot_path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(self) {
|
|
|
|
let mut interval = interval(self.snapshot_period);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
interval.tick().await;
|
2021-03-20 03:08:00 +08:00
|
|
|
self.perform_snapshot().await.unwrap();
|
2021-03-17 18:53:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 03:08:00 +08:00
|
|
|
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)?;
|
|
|
|
let uuids = self.uuid_resolver_handle.snapshot(temp_snapshot_path.clone()).await?;
|
|
|
|
let index_snapshot = self.index_handle.snapshot(uuids.clone(), temp_snapshot_path.clone());
|
|
|
|
let updates_snapshot = self.update_handle.snapshot(uuids.clone(), temp_snapshot_path.clone());
|
|
|
|
let (first, second) = tokio::join!(updates_snapshot, index_snapshot);
|
|
|
|
println!("results: {:?}, {:?}", first, second);
|
|
|
|
Ok(())
|
2021-03-17 18:53:23 +08:00
|
|
|
}
|
|
|
|
}
|