Skip to content

Commit

Permalink
When necessary, retry downloading reads from ENA
Browse files Browse the repository at this point in the history
  • Loading branch information
stitam committed Apr 2, 2024
1 parent 403dd82 commit c386b45
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 8 additions & 2 deletions R/ena_download_reads.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ ena_download_reads <- function(
next()
}
webseq_sleep(type = "FTP")
out <- try(utils::download.file(
ftpfiles[i], destfile = localfile, quiet = TRUE), silent = TRUE)
out <- try(retry(
utils::download.file(
ftpfiles[i],
destfile = localfile,
quiet = TRUE
),
times = 5
), silent = TRUE)
if (inherits(out, "try-error")) {
if (verbose) {
if (i < length(ftpfiles)) {
Expand Down
20 changes: 20 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
#' Retry a function call
#'
#' @param expr expression; Expression to evaluate.
#' @param times integer; Number of times to retry.
#' @noRd
retry <- function(expr, times = 5) {
if (!is.numeric(times) || times < 1) stop("times must be a positive integer.")
attempt <- 1
while (attempt <= times) {
result <- try(expr, silent = TRUE)
if (inherits(result, "try-error")) {
Sys.sleep(attempt ^ 2)
attempt <- attempt + 1
} else {
return(result)
}
}
stop()
}

#' Try an URL with default parameters
#'
#' This is a convenience wrapper for trying URLs
Expand Down

0 comments on commit c386b45

Please sign in to comment.