Skip to content

Commit

Permalink
check for crashed tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Jan 23, 2025
1 parent 71b02e1 commit 7128f78
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 deletions.
26 changes: 16 additions & 10 deletions R/class_monad.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,24 @@ as_monad <- function(task, name) {
if (is.list(out)) {
return(out)
}
if (!is.integer(out) || length(out) != 1L || anyNA(out)) {
status <- "error"
error <- as.character(out)
code <- -1L
} else {
code <- as.integer(out)
error <- nanonext::nng_error(code)
if (identical(code, 19L)) {
status <- "crash"
} else if (identical(code, 20L)) {
status <- "cancel"
}
}
monad_init(
name = name,
status = if_any(
identical(as.integer(out), 20L),
"canceled",
"error"
),
code = as.integer(out),
error = paste(
utils::capture.output(print(out), type = "output"),
collapse = "\n"
)
status = status,
error = error,
code = code
)
}

Expand Down
3 changes: 2 additions & 1 deletion R/crew_controller.R
Original file line number Diff line number Diff line change
Expand Up @@ -965,9 +965,10 @@ crew_class_controller <- R6::R6Class(
#' * `error`: the first 2048 characters of the error message if
#' the task threw an error, `NA` otherwise.
#' * `code`: an integer code denoting the specific exit status:
#' `0` for successful tasks, `1` for tasks with an error in the R
#' `0` for successful tasks, `-1` for tasks with an error in the R
#' command of the task, and another positive integer with an NNG
#' status code if there is an error at the NNG/`nanonext` level.
#' `nanonext::nng_error()` can interpret these codes.
#' * `trace`: the first 2048 characters of the text of the traceback
#' if the task threw an error, `NA` otherwise.
#' * `warnings`: the first 2048 characters. of the text of
Expand Down
2 changes: 1 addition & 1 deletion R/crew_eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ crew_eval <- function(
code <- 0L
} else {
status <- "error"
code <- 1L
code <- -1L
}
if (is.null(trace)) {
trace <- NA_character_
Expand Down
3 changes: 2 additions & 1 deletion man/crew_class_controller.Rd

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

33 changes: 33 additions & 0 deletions tests/testthat/test-class_monad.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,36 @@ crew_test("bad field", {
out$nope <- 0
expect_crew_error(monad_validate(out))
})

crew_test("as_monad()", {
skip_on_cran()
task <- structure(list(data = list(2L)), class = c("mirai", "recvAio"))
expect_equal(as_monad(task = task, name = "x"), list(2L))
task <- mirai::mirai(stop("error_message"))
mirai::call_mirai_(task)
monad <- as_monad(task = task, name = "x")
expect_equal(monad$name, "x")
expect_equal(monad$result, list(NA))
expect_equal(monad$status, "error")
expect_true(grepl("error_message", monad$error))
expect_equal(monad$code, -1L)
task <- mirai::mirai(Sys.sleep(300))
mirai::stop_mirai(task)
monad <- as_monad(task = task, name = "x")
expect_equal(monad$name, "x")
expect_equal(monad$result, list(NA))
expect_equal(monad$status, "cancel")
expect_true(grepl("cancel", tolower(monad$error)))
expect_equal(monad$code, 20L)
on.exit(mirai::daemons(n = 0L))
mirai::daemons(n = 1L)
task <- mirai::mirai(Sys.sleep(300))
Sys.sleep(0.25)
mirai::daemons(n = 0L)
monad <- as_monad(task = task, name = "x")
expect_equal(monad$name, "x")
expect_equal(monad$result, list(NA))
expect_equal(monad$status, "crash")
expect_true(grepl("connection reset", tolower(monad$error)))
expect_equal(monad$code, 19L)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-crew_controller_local.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ crew_test("exit status and code", {
x$wait(mode = "one")
task <- x$pop()
expect_equal(task$status, "error")
expect_equal(task$code, 1L)
expect_equal(task$code, -1L)
x$push(Sys.sleep(10000), name = "long")
x$cancel(names = "long")
x$wait()
task <- x$pop()
expect_equal(task$status, "canceled")
expect_equal(task$status, "cancel")
expect_equal(task$code, 20L)
expect_equal(x$client$resolved(), 3L)
})
Expand Down
3 changes: 2 additions & 1 deletion vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ Here is the full list of output in the `task` object returned by `pop()`.
* `error`: the first 2048 characters of the error message if
the task threw an error, `NA` otherwise.
* `code`: an integer code denoting the specific exit status:
`0` for successful tasks, `1` for tasks with an error in the R
`0` for successful tasks, `-1` for tasks with an error in the R
command of the task, and another positive integer with an NNG
status code if there is an error at the NNG/`nanonext` level.
`nanonext::nng_error()` can interpret these codes.
* `trace`: the first 2048 characters of the text of the traceback
if the task threw an error, `NA` otherwise.
* `warnings`: the first 2048 characters. of the text of
Expand Down

0 comments on commit 7128f78

Please sign in to comment.