Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a new cleanup function to run_snakefile.sh file and then call it as part of main() function #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion 08-snakemake/run_snakefile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}

# Function to perform cleanup after successful run
cleanup_files() {
log_message "Checking conditions for cleanup..."

if [ -z "$(ls -A FAILED_CONFIG 2>/dev/null)" ] && [ $(ls -1 FAILED_DB 2>/dev/null | wc -l) -le 1 ]; then
log_message "FAILED_CONFIG is empty and FAILED_DB has at most one file. Starting cleanup..."

# Delete files with specified extensions
find . -maxdepth 1 -type f \( -name "*.processed" -o -name "*.apsimx" -o -name "*.txt" -o -name "*.met" \) -delete

# Delete processing status files
rm -f txt_files_processed db_files_sorted

log_message "Cleanup completed successfully"
return 0
else
log_message "FAILED_CONFIG is not empty or FAILED_DB has more than one file. Skipping cleanup."
return 1
fi
}


# Function to run a Snakefile with comprehensive retry and recovery logic
run_snakefile() {
local snakefile=$1
Expand Down Expand Up @@ -57,21 +79,35 @@ run_snakefile() {

# Main workflow execution
main() {
local workflow_success=true

# Process Snakefile_1
if ! run_snakefile "Snakefile_1" 1; then
log_message "Error: Text file processing failed"
workflow_success=false
exit 1
fi

# Process Snakefile_2
if ! run_snakefile "Snakefile_2" "$APSIM_JOBS"; then
log_message "Error: APSIM file processing failed"
workflow_success=false
exit 1
fi

log_message "Workflow completed successfully"
if [ "$workflow_success" = true ]; then
log_message "Workflow completed successfully"

# Attempt cleanup
if cleanup_files; then
log_message "Post-processing cleanup completed successfully"
else
log_message "Warning: Cleanup skipped due to failed conditions"
fi
fi
}


# Trap for handling interrupts
trap 'log_message "Workflow interrupted"' INT TERM

Expand Down