Skip to content

Commit

Permalink
proper shutdown and progress reports
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jul 11, 2022
1 parent f949d23 commit bd5156e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 45 deletions.
84 changes: 48 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,4 @@ vendored = [
nightly = ["pgp/nightly"]

[patch.crates-io]
chacha20poly1305 = { git = "https://github.com/dignifiedquire/AEADs", branch = "chacha20-fix" }
chacha20 = { git = "https://github.com/dignifiedquire/stream-ciphers", branch = "chacha20-fix" }
libp2p = { git = "https://github.com/dignifiedquire/rust-libp2p", branch = "feat-kad-count" }
2 changes: 1 addition & 1 deletion examples/repl/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
let file = dir.join("qr.svg");
tokio::fs::write(file, qr_code.as_bytes()).await?;

tokio::time::sleep(std::time::Duration::from_secs(100)).await;
transfer.done().await?;
sender.close().await?;
}
"receive-backup" => {
Expand Down
37 changes: 31 additions & 6 deletions src/imex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,33 @@ pub async fn receive_backup_inner(
let receiver = Receiver::new(port, rpc_p2p_port, rpc_store_port, &sender_db)
.await
.context("failed to create sender")?;
let receiver_transfer = receiver
let mut receiver_transfer = receiver
.transfer_from_ticket(&ticket)
.await
.context("failed to read transfer")?;
let data = receiver_transfer.recv().await?;
let progress = receiver_transfer.progress()?;

// progress report
let ctx = context.clone();
let progress_task = tokio::spawn(async move {
let mut last_progress = 0;
while let Ok(ev) = progress.recv().await {
match ev {
Ok(iroh_share::ProgressEvent::Piece { index, total }) => {
let progress = 1000 * index / total;
if progress != last_progress && progress < 1000 {
ctx.emit_event(EventType::ImexProgress(progress));
last_progress = progress;
}
}
Err(err) => {
error!(ctx, "IMEX receive backup failed to complete: {}", err);
ctx.emit_event(EventType::ImexProgress(0));
}
}
}
});

let out = context.get_blobdir();

Expand Down Expand Up @@ -199,6 +221,9 @@ pub async fn receive_backup_inner(
}

println!("Received all data, written to: {}", out.display());
receiver.close().await?;
progress_task.await?;

Ok(())
}

Expand Down Expand Up @@ -782,8 +807,10 @@ async fn export_backup_iroh(
// get a fine backup file name (the name includes the date so that multiple backup instances are possible)
let now = time();
let (temp_db_path, temp_path, dest_path) = get_next_backup_path(dir, now)?;
let sender_db_path = dir.join("iroh_db");
let _d1 = DeleteOnDrop(temp_db_path.clone());
let _d2 = DeleteOnDrop(temp_path.clone());
let _d3 = DeleteOnDrop(sender_db_path.clone());

context
.sql
Expand Down Expand Up @@ -823,14 +850,12 @@ async fn export_backup_iroh(
let port = 9990;
let rpc_p2p_port = 5550;
let rpc_store_port = 5560;
// TODO: not tempfile
let sender_dir = tempfile::tempdir().unwrap();
// TODO: cleanup
let sender_db = sender_dir.into_path().join("db");

let sender =
iroh_share::Sender::new(port, rpc_p2p_port, rpc_store_port, &sender_db).await?;
iroh_share::Sender::new(port, rpc_p2p_port, rpc_store_port, &sender_db_path)
.await?;
let transfer = sender.transfer_from_dir_builder(dir_builder).await?;

Ok((sender, transfer))
}
Err(e) => {
Expand Down

0 comments on commit bd5156e

Please sign in to comment.