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

Commit 6c2fd92

Browse files
committed
add deleteJob to delete both job defintion and job result
1 parent 2106f23 commit 6c2fd92

File tree

6 files changed

+114
-3
lines changed

6 files changed

+114
-3
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(createOutputFile)
4+
export(deleteJob)
45
export(deleteStorageContainer)
56
export(deleteStorageFile)
67
export(generateClusterConfig)

R/doAzureParallel.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ setHttpTraffic <- function(value = FALSE) {
399399
stop("The specified job already exists.")
400400
}
401401

402+
saveMetadataBlob(id, metadata)
403+
402404
break
403405

404406
},

R/storage_management.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ deleteStorageContainer <- function(container) {
3939
rAzureBatch::deleteContainer(container, content = "response")
4040

4141
if (response$status_code == 202) {
42-
cat(sprintf("Your container '%s' has been deleted.", container),
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),
4346
fill = TRUE)
4447
}
4548

R/utility.R

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,34 @@ linuxWrapCommands <- function(commands = c()) {
5656
commandLine
5757
}
5858

59+
#' Delete a job
60+
#'
61+
#' @param jobId A job id
62+
#' @param deleteResult TRUE to delete job result in storage blob
63+
#' container, FALSE to keep the result in storage blob container.
64+
#'
65+
#' @examples
66+
#' \dontrun{
67+
#' deleteJob("job-001")
68+
#' deleteJob("job-001", deleteResult = FALSE)
69+
#' }
70+
#' @export
71+
deleteJob <- function(jobId, deleteResult = TRUE) {
72+
if (deleteResult == TRUE) {
73+
deleteStorageContainer(jobId)
74+
}
75+
76+
response <- rAzureBatch::deleteJob(jobId, content = "response")
77+
78+
if (response$status_code == 202) {
79+
cat(sprintf("Your job '%s' has been deleted.", jobId),
80+
fill = TRUE)
81+
} else if (response$status_code == 404) {
82+
cat(sprintf("Job '%s' does not exist.", jobId),
83+
fill = TRUE)
84+
}
85+
}
86+
5987
#' Get a list of job statuses from the given filter
6088
#'
6189
#' @param filter A filter containing job state
@@ -351,7 +379,14 @@ getJobResult <- function(jobId) {
351379
stop("jobId must contain at least 3 characters.")
352380
}
353381

354-
tempFile <- tempFile <- tempfile("getJobResult", fileext = ".rds")
382+
metadata <- readMetadataBlob(jobId)
383+
384+
if (metadata$enableCloudCombine == "FALSE" ) {
385+
cat("enalbeCloudCombine is set to FALSE, no job merge result is available", fill = TRUE)
386+
return()
387+
}
388+
389+
tempFile <- tempfile("getJobResult", fileext = ".rds")
355390

356391
results <- rAzureBatch::downloadBlob(
357392
jobId,
@@ -573,6 +608,48 @@ getXmlValues <- function(xmlResponse, xmlPath) {
573608
xml2::xml_text(xml2::xml_find_all(xmlResponse, xmlPath))
574609
}
575610

611+
saveMetadataBlob <- function(jobId, metadata) {
612+
xmlNode <- "<metadata>"
613+
if (length(metadata) > 0) {
614+
for (i in 1:length(metadata)) {
615+
xmlNode <-
616+
paste0(xmlNode,
617+
sprintf("<%s>%s</%s>", metadata[[i]]$name, metadata[[i]]$value, metadata[[i]]$name))
618+
}
619+
}
620+
xmlNode <- paste0(xmlNode, "</metadata>")
621+
saveXmlBlob(jobId, xmlNode, "metadata")
622+
}
623+
624+
saveXmlBlob <- function(jobId, xmlBlock, name) {
625+
xmlFile <- paste0(jobId, "-", name, ".rds")
626+
saveRDS(xmlBlock, file = xmlFile)
627+
rAzureBatch::uploadBlob(jobId, paste0(getwd(), "/", xmlFile))
628+
file.remove(xmlFile)
629+
}
630+
631+
readMetadataBlob <- function(jobId) {
632+
# xmlResponse <-
633+
# rAzureBatch::listBlobs(jobId , paste0(jobId, "-metadata.rds"), content = "parsed")
634+
tempFile <- tempfile(paste0(jobId, "-metadata"), fileext = ".rds")
635+
result <- rAzureBatch::downloadBlob(
636+
jobId,
637+
paste0(jobId, "-metadata.rds"),
638+
downloadPath = tempFile,
639+
overwrite = TRUE
640+
)
641+
result <- readRDS(tempFile)
642+
result <- xml2::as_xml_document(result)
643+
chunkSize <- getXmlValues(result, ".//chunkSize")
644+
packages <- getXmlValues(result, ".//packages")
645+
errorHandling <- getXmlValues(result, ".//errorHandling")
646+
enableCloudCombine <- getXmlValues(result, ".//enableCloudCombine")
647+
648+
metadata <- list(chunkSize = chunkSize, packages = packages, errorHandling = errorHandling, enableCloudCombine = enableCloudCombine)
649+
650+
metadata
651+
}
652+
576653
areShallowEqual <- function(a, b) {
577654
!is.null(a) && !is.null(b) && a == b
578655
}

docs/31-long-running-job.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ Once job is completed successfully, you can call getJobResult to retrieve the jo
8383

8484
Once you get the job result, you can delete the job.
8585
```R
86-
rAzureBatch::deleteJob(jobId)
86+
deleteJob(jobId)
87+
```
88+
89+
Please note deleteJob will delete the job at batch service, by default, it also deletes the storage container holding the job result. If you want to keep the job result around, you can set deleteResult parameter to FALSE
90+
```R
91+
deleteJob(jobId, deleteResult = FALSE)
8792
```
8893

8994
A [working sample](../samples/long_running_job/long_running_job.R) can be found in the samples directory.

man/deleteJob.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)