From 3eaabb08bc4280a743a008195eb4644f9f08ba74 Mon Sep 17 00:00:00 2001 From: Samuel Jenness Date: Thu, 5 Mar 2026 17:47:31 -0500 Subject: [PATCH] Archive old step log files before resubmitting a step When a workflow step is rerun, existing .out files with a lower SLURM job ID are moved to log/archive/ before the new step is submitted. This keeps the main log/ directory clean and makes it easy to identify which logs belong to the current run. Closes #31 Co-Authored-By: Claude Opus 4.6 --- inst/templates/workflow/SWF/controller.sh | 3 +++ inst/templates/workflow/SWF/lib.sh | 30 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/inst/templates/workflow/SWF/controller.sh b/inst/templates/workflow/SWF/controller.sh index 0209914..4836b97 100755 --- a/inst/templates/workflow/SWF/controller.sh +++ b/inst/templates/workflow/SWF/controller.sh @@ -26,6 +26,9 @@ make_env_cur_vars "$SWF_CUR" # Run the next sbatch job if [[ -f "$SWF__JOB_SCRIPT" ]] then + # Archive old log files for this step before submitting + archive_old_logs "${SWF_NAME}_step${SWF_CUR}" "$SLURM_JOB_ID" "$SWF_LOG_DIR" + sbatch --dependency=afterany:"$SLURM_JOB_ID" \ --output="$SWF__STEPS_OUT" \ --job-name="${SWF_NAME}_step${SWF_CUR}" \ diff --git a/inst/templates/workflow/SWF/lib.sh b/inst/templates/workflow/SWF/lib.sh index 8981a00..bb87781 100644 --- a/inst/templates/workflow/SWF/lib.sh +++ b/inst/templates/workflow/SWF/lib.sh @@ -25,6 +25,36 @@ function make_env_cur_vars { export SWF__INSTRUCTIONS_SCRIPT="$SWF__CUR_DIR/instructions.sh" } +# Archive old log files for a step before resubmitting it. +# Moves .out files with a job ID lower than the current one to log/archive/. +# Arguments: +# $1 - step name (e.g., "wf_name_step1") +# $2 - current SLURM job ID (files with lower IDs are considered old) +# $3 - log directory path +function archive_old_logs { + local step_name="$1" + local current_jobid="$2" + local log_dir="$3" + local archive_dir="$log_dir/archive" + + for f in "$log_dir"/${step_name}_*.out; do + [ -f "$f" ] || continue + + local basename + basename=$(basename "$f") + # Remove the step_name_ prefix and .out suffix to get JOBID_TASKID + local remainder="${basename#${step_name}_}" + remainder="${remainder%.out}" + # Extract the job ID (part before the first _) + local file_jobid="${remainder%%_*}" + + if [[ "$file_jobid" =~ ^[0-9]+$ ]] && [ "$file_jobid" -lt "$current_jobid" ]; then + mkdir -p "$archive_dir" + mv "$f" "$archive_dir/" + fi + done +} + # convert CRLF endings to LF - `|| echo ""` prevents error when none is found function fix_crlf_files { local CRLF_FILES=$(find "$1" -type f | xargs file -F "::" | grep CRLF || echo "")