Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit a0d5537

Browse files
authored
Improve R console UI experience (#193)
* Improve UI experience * Added verbose mode to deleteJob and deleteStorageContainer * Refactor print method
1 parent 89dbba9 commit a0d5537

File tree

4 files changed

+96
-26
lines changed

4 files changed

+96
-26
lines changed

R/doAzureParallel.R

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,20 @@ setHttpTraffic <- function(value = FALSE) {
489489
}
490490
}
491491

492-
cat("Job Summary: ", fill = TRUE)
492+
493493
job <- rAzureBatch::getJob(id)
494-
cat(sprintf("Id: %s", job$id), fill = TRUE)
494+
495+
printJobInformation(
496+
jobId = job$id,
497+
chunkSize = chunkSize,
498+
enableCloudCombine = enableCloudCombine,
499+
errorHandling = obj$errorHandling,
500+
wait = wait,
501+
autoDeleteJob = autoDeleteJob,
502+
cranPackages = obj$packages,
503+
githubPackages = githubPackages,
504+
bioconductorPackages = bioconductorPackages
505+
)
495506

496507
if (!is.null(job$id)) {
497508
saveMetadataBlob(job$id, metadata)
@@ -531,12 +542,16 @@ setHttpTraffic <- function(value = FALSE) {
531542
containerImage = data$containerImage
532543
)
533544

545+
cat("\r", sprintf("Submitting tasks (%s/%s)", i, length(endIndices)), sep = "")
546+
flush.console()
547+
534548
return(taskId)
535549
})
536550

537551
rAzureBatch::updateJob(id)
538552

539553
if (enableCloudCombine) {
554+
cat("\nSubmitting merge task")
540555
mergeTaskId <- paste0(id, "-merge")
541556
.addTask(
542557
jobId = id,
@@ -554,6 +569,7 @@ setHttpTraffic <- function(value = FALSE) {
554569
outputFiles = obj$options$azure$outputFiles,
555570
containerImage = data$containerImage
556571
)
572+
cat(". . .")
557573
}
558574

559575
if (wait) {
@@ -603,12 +619,10 @@ setHttpTraffic <- function(value = FALSE) {
603619
errorValue <- foreach::getErrorValue(it)
604620
errorIndex <- foreach::getErrorIndex(it)
605621

606-
cat(sprintf("Number of errors: %i", numberOfFailedTasks),
607-
fill = TRUE)
608-
609622
# delete job from batch service and job result from storage blob
610623
if (autoDeleteJob) {
611-
deleteJob(id)
624+
# Default behavior is to delete the job data
625+
deleteJob(id, verbose = !autoDeleteJob)
612626
}
613627

614628
if (identical(obj$errorHandling, "stop") &&

R/jobUtilities.R

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,28 @@ getJobResult <- function(jobId) {
258258
#' deleteJob("job-001")
259259
#' }
260260
#' @export
261-
deleteJob <- function(jobId) {
262-
deleteStorageContainer(jobId)
261+
deleteJob <- function(jobId, verbose = TRUE) {
262+
deleteStorageContainer(jobId, verbose)
263263

264264
response <- rAzureBatch::deleteJob(jobId, content = "response")
265265

266-
if (response$status_code == 202) {
267-
cat(sprintf("Your job '%s' has been deleted.", jobId),
268-
fill = TRUE)
269-
} else if (response$status_code == 404) {
270-
cat(sprintf("Job '%s' does not exist.", jobId),
271-
fill = TRUE)
272-
}
266+
tryCatch({
267+
httr::stop_for_status(response)
268+
269+
if (verbose) {
270+
cat(sprintf("Your job '%s' has been deleted.", jobId),
271+
fill = TRUE)
272+
}
273+
},
274+
error = function(e) {
275+
if (verbose) {
276+
response <- httr::content(response, encoding = "UTF-8")
277+
cat("Call: deleteJob", fill = TRUE)
278+
cat(sprintf("Exception: %s", response$message$value),
279+
fill = TRUE)
280+
}
281+
}
282+
)
273283
}
274284

275285
#' Terminate a job
@@ -301,7 +311,7 @@ terminateJob <- function(jobId) {
301311
#' @export
302312
waitForTasksToComplete <-
303313
function(jobId, timeout, errorHandling = "stop") {
304-
cat("Waiting for tasks to complete. . .", fill = TRUE)
314+
cat("\nWaiting for tasks to complete. . .", fill = TRUE)
305315

306316
totalTasks <- 0
307317
currentTasks <- rAzureBatch::listTask(jobId)

R/storage_management.R

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,29 @@ listStorageContainers <- function(prefix = "") {
3434
#' @param container The name of the container
3535
#'
3636
#' @export
37-
deleteStorageContainer <- function(container) {
37+
deleteStorageContainer <- function(container, verbose = TRUE) {
3838
response <-
3939
rAzureBatch::deleteContainer(container, content = "response")
4040

41-
if (response$status_code == 202) {
42-
cat(sprintf("Your storage container '%s' has been deleted.", container),
43-
fill = TRUE)
44-
} else if (response$status_code == 404) {
45-
cat(sprintf("storage container '%s' does not exist.", container),
46-
fill = TRUE)
47-
}
48-
49-
response
41+
tryCatch({
42+
httr::stop_for_status(response)
43+
44+
if (verbose) {
45+
cat(sprintf("Your storage container '%s' has been deleted.", jobId),
46+
fill = TRUE)
47+
}
48+
},
49+
error = function(e) {
50+
# Checking for status code instead of using xml2 package
51+
# Storage helper functions require xml2 package which requires special installations
52+
if (verbose && response$status_code == 404) {
53+
cat(sprintf("Call: deleteStorageContainer"),
54+
fill = TRUE)
55+
cat(sprintf("Exception: %s", "The specified storage container does not exist"),
56+
fill = TRUE)
57+
}
58+
}
59+
)
5060
}
5161

5262
#' List storage files from Azure storage.

R/stringUtilities.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,39 @@ getTaskFailedErrorString <- function(...) {
1414

1515
return(errorMessage)
1616
}
17+
18+
getJobPackageSummary <- function(packages) {
19+
if (length(packages) > 0) {
20+
cat(sprintf("%s: ", deparse(substitute(packages))), fill = TRUE)
21+
cat("\t")
22+
for (i in 1:length(packages)) {
23+
cat(packages[i], "; ", sep = "")
24+
}
25+
cat("\n")
26+
}
27+
}
28+
29+
printJobInformation <- function(jobId,
30+
chunkSize,
31+
enableCloudCombine,
32+
errorHandling,
33+
wait,
34+
autoDeleteJob,
35+
cranPackages,
36+
githubPackages,
37+
bioconductorPackages) {
38+
cat(strrep('=', options("width")), fill = TRUE)
39+
cat(sprintf("Id: %s", jobId), fill = TRUE)
40+
cat(sprintf("chunkSize: %s", as.character(chunkSize)), fill = TRUE)
41+
cat(sprintf("enableCloudCombine: %s", as.character(enableCloudCombine)), fill = TRUE)
42+
43+
packages <- cranPackages
44+
getJobPackageSummary(packages)
45+
getJobPackageSummary(githubPackages)
46+
getJobPackageSummary(bioconductorPackages)
47+
48+
cat(sprintf("errorHandling: %s", as.character(errorHandling)), fill = TRUE)
49+
cat(sprintf("wait: %s", as.character(wait)), fill = TRUE)
50+
cat(sprintf("autoDeleteJob: %s", as.character(autoDeleteJob)), fill = TRUE)
51+
cat(strrep('=', options("width")), fill = TRUE)
52+
}

0 commit comments

Comments
 (0)