Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit c20ca8c

Browse files
authored
Merge pull request #69 from aws-samples/master
Add EXIT trap to nextflow entrypoint script
2 parents b590c3a + 18d599d commit c20ca8c

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

docs/orchestration/nextflow/nextflow-overview.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ ENTRYPOINT ["/opt/bin/nextflow.aws.sh"]
6868
The script used for the entrypoint is shown below. The first parameter should be a Nextflow "project". Nextflow supports pulling projects directly from Git repositories. This script also allows for projects to be specified as an S3 URI - a bucket and folder therein where you have staged your Nextflow scripts and supporting files (like additional config files). Any additional parameters are passed along to the Nextflow executable. Also, the script automatically configures some Nextflow values based on environment variables set by AWS Batch.
6969

7070
```bash
71+
#!/bin/bash
72+
73+
set -e # fail on any error
74+
7175
echo "=== ENVIRONMENT ==="
7276
echo `env`
7377

@@ -89,6 +93,9 @@ process.queue = "$NF_JOB_QUEUE"
8993
aws.batch.cliPath = "/home/ec2-user/miniconda/bin/aws"
9094
EOF
9195

96+
echo "=== CONFIGURATION ==="
97+
cat ~/.nextflow/config
98+
9299
# AWS Batch places multiple jobs on an instance
93100
# To avoid file path clobbering use the JobID and JobAttempt
94101
# to create a unique path
@@ -105,27 +112,39 @@ cd /opt/work/$GUID
105112
# .nextflow directory holds all session information for the current and past runs.
106113
# it should be `sync`'d with an s3 uri, so that runs from previous sessions can be
107114
# resumed
115+
echo "== Restoring Session Cache =="
108116
aws s3 sync --only-show-errors $NF_LOGSDIR/.nextflow .nextflow
109117

118+
function preserve_session() {
119+
# stage out session cache
120+
if [ -d .nextflow ]; then
121+
echo "== Preserving Session Cache =="
122+
aws s3 sync --only-show-errors .nextflow $NF_LOGSDIR/.nextflow
123+
fi
124+
125+
# .nextflow.log file has more detailed logging from the workflow run and is
126+
# nominally unique per run.
127+
#
128+
# when run locally, .nextflow.logs are automatically rotated
129+
# when syncing to S3 uniquely identify logs by the batch GUID
130+
if [ -f .nextflow.log ]; then
131+
echo "== Preserving Session Log =="
132+
aws s3 cp --only-show-errors .nextflow.log $NF_LOGSDIR/.nextflow.log.${GUID/\//.}
133+
fi
134+
}
135+
136+
trap preserve_session EXIT
137+
110138
# stage workflow definition
111139
if [[ "$NEXTFLOW_PROJECT" =~ ^s3://.* ]]; then
140+
echo "== Staging S3 Project =="
112141
aws s3 sync --only-show-errors --exclude 'runs/*' --exclude '.*' $NEXTFLOW_PROJECT ./project
113142
NEXTFLOW_PROJECT=./project
114143
fi
115144

116145
echo "== Running Workflow =="
117146
echo "nextflow run $NEXTFLOW_PROJECT $NEXTFLOW_PARAMS"
118147
nextflow run $NEXTFLOW_PROJECT $NEXTFLOW_PARAMS
119-
120-
# stage out session cache
121-
aws s3 sync --only-show-errors .nextflow $NF_LOGSDIR/.nextflow
122-
123-
# .nextflow.log file has more detailed logging from the workflow run and is
124-
# nominally unique per run.
125-
#
126-
# when run locally, .nextflow.logs are automatically rotated
127-
# when syncing to S3 uniquely identify logs by the batch GUID
128-
aws s3 cp --only-show-errors .nextflow.log $NF_LOGSDIR/.nextflow.log.${GUID/\//.}
129148
```
130149

131150
The `AWS_BATCH_JOB_ID` and `AWS_BATCH_JOB_ATTEMPT` are [environment variables that are automatically provided](https://docs.aws.amazon.com/batch/latest/userguide/job_env_vars.html) to all AWS Batch jobs. The `NF_WORKDIR`, `NF_LOGSDIR`, and `NF_JOB_QUEUE` variables are ones set by the Batch Job Definition ([see below](#batch-job-definition)).

src/containers/nextflow/nextflow.aws.sh

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# * NF_LOGSDIR: where caching and logging data are stored
88
# * NF_WORKDIR: where intermmediate results are stored
99

10+
set -e # fail on any error
1011

1112
echo "=== ENVIRONMENT ==="
1213
echo `env`
@@ -48,24 +49,36 @@ cd /opt/work/$GUID
4849
# .nextflow directory holds all session information for the current and past runs.
4950
# it should be `sync`'d with an s3 uri, so that runs from previous sessions can be
5051
# resumed
52+
echo "== Restoring Session Cache =="
5153
aws s3 sync --only-show-errors $NF_LOGSDIR/.nextflow .nextflow
5254

55+
function preserve_session() {
56+
# stage out session cache
57+
if [ -d .nextflow ]; then
58+
echo "== Preserving Session Cache =="
59+
aws s3 sync --only-show-errors .nextflow $NF_LOGSDIR/.nextflow
60+
fi
61+
62+
# .nextflow.log file has more detailed logging from the workflow run and is
63+
# nominally unique per run.
64+
#
65+
# when run locally, .nextflow.logs are automatically rotated
66+
# when syncing to S3 uniquely identify logs by the batch GUID
67+
if [ -f .nextflow.log ]; then
68+
echo "== Preserving Session Log =="
69+
aws s3 cp --only-show-errors .nextflow.log $NF_LOGSDIR/.nextflow.log.${GUID/\//.}
70+
fi
71+
}
72+
73+
trap preserve_session EXIT
74+
5375
# stage workflow definition
5476
if [[ "$NEXTFLOW_PROJECT" =~ ^s3://.* ]]; then
77+
echo "== Staging S3 Project =="
5578
aws s3 sync --only-show-errors --exclude 'runs/*' --exclude '.*' $NEXTFLOW_PROJECT ./project
5679
NEXTFLOW_PROJECT=./project
5780
fi
5881

5982
echo "== Running Workflow =="
6083
echo "nextflow run $NEXTFLOW_PROJECT $NEXTFLOW_PARAMS"
61-
nextflow run $NEXTFLOW_PROJECT $NEXTFLOW_PARAMS
62-
63-
# stage out session cache
64-
aws s3 sync --only-show-errors .nextflow $NF_LOGSDIR/.nextflow
65-
66-
# .nextflow.log file has more detailed logging from the workflow run and is
67-
# nominally unique per run.
68-
#
69-
# when run locally, .nextflow.logs are automatically rotated
70-
# when syncing to S3 uniquely identify logs by the batch GUID
71-
aws s3 cp --only-show-errors .nextflow.log $NF_LOGSDIR/.nextflow.log.${GUID/\//.}
84+
nextflow run $NEXTFLOW_PROJECT $NEXTFLOW_PARAMS

0 commit comments

Comments
 (0)