diff --git a/R/array_submit.R b/R/array_submit.R index 9a23b0f..7d8fbcb 100644 --- a/R/array_submit.R +++ b/R/array_submit.R @@ -103,25 +103,29 @@ array_submit <- function(name, task_ids = NULL, submit = FALSE, restore = TRUE, # Find the log associated with that highest task #----------------------------------------------------------------------- - max_log <- job_original[grep("^#SBATCH -o ", job_original)] |> - str_extract("-o (.*)$", group = 1) |> - str_replace("%a", max_task) - - # For jobs created with 'job_loop', the actual log used will be on the - # line where 'log_path' is defined, not '/dev/null' - if (!is.na(max_log) && (max_log == "/dev/null")) { - max_log <- job_original[grep("^log_path=", job_original)] |> - str_extract("^log_path=(.*)/", group = 1) |> - list.files( - pattern = sprintf( - "%s.*_%s\\.txt$", - name, - max_task - ), - full.names = TRUE - ) + # For 'job_loop', use the path in the line starting with 'log_path='. + # Otherwise use the log in the line starting with '#SBATCH -o'. + last_occurence = rev(grep("^(#SBATCH -o |log_path=)", job_original))[1] + max_log = job_original[last_occurence] |> + # Extract just the path + str_replace("^(#SBATCH -o |log_path=)", "") |> + # Set the path for the highest task + str_replace("%a|\\$\\{SLURM_ARRAY_TASK_ID\\}", max_task) + + # If the log is specified with a relative path, make sure it's + # relative to the directory containing the shell script + if (!grepl('^/', max_log)) { + max_log = file.path(dirname(sh_file), max_log) } + # Find the precise path of the log in a way that's compatible with + # both 'job_single' and 'job_loop' + max_log = list.files( + dirname(max_log), + pattern = sprintf("^%s.*_%s\\.txt$", name, max_task), + full.names = TRUE + ) + if (verbose) { message( sprintf(