Skip to content

Commit 436ba7e

Browse files
Modernize debug output
1 parent a8ab41b commit 436ba7e

File tree

6 files changed

+91
-60
lines changed

6 files changed

+91
-60
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.12.1-9009
2+
Version: 0.12.1-9010
33
Depends:
44
R (>= 3.2.0),
55
parallelly,

R/BatchtoolsFuture-class.R

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ cancel.BatchtoolsFuture <- function(x, interrupt = FALSE, ...) {
214214

215215
#' @importFrom batchtools getStatus
216216
status <- function(future, ...) {
217-
debug <- getOption("future.debug", FALSE)
217+
debug <- isTRUE(getOption("future.debug"))
218218
if (debug) {
219219
mdebug("status() for ", class(future)[1], " ...")
220220
on.exit(mdebug("status() for ", class(future)[1], " ... done"), add = TRUE)
@@ -224,7 +224,7 @@ status <- function(future, ...) {
224224
get_status <- function(...) {
225225
## Temporarily disable batchtools output?
226226
## (i.e. messages and progress bars)
227-
debug <- getOption("future.debug", FALSE)
227+
debug <- isTRUE(getOption("future.debug"))
228228
batchtools_output <- getOption("future.batchtools.output", debug)
229229
if (!batchtools_output) {
230230
oopts <- options(batchtools.verbose = FALSE, batchtools.progress = FALSE)
@@ -271,7 +271,7 @@ status <- function(future, ...) {
271271
## Cache result
272272
future$.status <- status
273273

274-
if (debug) mdebug("- status: ", paste(sQuote(status), collapse = ", "))
274+
if (debug) mdebug("Status: ", paste(sQuote(status), collapse = ", "))
275275

276276
status
277277
}
@@ -380,53 +380,64 @@ resolved.BatchtoolsFuture <- function(x, ...) {
380380
#' @export
381381
#' @keywords internal
382382
result.BatchtoolsFuture <- function(future, cleanup = TRUE, ...) {
383-
384-
debug <- getOption("future.debug", FALSE)
383+
debug <- isTRUE(getOption("future.debug"))
385384
if (debug) {
386-
mdebug("result() for BatchtoolsFuture ...")
387-
on.exit(mdebug("result() for BatchtoolsFuture ... done"), add = TRUE)
385+
mdebugf_push("result() for %s ...", class(future)[1])
386+
on.exit(mdebug_pop())
388387
}
389388

390389
## Has the value already been collected?
391390
result <- future$result
392391
if (inherits(result, "FutureResult")) {
393-
if (debug) mdebug("- FutureResult already collected")
392+
if (debug) mdebug("FutureResult already collected")
394393
return(result)
395394
}
396395

397396
## Has the value already been collected? - take two
398397
if (future$state %in% c("finished", "failed", "interrupted")) {
399-
if (debug) mdebug("- FutureResult already collected - take 2")
398+
if (debug) mdebug("FutureResult already collected - take 2")
400399
return(NextMethod())
401400
}
402401

403402
if (future$state == "created") {
404-
if (debug) mdebug("- starting future ...")
405-
future <- run(future)
406-
if (debug) mdebug("- starting future ... done")
403+
future <- local({
404+
if (debug) {
405+
mdebug_push("Starting future ...")
406+
on.exit(mdebug_pop())
407+
}
408+
run(future)
409+
})
407410
}
408411

409-
if (debug) mdebug("- getting batchtools status")
412+
if (debug) mdebug("Getting batchtools status")
410413
stat <- status(future)
411414
if (is_na(stat)) {
412415
label <- sQuoteLabel(future)
413416
stopf("The result no longer exists (or never existed) for Future ('%s') of class %s", label, paste(sQuote(class(future)), collapse = ", ")) #nolint
414417
}
415418

416-
if (debug) mdebug("- waiting for batchtools job to finish ...")
417-
result <- await(future, cleanup = FALSE)
418-
if (debug) mdebug("- waiting for batchtools job to finish ... done")
419+
result <- local({
420+
if (debug) {
421+
mdebug_push("Waiting for batchtools job to finish ...")
422+
on.exit(mdebug_pop())
423+
}
424+
await(future, cleanup = FALSE)
425+
})
419426
stop_if_not(inherits(result, "FutureResult"))
420427
future$result <- result
421428
future$state <- "finished"
422429

423430
if (cleanup) {
424-
if (debug) mdebugf("- delete %s ...", class(future)[1])
425-
delete(future)
426-
if (debug) mdebugf("- delete %s ... done", class(future)[1])
431+
local({
432+
if (debug) {
433+
mdebugf_push("Delete %s ...", class(future)[1])
434+
on.exit(mdebug_pop())
435+
}
436+
delete(future)
437+
})
427438
}
428439

429-
if (debug) mdebug("- NextMethod()")
440+
if (debug) mdebug("NextMethod()")
430441
NextMethod()
431442
}
432443

@@ -436,6 +447,12 @@ result.BatchtoolsFuture <- function(future, cleanup = TRUE, ...) {
436447
#' @importFrom utils capture.output str
437448
#' @export
438449
run.BatchtoolsFuture <- function(future, ...) {
450+
debug <- isTRUE(getOption("future.debug"))
451+
if (debug) {
452+
mdebugf_push("run() for %s ...", class(future)[1])
453+
on.exit(mdebug_pop())
454+
}
455+
439456
if (future$state != "created") {
440457
label <- sQuoteLabel(future)
441458
msg <- sprintf("A future ('%s') can only be launched once.", label)
@@ -449,7 +466,6 @@ run.BatchtoolsFuture <- function(future, ...) {
449466

450467
## Temporarily disable batchtools output?
451468
## (i.e. messages and progress bars)
452-
debug <- getOption("future.debug", FALSE)
453469
batchtools_output <- getOption("future.batchtools.output", debug)
454470
if (!batchtools_output) {
455471
oopts <- options(batchtools.verbose = FALSE, batchtools.progress = FALSE)
@@ -467,7 +483,7 @@ run.BatchtoolsFuture <- function(future, ...) {
467483
reg <- future$config$reg
468484
stop_if_not(is.null(reg) || inherits(reg, "Registry"))
469485
if (is.null(reg)) {
470-
if (debug) mprint("- Creating batchtools registry")
486+
if (debug) mdebug("Creating batchtools registry")
471487
config <- future$config
472488
stop_if_not(is.list(config))
473489

@@ -491,7 +507,7 @@ run.BatchtoolsFuture <- function(future, ...) {
491507
## (ii) Attach packages that needs to be attached
492508
packages <- future$packages
493509
if (length(packages) > 0) {
494-
mdebugf("Attaching %d packages (%s) ...",
510+
mdebugf_push("Attaching %d packages (%s) ...",
495511
length(packages), hpaste(sQuote(packages)))
496512

497513
## Record which packages in 'pkgs' that are loaded and
@@ -507,8 +523,7 @@ run.BatchtoolsFuture <- function(future, ...) {
507523
saveRegistry(reg = reg)
508524
})
509525

510-
mdebugf("Attaching %d packages (%s) ... DONE",
511-
length(packages), hpaste(sQuote(packages)))
526+
mdebug_pop()
512527
}
513528
## Not needed anymore
514529
packages <- NULL
@@ -519,7 +534,7 @@ run.BatchtoolsFuture <- function(future, ...) {
519534
}
520535

521536
## 1. Add to batchtools for evaluation
522-
mdebug("batchtools::batchMap()")
537+
if (debug) mdebug("batchtools::batchMap()")
523538
## WORKAROUND: batchtools::batchMap() updates the RNG state,
524539
## which we must make sure to undo.
525540
with_stealth_rng({
@@ -535,7 +550,7 @@ run.BatchtoolsFuture <- function(future, ...) {
535550

536551
## 3. Update
537552
future$config$jobid <- jobid
538-
mdebugf("Created %s future #%d", class(future)[1], jobid$job.id)
553+
if (debug) mdebugf("Created %s future #%d", class(future)[1], jobid$job.id)
539554

540555
## WORKAROUND: (For multicore and macOS only)
541556
if (reg$cluster.functions$name == "Multicore") {
@@ -607,31 +622,34 @@ await <- function(future, cleanup = TRUE,
607622
stop_if_not(is.finite(timeout), timeout >= 0)
608623
stop_if_not(is.finite(alpha), alpha > 0)
609624

610-
debug <- getOption("future.debug", FALSE)
611-
if (debug) mdebug("future.batchtools:::await() ...")
625+
debug <- isTRUE(getOption("future.debug"))
626+
if (debug) {
627+
mdebug_push("future.batchtools:::await() ...")
628+
on.exit(mdebug_pop())
629+
}
612630

613631
expr <- future$expr
614632
config <- future$config
615633
reg <- config$reg
616634
stop_if_not(inherits(reg, "Registry"))
617635
jobid <- config$jobid
618636

619-
mdebug("batchtools::waitForJobs() ...")
637+
if (debug) mdebug_push("batchtools::waitForJobs() ...")
620638

621639
## Control batchtools info output
622640
oopts <- options(batchtools.verbose = debug)
623-
on.exit(options(oopts))
641+
on.exit(options(oopts), add = TRUE)
624642

625643
## Sleep function - increases geometrically as a function of iterations
626644
sleep_fcn <- function(i) delta * alpha ^ (i - 1)
627645

628646
res <- waitForJobs(ids = jobid, timeout = timeout, sleep = sleep_fcn,
629647
stop.on.error = FALSE, reg = reg)
630-
if (debug) mdebugf("- batchtools::waitForJobs(): %s", res)
648+
if (debug) mdebugf("batchtools::waitForJobs(): %s", res)
631649
stat <- status(future)
632650
if (debug) {
633-
mdebugf("- status(): %s", paste(sQuote(stat), collapse = ", "))
634-
mdebug("batchtools::waitForJobs() ... done")
651+
mdebugf("status(): %s", paste(sQuote(stat), collapse = ", "))
652+
mdebug_pop()
635653
}
636654

637655
finished <- is_na(stat) || any(c("finished", "error", "expired") %in% stat)
@@ -644,16 +662,20 @@ await <- function(future, cleanup = TRUE,
644662
mdebug("Results:")
645663
label <- sQuoteLabel(future)
646664
if ("finished" %in% stat) {
647-
if (debug) mdebug("- batchtools::loadResult() ...")
648-
result <- loadResult(reg = reg, id = jobid)
649-
if (debug) mdebug("- batchtools::loadResult() ... done")
665+
result <- local({
666+
if (debug) {
667+
mdebug_push("batchtools::loadResult() ...")
668+
on.exit(mdebug_pop())
669+
}
670+
loadResult(reg = reg, id = jobid)
671+
})
650672

651673
if (inherits(result, "FutureResult")) {
652674
prototype_fields <- c(prototype_fields, "batchtools_log")
653675
result[["batchtools_log"]] <- try(local({
654676
if (debug) {
655-
mdebug("- batchtools::getLog() ...")
656-
on.exit(mdebug("- batchtools::getLog() ... done"))
677+
mdebug_push("batchtools::getLog() ...")
678+
on.exit(mdebug_pop())
657679
}
658680
## Since we're already collected the results, the log file
659681
## should already exist, if it exists. Because of this,
@@ -704,8 +726,6 @@ await <- function(future, cleanup = TRUE,
704726
delete(future, delta = 0.5 * delta, ...)
705727
}
706728

707-
if (debug) mdebug("future.batchtools:::await() ... done")
708-
709729
result
710730
} # await()
711731

@@ -742,7 +762,7 @@ delete.BatchtoolsFuture <- function(future,
742762
onMissing <- match.arg(onMissing)
743763
onFailure <- match.arg(onFailure)
744764

745-
debug <- getOption("future.debug", FALSE)
765+
debug <- isTRUE(getOption("future.debug"))
746766

747767
## Identify registry
748768
config <- future$config
@@ -757,7 +777,7 @@ delete.BatchtoolsFuture <- function(future,
757777
if (is.null(path) || !file_test("-d", path)) {
758778
if (onMissing %in% c("warning", "error")) {
759779
msg <- sprintf("Cannot remove batchtools registry, because directory does not exist: %s", sQuote(path)) #nolint
760-
mdebugf("delete(): %s", msg)
780+
if (debug) mdebugf("delete(): %s", msg)
761781
if (onMissing == "warning") {
762782
warning(msg)
763783
} else if (onMissing == "error") {
@@ -774,7 +794,7 @@ delete.BatchtoolsFuture <- function(future,
774794
status <- status(future)
775795
label <- sQuoteLabel(future)
776796
msg <- sprintf("Will not remove batchtools registry, because is appears to hold a non-resolved future (%s; state = %s; batchtools status = %s): %s", sQuote(label), sQuote(future$state), paste(sQuote(status), collapse = ", "), sQuote(path)) #nolint
777-
mdebugf("delete(): %s", msg)
797+
if (debug) mdebugf("delete(): %s", msg)
778798
if (onRunning == "warning") {
779799
warning(msg)
780800
return(invisible(TRUE))
@@ -793,19 +813,23 @@ delete.BatchtoolsFuture <- function(future,
793813

794814
## To simplify post mortem troubleshooting in non-interactive sessions,
795815
## should the batchtools registry files be removed or not?
796-
mdebugf("delete(): Option 'future.delete = %s",
797-
sQuote(getOption("future.delete", "<NULL>")))
816+
if (debug) {
817+
mdebugf("delete(): Option 'future.delete = %s",
818+
sQuote(getOption("future.delete", "<NULL>")))
819+
}
798820
if (!getOption("future.delete", interactive())) {
799821
status <- status(future)
800822
res <- future$result
801823
if (inherits(res, "FutureResult")) {
802824
if (result_has_errors(res)) status <- unique(c("error", status))
803825
}
804-
mdebugf("delete(): status(<future>) = %s",
805-
paste(sQuote(status), collapse = ", "))
826+
if (debug) {
827+
mdebugf("delete(): status(<future>) = %s",
828+
paste(sQuote(status), collapse = ", "))
829+
}
806830
if (any(c("error", "expired") %in% status)) {
807831
msg <- sprintf("Will not remove batchtools registry, because the status of the batchtools was %s and option 'future.delete' is FALSE or running in an interactive session: %s", paste(sQuote(status), collapse = ", "), sQuote(path)) #nolint
808-
mdebugf("delete(): %s", msg)
832+
if (debug) mdebugf("delete(): %s", msg)
809833
warning(msg)
810834
return(invisible(FALSE))
811835
}
@@ -814,7 +838,7 @@ delete.BatchtoolsFuture <- function(future,
814838
## Have user disabled deletions?
815839
if (!getOption("future.delete", TRUE)) {
816840
msg <- sprintf("Option 'future.delete' is FALSE - will not delete batchtools registry: %s", sQuote(path))
817-
mdebugf("delete(): %s", msg)
841+
if (debug) mdebugf("delete(): %s", msg)
818842
return(invisible(FALSE))
819843
}
820844

@@ -845,7 +869,7 @@ delete.BatchtoolsFuture <- function(future,
845869
if (file_test("-d", path)) {
846870
if (onFailure %in% c("warning", "error")) {
847871
msg <- sprintf("Failed to remove batchtools registry: %s", sQuote(path))
848-
mdebugf("delete(): %s", msg)
872+
if (debug) mdebugf("delete(): %s", msg)
849873
if (onMissing == "warning") {
850874
warning(msg)
851875
} else if (onMissing == "error") {
@@ -855,7 +879,7 @@ delete.BatchtoolsFuture <- function(future,
855879
return(invisible(FALSE))
856880
}
857881

858-
mdebugf("delete(): batchtools registry deleted: %s", sQuote(path))
882+
if (debug) mdebugf("delete(): batchtools registry deleted: %s", sQuote(path))
859883

860884
invisible(TRUE)
861885
} # delete()
@@ -868,8 +892,8 @@ add_finalizer.BatchtoolsFuture <- function(future, debug = FALSE, ...) {
868892
## Register finalizer (will clean up registries etc.)
869893

870894
if (debug) {
871-
mdebug("add_finalizer() for ", sQuote(class(future)[1]), " ...")
872-
on.exit(mdebug("add_finalizer() for ", sQuote(class(future)[1]), " ... done"), add = TRUE)
895+
mdebugf_push("add_finalizer() for %s ...", sQuote(class(future)[1]))
896+
on.exit(mdebug_pop())
873897
}
874898

875899
reg.finalizer(future, f = function(f) {

R/batchtools_bash.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ makeClusterFunctionsBash <- function(template = "bash") {
3232

3333
script <- cfBrewTemplate(reg, text = template_text, jc = jc)
3434
output <- system2(bin, args = c(script), stdout = TRUE, stderr = TRUE)
35-
if (getOption("future.debug", FALSE)) {
36-
cat(paste(c(output, ""), collapse = "\n"), file = stderr())
35+
debug <- isTRUE(getOption("future.debug"))
36+
if (debug) {
37+
mdebug_push("makeClusterFunctionsBash() ...")
38+
mdebug(paste(c(output, ""), collapse = "\n"))
39+
on.exit(mdebug_pop())
3740
}
41+
3842
status <- attr(output, "status")
3943
if (is.null(status)) {
4044
status <- 0L

R/resources_OP.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#' Temporarily tweaks the resources for the current batchtools strategy
32
#'
43
#' @usage fassignment \%resources\% tweaks

R/temp_registry.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ temp_registry <- local({
66
make_registry <- function(cluster.functions = NULL, config = list(), ...) {
77
## Temporarily disable batchtools output?
88
## (i.e. messages and progress bars)
9-
debug <- getOption("future.debug", FALSE)
9+
debug <- isTRUE(getOption("future.debug"))
1010
batchtools_output <- getOption("future.batchtools.output", debug)
1111

1212
work.dir <- config$work.dir

R/waitForWorker.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ waitForWorker.BatchtoolsFuture <- function(future,
5555
delta = getOption("future.wait.interval", 0.2),
5656
alpha = getOption("future.wait.alpha", 1.01),
5757
...) {
58-
debug <- getOption("future.debug", FALSE)
58+
debug <- isTRUE(getOption("future.debug"))
59+
if (debug) {
60+
mdebugf_push("waitForWorker() for %s ...", class(future)[1])
61+
on.exit(mdebug_pop())
62+
}
5963

6064
stop_if_not(is.null(await) || is.function(await))
6165
workers <- as.integer(workers)

0 commit comments

Comments
 (0)