Skip to content

Commit ff58a85

Browse files
Preparing for when the batchtools registry not being created until when the future is launched
1 parent ecfa308 commit ff58a85

File tree

3 files changed

+61
-30
lines changed

3 files changed

+61
-30
lines changed

R/BatchtoolsFuture-class.R

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,12 @@ print.BatchtoolsFuture <- function(x, ...) {
165165
## batchtools specific
166166
reg <- x$config$reg
167167

168-
## Type of batchtools future
169-
printf("batchtools cluster functions: %s\n",
170-
sQuote(reg$cluster.functions$name))
168+
if (inherits(reg, "Registry")) {
169+
printf("batchtools cluster functions: %s\n",
170+
sQuote(reg$cluster.functions$name))
171+
} else {
172+
printf("batchtools cluster functions: N/A\n")
173+
}
171174

172175
## Ask for status once
173176
status <- status(x)
@@ -180,10 +183,14 @@ print.BatchtoolsFuture <- function(x, ...) {
180183
printf("batchtools %s: Not found (happens when finished and deleted)\n",
181184
class(reg))
182185
} else {
183-
printf("batchtools Registry:\n")
184-
printf(" File dir exists: %s\n", file_test("-d", reg$file.dir))
185-
printf(" Work dir exists: %s\n", file_test("-d", reg$work.dir))
186-
try(print(reg))
186+
if (inherits(reg, "Registry")) {
187+
printf("batchtools Registry:\n")
188+
printf(" File dir exists: %s\n", file_test("-d", reg$file.dir))
189+
printf(" Work dir exists: %s\n", file_test("-d", reg$work.dir))
190+
try(print(reg))
191+
} else {
192+
printf("batchtools Registry: N/A\n")
193+
}
187194
}
188195

189196
invisible(x)
@@ -286,6 +293,7 @@ loggedError.BatchtoolsFuture <- function(future, ...) {
286293

287294
config <- future$config
288295
reg <- config$reg
296+
if (!inherits(reg, "Registry")) return(NULL)
289297
jobid <- config$jobid
290298
res <- getErrorMessages(reg = reg, ids = jobid) ### CHECKED
291299
msg <- res$message
@@ -310,6 +318,7 @@ loggedOutput.BatchtoolsFuture <- function(future, ...) {
310318

311319
config <- future$config
312320
reg <- config$reg
321+
if (!inherits(reg, "Registry")) return(NULL)
313322
jobid <- config$jobid
314323
getLog(id = jobid, reg = reg)
315324
} # loggedOutput()
@@ -400,7 +409,29 @@ run.BatchtoolsFuture <- function(future, ...) {
400409
## Always evaluate in local environment
401410
expr <- substitute(local(expr), list(expr = expr))
402411

412+
## (i) Create batchtools registry
403413
reg <- future$config$reg
414+
stop_if_not(is.null(reg) || inherits(reg, "Registry"))
415+
if (is.null(reg)) {
416+
if (debug) mprint("- Creating batchtools registry")
417+
config <- future$config
418+
stop_if_not(is.list(config))
419+
420+
## Create batchtools registry
421+
reg <- temp_registry(
422+
label = future$label,
423+
conf.file = config$conf.file,
424+
cluster.functions = config$cluster.functions,
425+
config = config$registry
426+
)
427+
if (debug) mprint(reg)
428+
future$config$reg <- reg
429+
430+
## Register finalizer?
431+
if (config$finalize) future <- add_finalizer(future)
432+
433+
config <- NULL
434+
}
404435
stop_if_not(inherits(reg, "Registry"))
405436

406437
## (ii) Attach packages that needs to be attached
@@ -522,6 +553,7 @@ await.BatchtoolsFuture <- function(future, cleanup = TRUE,
522553
expr <- future$expr
523554
config <- future$config
524555
reg <- config$reg
556+
stop_if_not(inherits(reg, "Registry"))
525557
jobid <- config$jobid
526558

527559
mdebug("batchtools::waitForJobs() ...")
@@ -641,6 +673,10 @@ delete.BatchtoolsFuture <- function(future,
641673
## Identify registry
642674
config <- future$config
643675
reg <- config$reg
676+
677+
## Trying to delete a non-launched batchtools future?
678+
if (!inherits(reg, "Registry")) return(invisible(TRUE))
679+
644680
path <- reg$file.dir
645681

646682
## Already deleted?

tests/BatchtoolsFuture.R

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ f <- BatchtoolsFuture({ x <- 1 })
3535

3636
## Hack to emulate where batchtools registry is deleted or fails
3737
f$state <- "running"
38-
path <- f$config$reg$file.dir
39-
unlink(path, recursive = TRUE)
38+
if (!is.null(f$config$reg)) {
39+
path <- f$config$reg$file.dir
40+
unlink(path, recursive = TRUE)
41+
}
4042

4143
res <- tryCatch({
4244
value(f)
@@ -55,20 +57,6 @@ message("*** BatchtoolsFuture() - registry exceptions ... DONE")
5557

5658
message("*** BatchtoolsFuture() - exceptions ...")
5759

58-
f <- BatchtoolsFuture({ 42L })
59-
res <- tryCatch({
60-
loggedError(f)
61-
}, error = function(ex) ex)
62-
print(res)
63-
stopifnot(inherits(res, "error"))
64-
65-
f <- BatchtoolsFuture({ 42L })
66-
res <- tryCatch({
67-
loggedOutput(f)
68-
}, error = function(ex) ex)
69-
print(res)
70-
stopifnot(inherits(res, "error"))
71-
7260
res <- try(f <- BatchtoolsFuture(42L, workers = integer(0)), silent = TRUE)
7361
print(res)
7462
stopifnot(inherits(res, "try-error"))

tests/plan.R

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,27 @@ for (type in c("batchtools_interactive", "batchtools_local")) {
4141
f <- future(42, lazy = TRUE)
4242
## In future releases, lazy futures may stay vanilla Future objects
4343
if (inherits(f, "BatchtoolsFuture")) {
44-
utils::str(list(normalize_path(f$config$reg$work.dir), getwd = getwd()))
45-
stopifnot(normalize_path(f$config$reg$work.dir) == getwd())
44+
if (!is.null(f$config$reg)) {
45+
utils::str(list(
46+
normalize_path(f$config$reg$work.dir),
47+
getwd = getwd()
48+
))
49+
stopifnot(normalize_path(f$config$reg$work.dir) == getwd())
50+
}
4651
}
4752

4853
path <- tempdir()
4954
plan(type, registry = list(work.dir = path))
5055
f <- future(42, lazy = TRUE)
5156
## In future releases, lazy futures may stay vanilla Future objects
5257
if (inherits(f, "BatchtoolsFuture")) {
53-
utils::str(list(
54-
normalizePath(f$config$reg$work.dir),
55-
path = normalizePath(path)
56-
))
57-
stopifnot(normalize_path(f$config$reg$work.dir) == normalize_path(path))
58+
if (!is.null(f$config$reg)) {
59+
utils::str(list(
60+
normalize_path(f$config$reg$work.dir),
61+
path = normalize_path(path)
62+
))
63+
stopifnot(normalize_path(f$config$reg$work.dir) == normalize_path(path))
64+
}
5865
}
5966

6067
mprintf("*** plan('%s') ... DONE\n", type)

0 commit comments

Comments
 (0)