Skip to content

Commit

Permalink
return invisibly
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 9, 2025
1 parent 7c2cf3d commit 8503d87
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# mirai 2.0.0.9000 (development)

#### Updates

* `daemon()` now returns an integer exit value to indicate the reason for termination.

# mirai 2.0.0

#### New Architecture
Expand Down
32 changes: 22 additions & 10 deletions R/daemon.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@
#' automatically using L'Ecuyer-CMRG RNG streams generated by the host process
#' and should not be independently supplied.
#'
#' @return Integer exit code: 0L for normal termination, and a positive value if
#' exiting after reaching a self-imposed limit: 1L (idletime), 2L (walltime),
#' 3L (maxtasks).
#' @return Invisibly, an integer exit code: 0L for normal termination, and a
#' positive value if a self-imposed limit was reached: 1L (idletime), 2L
#' (walltime), 3L (maxtasks).
#'
#' @section Persistence:
#'
Expand Down Expand Up @@ -127,6 +127,7 @@ daemon <- function(url, dispatcher = FALSE, ..., asyncdial = FALSE, autoexit = T
}, add = TRUE)
}
snapshot()
xc <- 0L
task <- 1L
timeout <- if (idletime > walltime) walltime else if (is.finite(idletime)) idletime
maxtime <- if (is.finite(walltime)) mclock() + walltime else FALSE
Expand All @@ -135,16 +136,19 @@ daemon <- function(url, dispatcher = FALSE, ..., asyncdial = FALSE, autoexit = T
aio <- recv_aio(sock, mode = 1L, cv = cv)
if (is.numeric(id))
send(sock, c(.intmax, as.integer(id)), mode = 2L, block = TRUE)
wait(cv) || return(0L)
wait(cv) || return(invisible(xc))
serial <- collect_aio(aio)
if (is.list(serial))
`opt<-`(sock, "serial", serial)
repeat {
aio <- recv_aio(sock, mode = 1L, timeout = timeout, cv = cv)
wait(cv) || return(0L)
wait(cv) || break
m <- collect_aio(aio)
is.integer(m) && {
m == 5L && return(1L)
m == 5L && {
xc <- 1L
break
}
next
}
cancel <- recv_aio(sock, mode = 8L, cv = NA)
Expand All @@ -154,8 +158,9 @@ daemon <- function(url, dispatcher = FALSE, ..., asyncdial = FALSE, autoexit = T
.mark()
send(sock, data, mode = 1L, block = TRUE)
aio <- recv_aio(sock, mode = 8L, cv = cv)
xc <- 2L + task >= maxtasks
wait(cv)
return(2L + task >= maxtasks)
break
}
send(sock, data, mode = 1L, block = TRUE)
if (cleanup) do_cleanup()
Expand All @@ -167,16 +172,23 @@ daemon <- function(url, dispatcher = FALSE, ..., asyncdial = FALSE, autoexit = T
aio <- recv_aio(ctx, mode = 1L, timeout = timeout, cv = cv)
wait(cv) || break
m <- collect_aio(aio)
is.integer(m) && return(1L)
is.integer(m) && {
xc <- 1L
break
}
data <- eval_mirai(m)
send(ctx, data, mode = 1L, block = TRUE)
if (cleanup) do_cleanup()
{ task >= maxtasks || maxtime && mclock() >= maxtime } &&
return(2L + task >= maxtasks)
{ task >= maxtasks || maxtime && mclock() >= maxtime } && {
xc <- 2L + task >= maxtasks
break
}
task <- task + 1L
}
}

invisible(xc)

}

#' dot Daemon
Expand Down
6 changes: 3 additions & 3 deletions man/daemon.Rd

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

0 comments on commit 8503d87

Please sign in to comment.