Skip to content

Commit

Permalink
Added support for backend maintainers to specify "cleanup" hook funct…
Browse files Browse the repository at this point in the history
…ions [#719]
  • Loading branch information
HenrikBengtsson committed May 29, 2024
1 parent 99f8dda commit 9cc64fa
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: future
Version: 1.33.2-9001
Version: 1.33.2-9002
Title: Unified Parallel and Distributed Processing in R for Everyone
Imports:
digest,
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Version (development version)

## New Features

* Added support for backend maintainers to specify "cleanup" hook
functions on future strategies, which are called when switching
future plan. These hook functions are specified via the optional
`cleanup` attribute, cf. `attr(cluster, "cleanup")`.

## Bug Fixes

* `resolved()` for `ClusterFuture`:s would produce `Error:
Expand Down
3 changes: 3 additions & 0 deletions R/cluster.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ cluster <- function(..., persistent = FALSE, workers = availableWorkers(), envir
}
class(cluster) <- c("cluster", "multiprocess", "future", "function")
attr(cluster, "init") <- TRUE
attr(cluster, "cleanup") <- function() {
ClusterRegistry(action = "stop")
}
attr(cluster, "tweakable") <- quote(c(makeClusterPSOCK_args(), "persistent"))
3 changes: 3 additions & 0 deletions R/multisession.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,7 @@ multisession <- function(..., workers = availableCores(), lazy = FALSE, rscript_
}
class(multisession) <- c("multisession", "cluster", "multiprocess", "future", "function")
attr(multisession, "init") <- TRUE
attr(multisession, "cleanup") <- function() {
ClusterRegistry(action = "stop")
}
attr(multisession, "untweakable") <- c("persistent")
18 changes: 13 additions & 5 deletions R/zzz.plan.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,16 @@ plan <- local({
})

plan_cleanup <- function() {
ClusterRegistry(action = "stop")
evaluator <- stack[[1L]]

cleanup <- attr(evaluator, "cleanup", exact = TRUE)
if (!is.null(cleanup)) {
if (is.function(cleanup)) {
cleanup()
} else {
stop(FutureError(sprintf("Unknown type of 'cleanup' attribute on current future strategy: %s", sQuote(paste(class(cleanup), collapse = ", ")))))
}
}
}

plan_init <- function() {
Expand Down Expand Up @@ -266,11 +275,11 @@ plan <- local({
## Warn about 'multicore' on certain systems
warn_about_multicore(newStack)

stack <<- newStack

## Stop any (implicitly started) clusters?
## Stop/cleanup any previously registered backends?
if (cleanup) plan_cleanup()

stack <<- newStack

## Initiate future workers?
if (init) plan_init()

Expand Down Expand Up @@ -575,4 +584,3 @@ resetWorkers.multicore <- function(x, ...) {
FutureRegistry(reg, action = "collect-all", earlySignal = FALSE)
stop_if_not(usedCores() == 0L)
}

4 changes: 2 additions & 2 deletions man/future.options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/multisession.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9cc64fa

Please sign in to comment.