Skip to content

Commit 35b5e0c

Browse files
Add support for runOSCommand(..., stderr = NA), which captures 'stderr' via file and separately from 'stdout'
1 parent 1a9fbd5 commit 35b5e0c

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: future.batchtools
2-
Version: 0.20.0-9015
2+
Version: 0.20.0-9016
33
Depends:
44
R (>= 3.2.0),
55
parallelly,

NEWS.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
## New Features
88

9-
* Add `makeClusterFunctionsSlurm2()`, which enhances
10-
`batchtools::makeClusterFunctionsSlurm()` by patching the
9+
* Add `makeClusterFunctionsSlurm2()`, which patches
10+
`batchtools::makeClusterFunctionsSlurm()`. Firstly, it patches the
1111
`listJobsQueued()` cluster function such that it falls back to
1212
querying Slurm's account database (`sacct`), if the future was
1313
_not_ found in the Slurm job queue (`squeue`), which might be the
1414
case when Slurm provisions a job that was just submitted to the
15-
scheduler.
15+
scheduler. Secondly, it patched the `submitJob()` cluster function
16+
such that the system call to `sbatch` captures stderr separately
17+
from stdout, which prevents auxillary INFO messages from `sbatch`
18+
to corrupt the output to be parsed.
1619

1720
## Bug Fixes
1821

R/makeClusterFutureSlurm2.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ patchClusterFunctionsSlurm2 <- function(cf) {
1212
nodename <- env[["nodename"]]
1313
org_listJobsQueued <- env[["listJobsQueued"]]
1414

15-
## Patch submitJob() to use runOSCommand(..., stderr = FALSE)
15+
## Patch submitJob() to use runOSCommand(..., stderr = NA),
16+
## which captures 'stderr' separately from 'stdout'
1617
## See https://github.com/mlr-org/batchtools/pull/314
1718
submitJob <- cf[["submitJob"]]
1819
env_submitJob <- new.env(parent = environment(submitJob))
19-
env_submitJob[["runOSCommand"]] <- function(..., stderr = FALSE) {
20+
env_submitJob[["runOSCommand"]] <- function(..., stderr = NA) {
2021
debug <- isTRUE(getOption("future.batchtools.debug"))
2122
if (debug) {
22-
mdebugf_push("runOSCommand(..., stderr = FALSE) ...")
23+
mdebugf_push("runOSCommand(..., stderr = %s) ...", stderr)
2324
mprint(list(args = list(..., stderr = stderr)))
2425
on.exit(mdebugf_pop())
2526
}

R/runOSCommand.R

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,30 @@ runOSCommand = function(sys.cmd, sys.args = character(0L), stdin = "", stdout =
1919
"!DEBUG [runOSCommand]: cmd: `sys.cmd` `stri_flatten(sys.args, ' ')`"
2020

2121
if (nzchar(Sys.which(sys.cmd))) {
22-
res = suppressWarnings(system2(command = sys.cmd, args = sys.args, stdin = stdin, stdout = stdout, stderr = stderr, wait = TRUE))
22+
## Capture stderr separately
23+
if (is.na(stderr)) {
24+
stderr_file = tempfile()
25+
on.exit(file.remove(stderr_file))
26+
} else {
27+
stderr_file = stderr
28+
}
29+
res = suppressWarnings(system2(command = sys.cmd, args = sys.args, stdin = stdin, stdout = stdout, stderr = stderr_file, wait = TRUE))
2330
output = as.character(res)
2431
exit.code = attr(res, "status") %??% 0L
32+
if (is.na(stderr)) {
33+
output_stderr = readLines(stderr_file, warn = FALSE)
34+
} else {
35+
output_stderr = NULL
36+
}
2537
} else {
2638
output = "command not found"
39+
output_stderr = NULL
2740
exit.code = 127L
2841
}
2942

3043
"!DEBUG [runOSCommand]: OS result (stdin '`stdin`', exit code `exit.code`):"
3144
"!DEBUG [runOSCommand]: `paste0(output, sep = '\n')`"
45+
"!DEBUG [runOSCommand]: `paste0(output_stderr, sep = '\n')`"
3246

33-
return(list(sys.cmd = sys.cmd, sys.args = sys.args, exit.code = exit.code, output = output))
47+
return(list(sys.cmd = sys.cmd, sys.args = sys.args, exit.code = exit.code, output = output, stderr = output_stderr))
3448
}

0 commit comments

Comments
 (0)