Skip to content

Commit 920beb2

Browse files
committed
tar_workspace_download()
1 parent 0111440 commit 920beb2

18 files changed

+306
-9
lines changed

Diff for: NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ export(tar_watch_app_ui)
551551
export(tar_watch_server)
552552
export(tar_watch_ui)
553553
export(tar_workspace)
554+
export(tar_workspace_download)
554555
export(tar_workspaces)
555556
export(target_run_worker)
556557
export(use_targets)

Diff for: R/class_builder.R

+2
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@ builder_save_workspace <- function(target, pipeline, scheduler, meta) {
419419
path_store = meta$store
420420
)
421421
scheduler$reporter$report_workspace(target)
422+
meta$database$upload_workspace(target, meta)
423+
scheduler$reporter$report_workspace_upload(target)
422424
}
423425

424426
builder_record_error_meta <- function(target, pipeline, meta) {

Diff for: R/class_database.R

+6
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,18 @@ database_class <- R6::R6Class(
435435
}
436436
"upload"
437437
},
438+
upload_workspace = function(target, meta) {
439+
"upload_workspace"
440+
},
438441
download = function(verbose = TRUE) {
439442
if (verbose) {
440443
tar_message_run("downloading")
441444
}
442445
"download"
443446
},
447+
download_workspace = function(name, store) {
448+
"download_workspace"
449+
},
444450
head = function() {
445451
file <- file_init(path = "path_cloud")
446452
file_ensure_hash(file)

Diff for: R/class_database_aws.R

+33
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ database_aws_class <- R6::R6Class(
6767
)
6868
invisible()
6969
},
70+
download_workspace = function(name, store) {
71+
path <- path_workspace(store, name)
72+
key <- path_workspace(dirname(self$key), name)
73+
aws <- self$resources$aws
74+
dir_create(dirname(path))
75+
aws_s3_download(
76+
file = path,
77+
key = key,
78+
bucket = aws$bucket,
79+
region = aws$region,
80+
endpoint = aws$endpoint,
81+
args = aws$args,
82+
max_tries = aws$max_tries %|||% 5L
83+
)
84+
invisible()
85+
},
7086
upload = function(verbose = TRUE) {
7187
if (verbose) {
7288
tar_print(
@@ -96,6 +112,23 @@ database_aws_class <- R6::R6Class(
96112
)
97113
invisible()
98114
},
115+
upload_workspace = function(target, meta) {
116+
name <- target_get_name(target)
117+
path <- path_workspace(meta$store, name)
118+
key <- path_workspace(dirname(self$key), name)
119+
aws <- self$resources$aws
120+
aws_s3_upload(
121+
file = path,
122+
key = key,
123+
bucket = aws$bucket,
124+
region = aws$region,
125+
endpoint = aws$endpoint,
126+
part_size = aws$part_size,
127+
args = aws$args,
128+
max_tries = aws$max_tries %|||% 5L
129+
)
130+
invisible()
131+
},
99132
head = function() {
100133
aws <- self$resources$aws
101134
head <- aws_s3_head(

Diff for: R/class_reporter.R

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ reporter_class <- R6::R6Class(
7373
},
7474
report_workspace = function(target) {
7575
},
76+
report_workspace_upload = function(target) {
77+
},
7678
report_retry = function(target = NULL, progress = NULL) {
7779
},
7880
report_finalize = function(progress = NULL) {

Diff for: R/class_timestamp.R

+9
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ timestamp_class <- R6::R6Class(
8080
)
8181
)
8282
},
83+
report_workspace_upload = function(target) {
84+
self$buffer_message(
85+
cli_workspace_upload(
86+
target_get_name(target),
87+
time_stamp = TRUE,
88+
print = FALSE
89+
)
90+
)
91+
},
8392
report_retry = function(target, progress = NULL) {
8493
self$buffer_message(
8594
cli_retry(

Diff for: R/class_verbose.R

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ verbose_class <- R6::R6Class(
6969
report_workspace = function(target) {
7070
self$buffer_message(cli_workspace(target_get_name(target), print = FALSE))
7171
},
72+
report_workspace_upload = function(target) {
73+
self$buffer_message(
74+
cli_workspace_upload(target_get_name(target), print = FALSE)
75+
)
76+
},
7277
report_retry = function(target, progress = NULL) {
7378
self$buffer_message(
7479
cli_retry(

Diff for: R/tar_workspace.R

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' @title Load a saved workspace and seed for debugging.
1+
#' @title Load a locally saved workspace and seed for debugging.
22
#' @export
33
#' @family debug
44
#' @description Load the packages, environment, and random number generator
@@ -12,6 +12,13 @@
1212
#' are still in the data store (usually files in `_targets/objects/`).
1313
#' When you are done debugging, you can remove the workspace files
1414
#' using `tar_destroy(destroy = "workspaces")`.
15+
#'
16+
#' If `tar_option_get("repository_meta")` is `"aws"` or `"gcp"`, then
17+
#' [tar_make()] uploads workspaces to the bucket and prefix provided.
18+
#' Download one of these workspaces with [tar_workspace_download()].
19+
#' Downloaded workspaces can be loaded the usual way with
20+
#' [tar_workspace()], and you should see them in
21+
#' character vector returned by [tar_workspaces()].
1522
#' @return This function returns `NULL`, but it does load
1623
#' the target's required packages, as well as multiple objects
1724
#' into the environment (`envir` argument) in order to replicate the
@@ -35,7 +42,6 @@
3542
#' tar_script({
3643
#' library(targets)
3744
#' library(tarchetypes)
38-
#' tar_option_set(workspace_on_error = TRUE)
3945
#' list(
4046
#' tar_target(x, "loaded"),
4147
#' tar_target(y, stop(x))

Diff for: R/tar_workspace_download.R

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#' @title Download a workspace from the cloud.
2+
#' @export
3+
#' @family debug
4+
#' @description Download a workspace file from the cloud
5+
#' so it can be loaded with [tar_workspace()].
6+
#' @details If `tar_option_get("repository_meta")` is `"aws"` or `"gcp"`, then
7+
#' [tar_make()] uploads workspaces to the bucket and prefix provided.
8+
#' Download one of these workspaces with [tar_workspace_download()].
9+
#' Downloaded workspaces can be loaded the usual way with
10+
#' [tar_workspace()], and you should see them in
11+
#' character vector returned by [tar_workspaces()].
12+
#' @return `NULL` (invisibly). Returns an error if the workspace
13+
#' cannot be downloaded.
14+
#' @inheritParams tar_validate
15+
#' @param name Symbol, name of the target whose workspace to download.
16+
#' @param script Character string, file path to the `_targets.R` file
17+
#' defining the pipeline. Must be configured with the right `aws`
18+
#' and `repository_meta` options (in [tar_option_set()])
19+
#' to support downloading workspaces from the cloud.
20+
#' @examples
21+
#' if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) { # for CRAN
22+
#' tar_dir({ # tar_dir() runs code from a temp dir for CRAN.
23+
#' tmp <- sample(1)
24+
#' tar_script({
25+
#' library(targets)
26+
#' library(tarchetypes)
27+
#' tar_option_set(
28+
#' tar_option_set(
29+
#' resources = tar_resources(
30+
#' tar_resources_aws(
31+
#' bucket = "YOUR_AWS_BUCKET",
32+
#' prefix = "_targets"
33+
#' )
34+
#' ),
35+
#' repository = "aws",
36+
#' repository_meta = "aws"
37+
#' )
38+
#' f <- function() stop("this is an error and thus triggers a workspace")
39+
#' list(
40+
#' tar_target(x, f()),
41+
#' )
42+
#' }, ask = FALSE)
43+
#' # The following code throws an error for demonstration purposes.
44+
#' try(tar_make())
45+
#' # Say the workspace file for target x does not exist.
46+
#' unlink("_targets/workspaces/x")
47+
#' file.exists("_targets/workspaces/x")
48+
#' # We can download it with tar_workspace_download()
49+
#' tar_workspace_download(x)
50+
#' file.exists("_targets/workspaces/x")
51+
#' tar_workspace(x)
52+
#' })
53+
#' }
54+
tar_workspace_download <- function(
55+
name,
56+
script = targets::tar_config_get("script"),
57+
store = targets::tar_config_get("store")
58+
) {
59+
name <- tar_deparse_language(substitute(name))
60+
tar_assert_chr(name)
61+
tar_assert_scalar(name)
62+
tar_assert_nzchar(name)
63+
tar_assert_scalar(store)
64+
tar_assert_chr(store)
65+
tar_assert_nzchar(store)
66+
options <- tar_script_options(script = script)
67+
old_repository_meta <- tar_options$get_repository_meta()
68+
old_resources <- tar_options$get_resources()
69+
on.exit({
70+
tar_options$set_repository_meta(old_repository_meta)
71+
tar_options$set_resources(old_resources)
72+
})
73+
tar_options$set_repository_meta(options$repository_meta)
74+
tar_options$set_resources(options$resources)
75+
tar_assert_not_in(
76+
x = options$repository_meta,
77+
choices = "local",
78+
msg = paste(
79+
"tar_workspace_download() is not supported for",
80+
"tar_option_get(\"repository_meta\") == \"local\"."
81+
)
82+
)
83+
database_meta(path_store = store)$download_workspace(name, store)
84+
invisible()
85+
}

Diff for: R/tar_workspaces.R

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
#' @title List saved target workspaces.
1+
#' @title List locally saved target workspaces.
22
#' @export
33
#' @family debug
44
#' @description List target workspaces currently saved to
5-
#' `_targets/workspaces/`. See [tar_workspace()] for more information.
5+
#' `_targets/workspaces/` locally.
6+
#' Does not include workspaces saved to the cloud.
7+
#' See [tar_workspace()] and [tar_workspace_download()]
8+
#' for more information.
69
#' @return Character vector of available workspaces to load with
710
#' [tar_workspace()].
811
#' @inheritParams tar_validate

Diff for: R/utils_cli.R

+6
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ cli_workspace <- function(name, time_stamp = FALSE, print = TRUE) {
126126
cli_blue_play(msg, print = print)
127127
}
128128

129+
cli_workspace_upload <- function(name, time_stamp = FALSE, print = TRUE) {
130+
time <- if_any(time_stamp, time_stamp_cli(), NULL)
131+
msg <- paste(c(time, "uploaded workspace", name), collapse = " ")
132+
cli_blue_play(msg, print = print)
133+
}
134+
129135
cli_retry <- function(name, prefix = NULL, time_stamp = FALSE, print = TRUE) {
130136
time <- if_any(time_stamp, time_stamp_cli(), NULL)
131137
msg <- paste(c(time, "retrying", prefix, name), collapse = " ")

Diff for: _pkgdown.yml

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ reference:
5252
- 'tar_load_globals'
5353
- 'tar_traceback'
5454
- 'tar_workspace'
55+
- 'tar_workspace_download'
5556
- 'tar_workspaces'
5657
- title: Storage
5758
contents:

Diff for: man/tar_load_globals.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: man/tar_traceback.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: man/tar_workspace.Rd

+9-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)