"retry-limit" CLI param
This commit is contained in:
parent
a4bef827d1
commit
4eb0627a5f
3 changed files with 21 additions and 2 deletions
1
notes.md
1
notes.md
|
|
@ -46,7 +46,6 @@
|
||||||
# Ideas
|
# Ideas
|
||||||
|
|
||||||
- cli functions
|
- cli functions
|
||||||
- max retries => stop
|
|
||||||
- "continue" and "new" flags to avoid user interaction
|
- "continue" and "new" flags to avoid user interaction
|
||||||
- "quiet" flag to disable output entirely
|
- "quiet" flag to disable output entirely
|
||||||
- "verbose" flag to adjust RUST_LOG for `shrupl` crate
|
- "verbose" flag to adjust RUST_LOG for `shrupl` crate
|
||||||
|
|
|
||||||
12
src/cli.rs
12
src/cli.rs
|
|
@ -36,6 +36,10 @@ pub struct Cli {
|
||||||
)]
|
)]
|
||||||
protocol: String,
|
protocol: String,
|
||||||
|
|
||||||
|
/// Number of times actions are retried
|
||||||
|
#[arg(short, long, default_value_t = 5, value_name = "N")]
|
||||||
|
retry_limit: u32,
|
||||||
|
|
||||||
/// Name of the new share
|
/// Name of the new share
|
||||||
#[arg(short, long, default_value = "ShrUpl Upload", value_name = "TEXT")]
|
#[arg(short, long, default_value = "ShrUpl Upload", value_name = "TEXT")]
|
||||||
name: String,
|
name: String,
|
||||||
|
|
@ -71,6 +75,7 @@ impl fmt::Debug for Cli {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.debug_struct("Cli")
|
f.debug_struct("Cli")
|
||||||
.field("uri", &self.get_uri())
|
.field("uri", &self.get_uri())
|
||||||
|
.field("retry_limit", &self.retry_limit)
|
||||||
.field("alias", &self.alias)
|
.field("alias", &self.alias)
|
||||||
.field("timeout", &self.get_timeout())
|
.field("timeout", &self.get_timeout())
|
||||||
.field("chunk_size", &self.chunk_size)
|
.field("chunk_size", &self.chunk_size)
|
||||||
|
|
@ -98,6 +103,13 @@ impl Cli {
|
||||||
Uri::new(&self.protocol, &self.url)
|
Uri::new(&self.protocol, &self.url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn may_retry(&self, tries: u32) -> bool {
|
||||||
|
match self.retry_limit {
|
||||||
|
0 => true,
|
||||||
|
limit => tries < limit,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_share_request(&self) -> NewShareRequest {
|
pub fn get_share_request(&self) -> NewShareRequest {
|
||||||
NewShareRequest::new(&self.name, self.description.as_ref(), self.max_views)
|
NewShareRequest::new(&self.name, self.description.as_ref(), self.max_views)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
src/main.rs
10
src/main.rs
|
|
@ -73,8 +73,13 @@ fn main() {
|
||||||
println!("{} is uploading: {fns_magenta}", *SHRUPL);
|
println!("{} is uploading: {fns_magenta}", *SHRUPL);
|
||||||
|
|
||||||
let mut buffer = vec![0; args.chunk_size * 1024 * 1024];
|
let mut buffer = vec![0; args.chunk_size * 1024 * 1024];
|
||||||
|
let mut tries = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
if !args.may_retry(tries) {
|
||||||
|
Log::error("Retry limit reached!");
|
||||||
|
}
|
||||||
|
|
||||||
match state.upload_chunk(&mut buffer) {
|
match state.upload_chunk(&mut buffer) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
// TODO better error handling (this will just retry endlessly)
|
// TODO better error handling (this will just retry endlessly)
|
||||||
|
|
@ -82,7 +87,9 @@ fn main() {
|
||||||
Log::handle(&e);
|
Log::handle(&e);
|
||||||
|
|
||||||
if let Some(s) = state.rewind() {
|
if let Some(s) = state.rewind() {
|
||||||
trace!("State rewound, retrying last chunk");
|
tries += 1;
|
||||||
|
trace!("State rewound, retrying last chunk (tried: {tries})");
|
||||||
|
|
||||||
state = s;
|
state = s;
|
||||||
} else {
|
} else {
|
||||||
Log::error("Failed to retry chunk!");
|
Log::error("Failed to retry chunk!");
|
||||||
|
|
@ -90,6 +97,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
Ok(false) => {
|
Ok(false) => {
|
||||||
trace!("chunk uploaded");
|
trace!("chunk uploaded");
|
||||||
|
tries = 0;
|
||||||
}
|
}
|
||||||
Ok(true) => {
|
Ok(true) => {
|
||||||
info!("all uploads done");
|
info!("all uploads done");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue