Skip to content

Commit

Permalink
use logical cleanup vector closes #79
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Oct 10, 2023
1 parent b0f2ac9 commit 0a56b67
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 48 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down
22 changes: 10 additions & 12 deletions R/daemon.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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))
12 changes: 6 additions & 6 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ knitr::opts_chunk$set(

<!-- badges: start -->
[![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)
Expand Down Expand Up @@ -127,25 +127,25 @@ 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

`mirai` serves as a backend for enterprise asynchronous [`shiny`](https://cran.r-project.org/package=shiny) applications:

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.

Expand Down
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -90,19 +90,19 @@ 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
`call_mirai()`.

``` 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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions man/daemon.Rd

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

2 changes: 1 addition & 1 deletion tests/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0a56b67

Please sign in to comment.