Downloader provides a general purpose file download system with:
File status management (missing, downloading, downloaded, verified)
Incremental checksum verification during download
Resume capability for interrupted downloads
Async and parallel download using mirai
Progress tracking
Error handling and retry logic
data_dirThe final data directory
tmp_dirThe temporary files directory
max_retriesMaximum number of retry attempts
timeoutDownload timeout in seconds
network_policyNetwork options passed to libcurl.
node_policyData-node cooldown and ranking policy.
transfer_policyCurl transfer policy.
resource_policyLocal resource and scheduling policy.
n_workersNumber of parallel workers
manifestPersistent download manifest path, or NULL.
configCurrent downloader configuration as a named list. When
manifest is set, the configuration is stored in the
manifest's download_config table.
new()Create a new Downloader object
Downloader$new(
dest = NULL,
temp = NULL,
retries = 3L,
timeout = 3600L,
ssl_verifypeer = TRUE,
proxy = NULL,
connect_timeout = NULL,
useragent = NULL,
cleanup = TRUE,
n_workers = 4L,
node_policy = NULL,
transfer_policy = NULL,
resource_policy = NULL,
manifest = NULL
)destA string specifying the directory for final
downloaded files. If NULL, uses a temporary directory. Default: NULL.
tempA string specifying the directory for temporary files
(.part, .done). If NULL, uses dest/.tmp. Should ideally
be on the same filesystem as dest for atomic rename
operations. Default: NULL.
retriesA positive integer specifying the maximum number of
retry attempts for failed downloads. Default: 3L.
timeoutA positive integer specifying the timeout in seconds for
each download. Default: 3600L (1 hour).
ssl_verifypeerWhether to verify HTTPS certificates. Default:
TRUE.
proxyOptional proxy URL passed to libcurl. Default: NULL.
connect_timeoutOptional connection timeout in seconds passed
to libcurl. Default: NULL.
useragentOptional HTTP user agent passed to libcurl.
Default: NULL.
cleanupA logical value specifying whether to automatically clean up failed temporary
files. Default: TRUE.
n_workersA non-negative integer specifying the number of parallel workers
for async downloads. If 0, async downloads will fallback to synchronous mode.
Default: 4L.
node_policyA list controlling historical data-node cooldown and ranking. Missing fields use conservative defaults.
transfer_policyA list controlling curl transfer options and
optional experimental Range-piece downloads. Supported curl
fields are chunk_size, bandwidth_limit, low_speed_limit,
and low_speed_time. Range fields are range_mode
("off", "single", "multi", or "auto"), piece_size,
piece_concurrency, max_sources,
require_checksum_for_multisource, and range_probe_timeout.
The default range_mode = "off" keeps the existing streaming
download behavior.
resource_policyA list controlling local resource checks and
scheduling. Supported fields are host_concurrency,
disk_preflight, and min_free_space.
manifestOptional DuckDB manifest path for persistent
sessions, tasks, candidate URLs, and events. If NULL, only
the single-file shortcut API is available. Default: NULL.
download()Download a single file with state management and resume support
Downloader$download(
url,
filename = NULL,
subdir = NULL,
progress = TRUE,
overwrite = FALSE,
checksum = NULL,
checksum_type = "sha256",
resume = TRUE,
block = TRUE,
.tmp_id = NULL
)urlA string specifying the URL to download from.
filenameA string specifying the filename for the downloaded file. If NULL, uses
filename from URL. Default: NULL.
subdirA string specifying the subdirectory within dest to save file.
Default: NULL (save directly in dest).
progressA logical value specifying whether to show progress bar. Default: TRUE.
overwriteA logical value specifying whether to overwrite existing file. Default: FALSE.
checksumA string specifying the expected checksum for verification. If provided,
enables incremental checksum calculation. Default: NULL.
checksum_typeA string specifying the checksum type ("sha256" or "md5"). Default: "sha256".
resumeA logical value specifying whether to resume interrupted downloads.
Default: TRUE.
blockA logical value specifying whether to block until download completes.
If FALSE, downloads asynchronously in background. Default: TRUE.
.tmp_idInternal temporary file ID used by persistent
download tasks. Default: NULL.
If block = TRUE, returns the path to the downloaded file.
If block = FALSE, returns a task ID for tracking the download.
\dontrun{
# Blocking download
path <- dl$download(url = "https://example.com/data.nc")
# Async download
task_id <- dl$download(
url = "https://example.com/data.nc",
block = FALSE
)
dl$wait_for_tasks(task_id)
# Multiple files (async batch)
urls <- c("https://example.com/file1.nc", "https://example.com/file2.nc")
task_ids <- sapply(urls, function(url) {
dl$download(url = url, block = FALSE)
})
results <- dl$wait_for_tasks(task_ids)
}
enqueue()Add a download plan to the persistent manifest.
preflight()Check local resource requirements before downloading.
run()Run queued persistent download tasks.
Downloader$run(
session_id = NULL,
task_id = NULL,
block = TRUE,
progress = TRUE,
overwrite = FALSE,
resume = TRUE
)session_idOptional session ID.
task_idOptional task ID vector.
blockWhether to block until completion. If FALSE, creates
a detached background job via $start().
progressWhether to show per-file progress.
overwriteWhether to overwrite existing final files.
resumeWhether to resume .part files.
start()Start a persistent download session in the background.
Downloader$start(
session_id = NULL,
task_id = NULL,
overwrite = FALSE,
resume = TRUE,
mode = c("process", "daemon"),
store_path = NULL
)session_idOptional session ID.
task_idOptional task ID vector.
overwriteWhether to overwrite existing final files.
resumeWhether to resume .part files.
modeBackground execution mode. "process" starts a detached
Rscript; "daemon" submits the job to a running downloader
daemon.
store_pathOptional EsgStore path to sync after completion.
daemon_start()Start a persistent downloader daemon.
status()Return persistent download task status.
events()Return persistent downloader event logs.
record_probes()Record URL probe outcomes from a download plan into node history.
retry()Requeue failed or cancelled persistent tasks.
Downloader$retry(
session_id = NULL,
task_id = NULL,
status = c("error", "cancelled")
)cancel()Cancel queued or in-progress persistent download tasks.
Downloader$cancel(
session_id = NULL,
task_id = NULL,
status = c("queued", "downloading")
)resume()Resume queued or interrupted persistent tasks.
cleanup_tmp()Clean up temporary files (.part and .done files)
wait_for_tasks()Wait for all async download tasks to complete
verify_checksum()Verify file checksum
print()Print downloader summary
## ------------------------------------------------
## Method `Downloader$new`
## ------------------------------------------------
if (FALSE) { # \dontrun{
dl <- Downloader$new()
dl <- Downloader$new(dest = "~/data")
dl <- Downloader$new(
dest = "~/data",
temp = "~/data/.tmp",
n_workers = 8
)
} # }
## ------------------------------------------------
## Method `Downloader$download`
## ------------------------------------------------
if (FALSE) { # \dontrun{
# Blocking download
path <- dl$download(url = "https://example.com/data.nc")
# Async download
task_id <- dl$download(
url = "https://example.com/data.nc",
block = FALSE
)
dl$wait_for_tasks(task_id)
# Multiple files (async batch)
urls <- c("https://example.com/file1.nc", "https://example.com/file2.nc")
task_ids <- sapply(urls, function(url) {
dl$download(url = url, block = FALSE)
})
results <- dl$wait_for_tasks(task_ids)
} # }
## ------------------------------------------------
## Method `Downloader$cleanup_tmp`
## ------------------------------------------------
if (FALSE) { # \dontrun{
n_removed <- downloader$cleanup_tmp()
n_removed <- downloader$cleanup_tmp(all = TRUE)
} # }
## ------------------------------------------------
## Method `Downloader$get_tasks`
## ------------------------------------------------
if (FALSE) { # \dontrun{
tasks <- downloader$get_tasks()
} # }
## ------------------------------------------------
## Method `Downloader$get_task_status`
## ------------------------------------------------
if (FALSE) { # \dontrun{
task_id <- dl$download(url, block = FALSE)
status <- dl$get_task_status(task_id)
} # }
## ------------------------------------------------
## Method `Downloader$wait_for_tasks`
## ------------------------------------------------
if (FALSE) { # \dontrun{
task1 <- dl$download(url1, block = FALSE)
task2 <- dl$download(url2, block = FALSE)
results <- dl$wait_for_tasks()
} # }
## ------------------------------------------------
## Method `Downloader$cancel_task`
## ------------------------------------------------
if (FALSE) { # \dontrun{
task_id <- dl$download(url, block = FALSE)
# Cancel if needed
dl$cancel_task(task_id)
} # }
## ------------------------------------------------
## Method `Downloader$list_incomplete`
## ------------------------------------------------
if (FALSE) { # \dontrun{
incomplete <- downloader$list_incomplete()
} # }
## ------------------------------------------------
## Method `Downloader$verify_checksum`
## ------------------------------------------------
if (FALSE) { # \dontrun{
valid <- downloader$verify_checksum("data.nc", "abc123", "sha256")
} # }