diff --git a/NEWS.md b/NEWS.md index 5be4e2d7..9253ad47 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,9 @@ * Fixed documentation regarding `store.job.collection` behavior in `makeClusterFunctions()` (#333, @n3orma) * Fixed `reduceResults()` passing the wrong `job` to `fun` when `init` is omitted: result `i + 1` was paired with job `i` (#336, @chenyangsu). * Fixed `Worker` (used by `makeClusterFunctionsSSH()`) failing on registry paths containing spaces. +* The "Work dir does not exist" error now includes the missing directory and the node name (#260). +* `getLog()` gains argument `missing.as.empty` to return an empty character vector instead of an error if the log file is missing (#239). +* Fixed `getLog()` and `showLog()` failing with "cannot open the connection" instead of reporting a missing log file if `fs.latency` is 0. # batchtools 0.9.18 diff --git a/R/Logs.R b/R/Logs.R index de861444..d5085727 100644 --- a/R/Logs.R +++ b/R/Logs.R @@ -1,7 +1,9 @@ #' @useDynLib batchtools fill_gaps readLog = function(id, missing.as.empty = FALSE, reg = getDefaultRegistry()) { log.file = getLogFiles(reg, id) - if (is.na(log.file) || !waitForFile(log.file, timeout = reg$cluster.functions$fs.latency, must.work = FALSE)) { + # waitForFile() returns TRUE without checking if fs.latency is 0 + fs.latency = reg$cluster.functions$fs.latency + if (is.na(log.file) || !(fs::file_exists(log.file) || (fs.latency > 0 && waitForFile(log.file, timeout = fs.latency, must.work = FALSE)))) { if (missing.as.empty) return(data.table(job.id = integer(0L), lines = character(0L))) stopf("Log file '%s' for job with id %i not available", log.file, id$job.id) @@ -82,9 +84,13 @@ grepLogs = function(ids = NULL, pattern, ignore.case = FALSE, fixed = FALSE, reg #' \code{getLog} returns the log as character vector. #' @template id #' @template reg +#' @param missing.as.empty [\code{logical(1)}]\cr +#' Only used by \code{getLog}. +#' If \code{TRUE}, return an empty character vector if the log file does not exist (yet). +#' Default is \code{FALSE}, which throws an error. #' @export #' @family debug -#' @return Nothing. +#' @return \code{showLog} returns nothing, \code{getLog} a character vector of log lines. #' @examples #' \dontshow{ batchtools:::example_push_temp(1) } #' tmp = makeRegistry(file.dir = NA, make.default = FALSE) @@ -116,8 +122,9 @@ showLog = function(id, reg = getDefaultRegistry()) { #' @export #' @rdname showLog -getLog = function(id, reg = getDefaultRegistry()) { +getLog = function(id, reg = getDefaultRegistry(), missing.as.empty = FALSE) { assertRegistry(reg, sync = TRUE) + assertFlag(missing.as.empty) id = convertId(reg, id) - extractLog(readLog(id, reg = reg), id) + extractLog(readLog(id, missing.as.empty = missing.as.empty, reg = reg), id) } diff --git a/R/doJobCollection.R b/R/doJobCollection.R index b34c6f81..99ed09bf 100644 --- a/R/doJobCollection.R +++ b/R/doJobCollection.R @@ -83,7 +83,7 @@ doJobCollection.JobCollection = function(jc, output = NULL) { # set work dir if (!fs::dir_exists(jc$work.dir)) - return(error("Work dir does not exist")) + return(error("Work dir does not exist: '%s' (node: '%s')", jc$work.dir, Sys.info()[["nodename"]])) local_dir(jc$work.dir) # load registry dependencies: packages, source files, ... diff --git a/man/showLog.Rd b/man/showLog.Rd index 8c9c267f..983125d6 100644 --- a/man/showLog.Rd +++ b/man/showLog.Rd @@ -7,7 +7,7 @@ \usage{ showLog(id, reg = getDefaultRegistry()) -getLog(id, reg = getDefaultRegistry()) +getLog(id, reg = getDefaultRegistry(), missing.as.empty = FALSE) } \arguments{ \item{id}{[\code{integer(1)} or \code{data.table}]\cr @@ -16,9 +16,14 @@ and exactly one row.} \item{reg}{[\code{\link{Registry}}]\cr Registry. If not explicitly passed, uses the default registry (see \code{\link{setDefaultRegistry}}).} + +\item{missing.as.empty}{[\code{logical(1)}]\cr +Only used by \code{getLog}. +If \code{TRUE}, return an empty character vector if the log file does not exist (yet). +Default is \code{FALSE}, which throws an error.} } \value{ -Nothing. +\code{showLog} returns nothing, \code{getLog} a character vector of log lines. } \description{ \code{showLog} opens the log in the pager. For customization, see \code{\link[base]{file.show}}. diff --git a/tests/testthat/test_doJobCollection.R b/tests/testthat/test_doJobCollection.R index 05f4ab57..738952be 100644 --- a/tests/testthat/test_doJobCollection.R +++ b/tests/testthat/test_doJobCollection.R @@ -51,3 +51,18 @@ test_that("doJobCollection signals slave errors", { expect_string(getErrorMessages(reg = reg)$message, fixed = "y_on_master") fs::file_delete(fn) }) + +test_that("doJobCollection reports missing work dir with path and node", { + reg = makeTestRegistry() + batchMap(identity, 1, reg = reg) + jc = makeJobCollection(1, reg = reg) + jc$work.dir = fs::path(fs::file_temp(), "nonexistent") + fn = fs::file_temp() + doJobCollection(jc, output = fn) + syncRegistry(reg = reg) + msg = getErrorMessages(reg = reg)$message + expect_string(msg, pattern = "^Work dir does not exist") + expect_string(msg, fixed = jc$work.dir) + expect_string(msg, fixed = Sys.info()[["nodename"]]) + fs::file_delete(fn) +}) diff --git a/tests/testthat/test_showLog.R b/tests/testthat/test_showLog.R index b4935ca8..875d1a28 100644 --- a/tests/testthat/test_showLog.R +++ b/tests/testthat/test_showLog.R @@ -40,3 +40,17 @@ test_that("empty log files", { expect_equal(getLog(1, reg = reg), character(0L)) }) + +test_that("getLog with missing log files", { + reg = makeTestRegistry() + batchMap(identity, 1, reg = reg) + expect_error(getLog(1, reg = reg), "not available") + expect_identical(getLog(1, missing.as.empty = TRUE, reg = reg), character(0L)) + + submitAndWait(reg) + fs::file_delete(getLogFiles(reg, 1)) + expect_error(getLog(1, reg = reg), "not available") + expect_identical(getLog(1, missing.as.empty = TRUE, reg = reg), character(0L)) + + expect_error(getLog(1, missing.as.empty = NA, reg = reg), "missing.as.empty") +})