Skip to content

Commit

Permalink
Make sure all job scripts get deleted on terminate
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Apr 27, 2023
1 parent 7c01cc8 commit 626647f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
30 changes: 26 additions & 4 deletions R/crew_launcher_sge.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ crew_class_launcher_sge <- R6::R6Class(
sge_lines = NULL,
#' @field verbose See [crew_launcher_sge()].
verbose = NULL,
#' @field prefix Unique prefix of worker scripts.
prefix = NULL,
#' @description SGE launcher constructor.
#' @return an SGE launcher object.
#' @param name See [crew_launcher_sge()].
Expand Down Expand Up @@ -325,7 +327,12 @@ crew_class_launcher_sge <- R6::R6Class(
self$script(),
paste("R -e", shQuote(call))
)
script <- name_script(name = name)
self$prefix <- self$prefix %|||% crew::crew_random_name()
script <- name_script(
prefix = self$prefix,
launcher = launcher,
worker = worker
)
writeLines(text = lines, con = script)
system2(
command = self$sge_qsub,
Expand All @@ -334,21 +341,36 @@ crew_class_launcher_sge <- R6::R6Class(
stderr = if_any(self$verbose, "", FALSE),
wait = FALSE
)
name
list(
launcher = launcher,
worker = worker,
instance = instance
)
},
#' @description Terminate a local process worker.
#' @return `NULL` (invisibly).
#' @param handle A process handle object previously
#' returned by `launch_worker()`.
terminate_worker = function(handle) {
unlink(name_script(name = handle), force = TRUE)
script <- name_script(
prefix = self$prefix,
launcher = handle$launcher,
worker = handle$worker
)
unlink(script)
name <- name_job(
launcher = handle$launcher,
worker = handle$worker,
instance = handle$instance
)
system2(
command = self$sge_qdel,
args = shQuote(handle),
args = shQuote(name),
stdout = if_any(self$verbose, "", FALSE),
stderr = if_any(self$verbose, "", FALSE),
wait = FALSE
)
invisible()
},
#' @description Generate the job script.
#' @details Includes everything except the worker-instance-specific
Expand Down
4 changes: 2 additions & 2 deletions R/utils_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ name_job <- function(launcher, worker, instance) {
out
}

name_script <- function(name) {
file.path(tempdir(), paste0(name, ".sh"))
name_script <- function(prefix, launcher, worker) {
file.path(tempdir(), sprintf("%s-%s-%s.sh", prefix, launcher, worker))
}
2 changes: 2 additions & 0 deletions man/crew_class_launcher_sge.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions tests/testthat/test-utils_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ test_that("name_job()", {
})

test_that("name_script()", {
name <- name_job("a", "b", "c")
script <- name_script(name)
script <- name_script(prefix = "a", launcher = "b", worker = "c")
expect_equal(script, file.path(tempdir(), "a-b-c.sh"))
})

0 comments on commit 626647f

Please sign in to comment.