rtargets-r-package

How to properly kill a targets pipeline


I use targets via RStudio Web and usually start a pipeline via tar_make in an RStudio job. I use crew as parallel backend. Whenever I stop a pipeline I do this by killing the RStudio job. But this leaves some processes working in the background. When I then start the next time (often for debugging puposes) I always receive an error message that a background process still works on the pipeline and must be killed. Only at this point I get the PID of this process and can kill it via Linux kill command. Afterwards I can start a pipeline again.

How can I properly kill a tar_make without pending processes in the background? Alternatively, how can I automate killing the pending stuff in the background?


Solution

  • A targets pipeline can be properly killed with the following function:

    tar_kill <- function(store = tar_config_get("store")) {
      pid <-
        readr::read_delim(
          file.path(store, "meta", "process"),
          delim = "|",
          col_types = cols(name = col_character(),
                           value = col_character())
        ) |> filter(name == "pid") |>
        select(value) |>
        mutate(value = as.integer(value))
      tools::pskill(pid$value)
    }