From 0a56b670bb772bc0f1a59635d0ec94bc338f185f Mon Sep 17 00:00:00 2001 From: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com> Date: Tue, 10 Oct 2023 21:31:14 +0100 Subject: [PATCH] use logical cleanup vector closes #79 --- NEWS.md | 1 + R/daemon.R | 22 ++++++++++------------ README.Rmd | 12 ++++++------ README.md | 42 +++++++++++++++++++++--------------------- man/daemon.Rd | 16 ++++++++-------- tests/tests.R | 2 +- 6 files changed, 47 insertions(+), 48 deletions(-) diff --git a/NEWS.md b/NEWS.md index 6a054da25..6e998ffd2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,7 @@ * Adds 'mirai' method for 'as.promise()' from the `promises` package (if both packages `promises` and `later` are available). This functionality is merged from the package `mirai.promises` for convenience, and allows use of the promise pipe `%...>%` with a 'mirai'. * Eliminates the 'exitlinger' period that still applied to ephemeral daemons. These now synchronise with host and exit as soon as permissible. * Daemons connecting over TLS now follow the specified 'asyncdial'. +* `daemons()` argument 'cleanup' now takes a logical vector specifying the cleanup actions to take, rather than an integer bitmask (thanks @krlmlr #79). * Adds a 'not implemented' `[` method for 'miraiCluster' (thanks @HenrikBengtsson #83). * Removes the deprecated deferred evaluation pipe `%>>%`. * Requires nanonext >= [0.10.2.9001]. diff --git a/R/daemon.R b/R/daemon.R index 48200c0b9..e9d08f1c9 100644 --- a/R/daemon.R +++ b/R/daemon.R @@ -46,13 +46,13 @@ #' redirection of output to the host process (applicable only for local #' daemons when not using dispatcher). #' @param ... reserved but not currently used. -#' @param cleanup [default 7L] Integer additive bitmask controlling whether to -#' perform cleanup of the global environment (1L), reset loaded packages to -#' an initial state (2L), reset options to an initial state (4L), and -#' perform garbage collection (8L) after each evaluation. This option should -#' not normally be modified. Do not set unless you are certain you require -#' persistence across evaluations. Note: it may be an error to reset options -#' but not loaded packages if packages set options on load. +#' @param cleanup [default c(TRUE, TRUE, TRUE, FALSE)] logical vector of length +#' 4, specifying whether to perform the following operations after each +#' evaluation: (1) cleanup the global environment, (2) reset loaded packages, +#' (3) reset options to an initial state, and (4) perform garbage collection. +#' This option should not normally be modified - do not set unless you are +#' certain you require persistence across evaluations. Caution: do not reset +#' options but not loaded packages if packages set options on load. #' @param tls [default NULL] required for secure TLS connections over 'tls+tcp://' #' or 'wss://'. \strong{Either} the character path to a file containing #' X.509 certificate(s) in PEM format, comprising the certificate authority @@ -75,7 +75,7 @@ #' daemon <- function(url, asyncdial = FALSE, maxtasks = Inf, idletime = Inf, walltime = Inf, timerstart = 0L, output = FALSE, ..., - cleanup = 7L, tls = NULL, rs = NULL) { + cleanup = c(TRUE, TRUE, TRUE, FALSE), tls = NULL, rs = NULL) { sock <- socket(protocol = "rep") on.exit(reap(sock)) @@ -86,7 +86,8 @@ daemon <- function(url, asyncdial = FALSE, maxtasks = Inf, idletime = Inf, if (is.numeric(rs)) `[[<-`(.GlobalEnv, ".Random.seed", as.integer(rs)) if (idletime > walltime) idletime <- walltime else if (idletime == Inf) idletime <- NULL - cleanup <- parse_cleanup(cleanup) + if (length(cleanup) != 4L) + cleanup <- c(cleanup %% 2L, (clr <- as.raw(cleanup)) & as.raw(2L), clr & as.raw(4L), clr & as.raw(8L)) if (!output) { devnull <- file(nullfile(), open = "w", blocking = FALSE) sink(file = devnull) @@ -172,6 +173,3 @@ dial_and_sync_socket <- function(sock, url, asyncdial, tls = NULL) { dial(sock, url = url, autostart = asyncdial || NA, tls = tls, error = TRUE) wait(cv) } - -parse_cleanup <- function(cleanup) - c(cleanup %% 2L, (clr <- as.raw(cleanup)) & as.raw(2L), clr & as.raw(4L), clr & as.raw(8L)) diff --git a/README.Rmd b/README.Rmd index 63335883b..493506ded 100644 --- a/README.Rmd +++ b/README.Rmd @@ -17,7 +17,7 @@ knitr::opts_chunk$set( [![CRAN status](https://www.r-pkg.org/badges/version/mirai?color=112d4e)](https://CRAN.R-project.org/package=mirai) -[![mirai status badge](https://shikokuchuo.r-universe.dev/badges/mirai?color=24a60e)](https://shikokuchuo.r-universe.dev) +[![mirai status badge](https://shikokuchuo.r-universe.dev/badges/mirai?color=24a60e)](https://shikokuchuo.r-universe.dev/) [![R-CMD-check](https://github.com/shikokuchuo/mirai/workflows/R-CMD-check/badge.svg)](https://github.com/shikokuchuo/mirai/actions) [![codecov](https://codecov.io/gh/shikokuchuo/mirai/branch/main/graph/badge.svg)](https://app.codecov.io/gh/shikokuchuo/mirai) [![DOI](https://zenodo.org/badge/459341940.svg)](https://zenodo.org/badge/latestdoi/459341940) @@ -127,11 +127,11 @@ This functionality, fulfilling a request from R-Core at R Project Sprint 2023, r ### Use with Crew and Targets -The [`crew`](https://wlandau.github.io/crew/) package is a distributed worker-launcher that provides an R6-based interface extending `mirai` to different distributed computing platforms, from traditional clusters to cloud services. +The [`crew`](https://cran.r-project.org/package=crew) package is a distributed worker-launcher that provides an R6-based interface extending `mirai` to different distributed computing platforms, from traditional clusters to cloud services. -The [`crew.cluster`](https://wlandau.github.io/crew.cluster/) package is a plug-in that enables mirai-based workflows on traditional high-performance computing clusters using LFS, PBS/TORQUE, SGE and SLURM. +The [`crew.cluster`](https://cran.r-project.org/package=crew.cluster) package is a plug-in that enables mirai-based workflows on traditional high-performance computing clusters using LFS, PBS/TORQUE, SGE and SLURM. -[`targets`](https://docs.ropensci.org/targets/), a Make-like pipeline tool for statistics and data science, has integrated and adopted [`crew`](https://wlandau.github.io/crew/) as its predominant high-performance computing backend. +[`targets`](https://cran.r-project.org/package=targets), a Make-like pipeline tool for statistics and data science, has integrated and adopted [`crew`](https://cran.r-project.org/package=crew) as its predominant high-performance computing backend. ### Use with Shiny @@ -139,13 +139,13 @@ The [`crew.cluster`](https://wlandau.github.io/crew.cluster/) package is a plug- A 'mirai' may be used interchangeably with a 'promise' by using the the promise pipe `%...>%`, or explictly by `promises::as.promise()`, allowing side-effects to be performed upon asynchronous resolution of a 'mirai'. -[`crew`](https://wlandau.github.io/crew/) also provides an interface that facilitates deploying `mirai` for [`shiny`](https://cran.r-project.org/package=shiny). The package provides a [Shiny vignette](https://wlandau.github.io/crew/articles/shiny.html) with tutorial and sample code for this purpose. +[`crew`](https://cran.r-project.org/package=crew) also provides an interface that facilitates deploying `mirai` for [`shiny`](https://cran.r-project.org/package=shiny). The package provides a [Shiny vignette](https://wlandau.github.io/crew/articles/shiny.html) with tutorial and sample code for this purpose. ### Thanks We would like to thank in particular: -[William Landau](https://github.com/wlandau/), for being instrumental in shaping development of the package, from initiating the original request for persistent daemons, through to orchestrating robustness testing for the high performance computing requirements of [`crew`](https://wlandau.github.io/crew/) and [`targets`](https://docs.ropensci.org/targets/). +[William Landau](https://github.com/wlandau/), for being instrumental in shaping development of the package, from initiating the original request for persistent daemons, through to orchestrating robustness testing for the high performance computing requirements of [`crew`](https://cran.r-project.org/package=crew) and [`targets`](https://cran.r-project.org/package=targets). [Henrik Bengtsson](https://github.com/HenrikBengtsson/), for valuable and incisive insights leading to the interface accepting broader usage patterns. diff --git a/README.md b/README.md index 138f90de0..aa695c60a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ [![CRAN status](https://www.r-pkg.org/badges/version/mirai?color=112d4e)](https://CRAN.R-project.org/package=mirai) [![mirai status -badge](https://shikokuchuo.r-universe.dev/badges/mirai?color=24a60e)](https://shikokuchuo.r-universe.dev) +badge](https://shikokuchuo.r-universe.dev/badges/mirai?color=24a60e)](https://shikokuchuo.r-universe.dev/) [![R-CMD-check](https://github.com/shikokuchuo/mirai/workflows/R-CMD-check/badge.svg)](https://github.com/shikokuchuo/mirai/actions) [![codecov](https://codecov.io/gh/shikokuchuo/mirai/branch/main/graph/badge.svg)](https://app.codecov.io/gh/shikokuchuo/mirai) [![DOI](https://zenodo.org/badge/459341940.svg)](https://zenodo.org/badge/latestdoi/459341940) @@ -90,9 +90,9 @@ result. ``` r m$data -#> [1] 3.10654162 -0.63227381 -6.06113466 -69.78861654 0.88790445 -#> [6] 1.00000000 1.12624731 -0.01432898 -0.16498561 -1.58159326 -#> [11] 0.32190137 +#> [1] 3.13793074 0.90508259 -0.01180767 -0.24487893 2.31528028 +#> [6] 1.00000000 0.43191315 -4.08365070 -84.69073785 1.10487154 +#> [11] 0.31868135 ``` Alternatively, explicitly call and wait for the result using @@ -100,9 +100,9 @@ Alternatively, explicitly call and wait for the result using ``` r call_mirai(m)$data -#> [1] 3.10654162 -0.63227381 -6.06113466 -69.78861654 0.88790445 -#> [6] 1.00000000 1.12624731 -0.01432898 -0.16498561 -1.58159326 -#> [11] 0.32190137 +#> [1] 3.13793074 0.90508259 -0.01180767 -0.24487893 2.31528028 +#> [6] 1.00000000 0.43191315 -4.08365070 -84.69073785 1.10487154 +#> [11] 0.31868135 ``` ### Vignette @@ -144,20 +144,20 @@ This functionality, fulfilling a request from R-Core at R Project Sprint ### Use with Crew and Targets -The [`crew`](https://wlandau.github.io/crew/) package is a distributed -worker-launcher that provides an R6-based interface extending `mirai` to -different distributed computing platforms, from traditional clusters to -cloud services. +The [`crew`](https://cran.r-project.org/package=crew) package is a +distributed worker-launcher that provides an R6-based interface +extending `mirai` to different distributed computing platforms, from +traditional clusters to cloud services. -The [`crew.cluster`](https://wlandau.github.io/crew.cluster/) package is -a plug-in that enables mirai-based workflows on traditional +The [`crew.cluster`](https://cran.r-project.org/package=crew.cluster) +package is a plug-in that enables mirai-based workflows on traditional high-performance computing clusters using LFS, PBS/TORQUE, SGE and SLURM. -[`targets`](https://docs.ropensci.org/targets/), a Make-like pipeline -tool for statistics and data science, has integrated and adopted -[`crew`](https://wlandau.github.io/crew/) as its predominant -high-performance computing backend. +[`targets`](https://cran.r-project.org/package=targets), a Make-like +pipeline tool for statistics and data science, has integrated and +adopted [`crew`](https://cran.r-project.org/package=crew) as its +predominant high-performance computing backend. ### Use with Shiny @@ -169,8 +169,8 @@ promise pipe `%...>%`, or explictly by `promises::as.promise()`, allowing side-effects to be performed upon asynchronous resolution of a ‘mirai’. -[`crew`](https://wlandau.github.io/crew/) also provides an interface -that facilitates deploying `mirai` for +[`crew`](https://cran.r-project.org/package=crew) also provides an +interface that facilitates deploying `mirai` for [`shiny`](https://cran.r-project.org/package=shiny). The package provides a [Shiny vignette](https://wlandau.github.io/crew/articles/shiny.html) with @@ -184,8 +184,8 @@ We would like to thank in particular: shaping development of the package, from initiating the original request for persistent daemons, through to orchestrating robustness testing for the high performance computing requirements of -[`crew`](https://wlandau.github.io/crew/) and -[`targets`](https://docs.ropensci.org/targets/). +[`crew`](https://cran.r-project.org/package=crew) and +[`targets`](https://cran.r-project.org/package=targets). [Henrik Bengtsson](https://github.com/HenrikBengtsson/), for valuable and incisive insights leading to the interface accepting broader usage diff --git a/man/daemon.Rd b/man/daemon.Rd index 1c0384d5e..e4c3131b4 100644 --- a/man/daemon.Rd +++ b/man/daemon.Rd @@ -13,7 +13,7 @@ daemon( timerstart = 0L, output = FALSE, ..., - cleanup = 7L, + cleanup = c(TRUE, TRUE, TRUE, FALSE), tls = NULL, rs = NULL ) @@ -51,13 +51,13 @@ daemons when not using dispatcher).} \item{...}{reserved but not currently used.} -\item{cleanup}{[default 7L] Integer additive bitmask controlling whether to -perform cleanup of the global environment (1L), reset loaded packages to -an initial state (2L), reset options to an initial state (4L), and -perform garbage collection (8L) after each evaluation. This option should -not normally be modified. Do not set unless you are certain you require -persistence across evaluations. Note: it may be an error to reset options -but not loaded packages if packages set options on load.} +\item{cleanup}{[default c(TRUE, TRUE, TRUE, FALSE)] logical vector of length +4, specifying whether to perform the following operations after each +evaluation: (1) cleanup the global environment, (2) reset loaded packages, +(3) reset options to an initial state, and (4) perform garbage collection. +This option should not normally be modified - do not set unless you are +certain you require persistence across evaluations. Caution: do not reset +options but not loaded packages if packages set options on load.} \item{tls}{[default NULL] required for secure TLS connections over 'tls+tcp://' or 'wss://'. \strong{Either} the character path to a file containing diff --git a/tests/tests.R b/tests/tests.R index a854111d2..aa1d5d633 100644 --- a/tests/tests.R +++ b/tests/tests.R @@ -66,7 +66,7 @@ nanotest(daemons(url = value <- mirai:::auto_tokenized_url(), dispatcher = FALSE nanotest(grepl("://", launch_remote(status()$daemons), fixed = TRUE)) nanotestz(daemons(0L)) Sys.sleep(1L) -nanotesto(daemons(1L, dispatcher = FALSE, idletime = 500L, timerstart = 1L, cleanup = 0L, seed = 1546, .compute = "new")) +nanotesto(daemons(1L, dispatcher = FALSE, idletime = 500L, timerstart = 1L, cleanup = c(FALSE, FALSE, FALSE, FALSE), seed = 1546, .compute = "new")) nanotest(is.character(nextget("urls", .compute = "new"))) nanotest(is.integer(nextstream(.compute = "new"))) Sys.sleep(1.5)