From 626647fd0a4d26017a7f94533cf0abd370ff3c8b Mon Sep 17 00:00:00 2001 From: wlandau Date: Thu, 27 Apr 2023 05:11:04 -0400 Subject: [PATCH] Make sure all job scripts get deleted on terminate --- R/crew_launcher_sge.R | 30 ++++++++++++++++++++++++++---- R/utils_names.R | 4 ++-- man/crew_class_launcher_sge.Rd | 2 ++ tests/testthat/test-utils_names.R | 3 +-- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/R/crew_launcher_sge.R b/R/crew_launcher_sge.R index d218814..92c8432 100644 --- a/R/crew_launcher_sge.R +++ b/R/crew_launcher_sge.R @@ -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()]. @@ -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, @@ -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 diff --git a/R/utils_names.R b/R/utils_names.R index ebe989e..4e8eade 100644 --- a/R/utils_names.R +++ b/R/utils_names.R @@ -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)) } diff --git a/man/crew_class_launcher_sge.Rd b/man/crew_class_launcher_sge.Rd index 30a378c..c49e2de 100644 --- a/man/crew_class_launcher_sge.Rd +++ b/man/crew_class_launcher_sge.Rd @@ -55,6 +55,8 @@ Other launchers: \item{\code{sge_lines}}{See \code{\link[=crew_launcher_sge]{crew_launcher_sge()}}.} \item{\code{verbose}}{See \code{\link[=crew_launcher_sge]{crew_launcher_sge()}}.} + +\item{\code{prefix}}{Unique prefix of worker scripts.} } \if{html}{\out{}} } diff --git a/tests/testthat/test-utils_names.R b/tests/testthat/test-utils_names.R index f3c97d5..130cef1 100644 --- a/tests/testthat/test-utils_names.R +++ b/tests/testthat/test-utils_names.R @@ -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")) })