Merge pull request #1627 from nf-core/fix-prefetch

Use a template to add retrying to prefetch
This commit is contained in:
Harshil Patel 2022-05-06 19:43:43 +01:00 committed by GitHub
commit e36489d9e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 25 deletions

View file

@ -1,43 +1,26 @@
process SRATOOLS_PREFETCH {
tag "$id"
label 'process_low'
label 'error_retry'
conda (params.enable_conda ? 'bioconda::sra-tools=2.11.0' : null)
container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ?
'https://depot.galaxyproject.org/singularity/sra-tools:2.11.0--pl5262h314213e_0' :
'quay.io/biocontainers/sra-tools:2.11.0--pl5262h314213e_0' }"
'https://depot.galaxyproject.org/singularity/sra-tools:2.11.0--pl5321ha49a11a_3' :
'quay.io/biocontainers/sra-tools:2.11.0--pl5321ha49a11a_3' }"
input:
tuple val(meta), val(id)
output:
tuple val(meta), path("$id"), emit: sra
tuple val(meta), path(id), emit: sra
path "versions.yml" , emit: versions
when:
task.ext.when == null || task.ext.when
script:
def args = task.ext.args ?: ''
def config = "/LIBS/GUID = \"${UUID.randomUUID().toString()}\"\\n/libs/cloud/report_instance_identity = \"true\"\\n"
"""
eval "\$(vdb-config -o n NCBI_SETTINGS | sed 's/[" ]//g')"
if [[ ! -f "\${NCBI_SETTINGS}" ]]; then
mkdir -p "\$(dirname "\${NCBI_SETTINGS}")"
printf '${config}' > "\${NCBI_SETTINGS}"
fi
shell:
args = task.ext.args ?: ''
args2 = task.ext.args2 ?: '5 1 100' // <num retries> <base delay in seconds> <max delay in seconds>
config = "/LIBS/GUID = \"${UUID.randomUUID().toString()}\"\\n/libs/cloud/report_instance_identity = \"true\"\\n"
prefetch \\
$args \\
--progress \\
$id
vdb-validate $id
cat <<-END_VERSIONS > versions.yml
"${task.process}":
sratools: \$(prefetch --version 2>&1 | grep -Eo '[0-9.]+')
END_VERSIONS
"""
template 'retry_with_backoff.sh'
}

View file

@ -0,0 +1,59 @@
#!/usr/bin/env bash
set -u
retry_with_backoff() {
local max_attempts=${1}
local delay=${2}
local max_time=${3}
local attempt=1
local output=
local status=
# Remove the first three arguments to this function in order to access
# the 'real' command with `${@}`.
shift 3
while [ ${attempt} -le ${max_attempts} ]; do
output=$("${@}")
status=${?}
if [ ${status} -eq 0 ]; then
break
fi
if [ ${attempt} -lt ${max_attempts} ]; then
echo "Failed attempt ${attempt} of ${max_attempts}. Retrying in ${delay} s." >&2
sleep ${delay}
elif [ ${attempt} -eq ${max_attempts} ]; then
echo "Failed after ${attempt} attempts." >&2
return ${status}
fi
attempt=$(( ${attempt} + 1 ))
delay=$(( ${delay} * 2 ))
if [ ${delay} -ge ${max_time} ]; then
delay=${max_time}
fi
done
echo "${output}"
}
eval "$(vdb-config -o n NCBI_SETTINGS | sed 's/[" ]//g')"
if [[ ! -f "${NCBI_SETTINGS}" ]]; then
mkdir -p "$(dirname "${NCBI_SETTINGS}")"
printf '!{config}' > "${NCBI_SETTINGS}"
fi
retry_with_backoff !{args2} \
prefetch \
!{args} \
!{id}
vdb-validate !{id}
cat <<-END_VERSIONS > versions.yml
"!{task.process}":
sratools: $(prefetch --version 2>&1 | grep -Eo '[0-9.]+')
END_VERSIONS