Skip to content

Commit f2d7425

Browse files
authored
Make daemons() calls reset any existing daemons (#396)
* Make `daemons()` calls reset any existing daemons * Need `match.call(expand.dots = TRUE)`
1 parent cc60e66 commit f2d7425

File tree

7 files changed

+24
-36
lines changed

7 files changed

+24
-36
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* `daemons()` creating new daemons returns invisibly the character compute profile created.
66
`daemons(0)` or `daemons(NULL)` resetting daemons returns invisible NULL (thanks @eliocamp, #384).
7+
* Calling `daemons()` will now reset any existing daemons for the same compute profile rather than error.
8+
This means that an explicit `daemons(0)` is no longer required before applying new settings (#383).
79

810
#### New Features
911

R/daemon.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@
7676
#' Setting to FALSE allows the daemon to persist indefinitely even when there is
7777
#' no longer a socket connection. This allows a host session to end and a new
7878
#' session to connect at the URL where the daemon is dialled in. Daemons must be
79-
#' terminated with `daemons(NULL)` in this case, which sends explicit exit
80-
#' signals to all connected daemons.
79+
#' terminated with `daemons(NULL)` in this case instead of `daemons(0)`. This
80+
#' sends explicit exit signals to all connected daemons.
8181
#'
8282
#' @export
8383
#'

R/daemons.R

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,18 @@
1212
#' Use `daemons(0)` to reset daemon connections:
1313
#' \itemize{
1414
#' \item All connected daemons and/or dispatchers exit automatically.
15-
#' \item \pkg{mirai} reverts to the default behaviour of creating a new
15+
#' \item Any as yet unresolved 'mirai' will return an 'errorValue' 19
16+
#' (Connection reset).
17+
#' \item [mirai()] reverts to the default behaviour of creating a new
1618
#' background process for each request.
17-
#' \item Any unresolved 'mirai' will return an 'errorValue' 19 (Connection
18-
#' reset) after a reset.
19-
#' \item Daemons must be reset before calling `daemons()` with revised
20-
#' settings for a compute profile. Daemons may be added at any time by using
21-
#' [launch_local()] or [launch_remote()] without needing to revise daemons
22-
#' settings.
2319
#' }
2420
#'
2521
#' If the host session ends, all connected dispatcher and daemon processes
26-
#' automatically exit as soon as their connections are dropped (unless the
27-
#' daemons were started with `autoexit = FALSE`).
22+
#' automatically exit as soon as their connections are dropped.
2823
#'
29-
#' To reset persistent daemons started with `autoexit = FALSE`, use
30-
#' `daemons(NULL)` instead, which also sends exit signals to all connected
31-
#' daemons prior to resetting.
24+
#' Calling [daemons()] implicitly resets any existing daemons for the compute
25+
#' profile with `daemons(0)`. Instead, [launch_local()] or [launch_remote()] may
26+
#' be used to add daemons at any time without resetting daemons.
3227
#'
3328
#' For historical reasons, `daemons()` with no arguments (other than
3429
#' optionally `.compute`) returns the value of [status()].
@@ -252,7 +247,8 @@ daemons <- function(
252247
on.exit()
253248
}
254249
} else {
255-
stop(sprintf(._[["daemons_set"]], .compute))
250+
daemons(0, .compute = .compute)
251+
return(eval(match.call()))
256252
}
257253
} else {
258254
signal <- is.null(n)
@@ -282,7 +278,8 @@ daemons <- function(
282278
}
283279
create_profile(envir, .compute, n, dots)
284280
} else {
285-
stop(sprintf(._[["daemons_set"]], .compute))
281+
daemons(0, .compute = .compute)
282+
return(eval(match.call()))
286283
}
287284
}
288285

R/mirai-package.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
list(
7070
arglen = "`n` must equal the length of `args`, or either must be 1",
7171
cluster_inactive = "cluster is no longer active",
72-
daemons_set = "daemons already set for `%s` compute profile",
7372
daemons_unset = "daemons must be set to use launchers",
7473
dot_required = "`.` must be an element of the character vector(s) supplied to `args`",
7574
function_required = "`.f` must be of type function, not %s",

man/daemon.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/daemons.Rd

Lines changed: 7 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tests.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ connection && {
8484
Sys.sleep(1L)
8585
test_equal("default", d <- daemons(1L, dispatcher = FALSE, asyncdial = FALSE, seed = 1546L))
8686
test_print(d)
87-
test_error(daemons(1L), "daemons already set")
8887
test_true(daemons_set())
8988
test_true(require_daemons(parent.frame()))
9089
me <- mirai(mirai::mirai(), .timeout = 2000L)[]
@@ -133,7 +132,6 @@ connection && {
133132
Sys.sleep(1L)
134133
test_type("integer", status(.compute = "new")[["connections"]])
135134
test_error(mirai_map(1:2, "a function", .compute = "new"), "must be of type function, not character")
136-
test_error(daemons(url = local_url(), .compute = "new"), "daemons already set")
137135
test_null(daemons(0L, .compute = "new"))
138136
}
139137
# additional daemons tests
@@ -259,7 +257,6 @@ connection && Sys.getenv("NOT_CRAN") == "true" && {
259257
test_zero(status[["mirai"]][["awaiting"]])
260258
test_zero(status[["mirai"]][["executing"]])
261259
test_zero(status[["mirai"]][["completed"]])
262-
test_null(daemons(0))
263260
test_nzchar(daemons(2, correcttype = NA))
264261
test_equal(daemons()[["connections"]], 2L)
265262
test_type("list", res <- mirai_map(c(1,1), rnorm)[.progress])
@@ -277,8 +274,6 @@ connection && Sys.getenv("NOT_CRAN") == "true" && {
277274
m <- mirai("Seattle", .timeout = 1000)
278275
if (!is_error_value(m[])) test_equal(m[], "Seattle")
279276
test_class("errorValue", mirai(q(), .timeout = 1000)[])
280-
test_null(daemons(0))
281-
Sys.sleep(0.5)
282277
test_nzchar(daemons(n = 1L, url = local_url(), dispatcher = TRUE))
283278
task <- mirai(substitute())
284279
url <- nextget("url")

0 commit comments

Comments
 (0)