[wip] retry chunks

- `AppState::rewind` impl
- error handling in `main`
This commit is contained in:
Jörn-Michael Miehe 2025-06-13 23:00:36 +00:00
parent 0a8e5cf3f0
commit 30855ed8ff
3 changed files with 36 additions and 7 deletions

View file

@ -11,7 +11,7 @@ use log::{debug, warn};
use super::{ use super::{
cachefile::CacheFile, cachefile::CacheFile,
cli::Cli, cli::Cli,
file::{self, FileTrait, Chunk}, file::{self, Chunk, FileTrait},
sharry::{self, Client}, sharry::{self, Client},
}; };
@ -162,6 +162,18 @@ impl AppState {
Ok(self.is_done()) Ok(self.is_done())
} }
pub fn rewind(mut self) -> Option<Self> {
let Some(uploading) = self.inner.pop_file(&self.http) else {
warn!("rewind called on empty queue");
return None;
};
let uploading = uploading.rewind()?;
self.inner.push_file(uploading);
Some(self)
}
pub fn file_names(&self) -> Vec<&str> { pub fn file_names(&self) -> Vec<&str> {
self.inner.file_names() self.inner.file_names()
} }

View file

@ -4,6 +4,7 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
use log::warn;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::{Chunk, FileTrait}; use super::{Chunk, FileTrait};
@ -45,11 +46,17 @@ impl Uploading {
} }
pub fn rewind(self) -> Option<Self> { pub fn rewind(self) -> Option<Self> {
self.last_offset.map(|last_offset| Self { match self.last_offset {
last_offset: None, Some(last_offset) => Some(Self {
offset: last_offset, last_offset: None,
..self offset: last_offset,
}) ..self
}),
None => {
warn!("attempted to rewind twice");
None
}
}
} }
pub fn read<'t>(&mut self, buf: &'t mut [u8]) -> io::Result<Chunk<'t>> { pub fn read<'t>(&mut self, buf: &'t mut [u8]) -> io::Result<Chunk<'t>> {

View file

@ -117,7 +117,17 @@ fn main() {
loop { loop {
match state.upload_chunk(&mut buffer) { match state.upload_chunk(&mut buffer) {
Err(e) => error!("error: {e:?}"), // HACK handle errors better Err(e) => {
// HACK handle errors better
error!("error: {e:?}");
if let Some(s) = state.rewind() {
state = s;
} else {
eprintln!("{} Failed to retry chunk!", style("Error:").red().bold());
process::exit(1);
};
}
Ok(true) => { Ok(true) => {
info!("all uploads done"); info!("all uploads done");
state.clear().unwrap_or_else(|e| { state.clear().unwrap_or_else(|e| {