forked from molgenis/PGx-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.ftl
199 lines (179 loc) · 5.09 KB
/
header.ftl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
#SBATCH --job-name=<#if project??>${project}_</#if>${taskId}
#SBATCH --output=${taskId}.out
#SBATCH --error=${taskId}.err
#SBATCH --time=${walltime}
#SBATCH --cpus-per-task ${ppn}
#SBATCH --mem ${mem}
#SBATCH --open-mode=append
#SBATCH --export=NONE
#SBATCH --get-user-env=30L
set -e
set -u
ENVIRONMENT_DIR='.'
#
# Variables declared in MOLGENIS Compute headers/footers always start with a MC_ prefix.
#
declare MC_jobScript="${taskId}.sh"
declare MC_jobScriptSTDERR="${taskId}.err"
declare MC_jobScriptSTDOUT="${taskId}.out"
<#noparse>
declare MC_singleSeperatorLine=$(head -c 120 /dev/zero | tr '\0' '-')
declare MC_doubleSeperatorLine=$(head -c 120 /dev/zero | tr '\0' '=')
declare MC_tmpFolder='tmpFolder'
declare MC_tmpFile='tmpFile'
declare MC_tmpFolderCreated=0
#
##
### Header functions.
##
#
function errorExitAndCleanUp() {
local signal=${1}
local problematicLine=${2}
local exitStatus=${3:-$?}
local executionHost=${SLURMD_NODENAME:-$(hostname)}
local errorMessage="FATAL: Trapped ${signal} signal in ${MC_jobScript} running on ${executionHost}. Exit status code was ${exitStatus}."
if [ $signal == 'ERR' ]; then
local errorMessage="FATAL: Trapped ${signal} signal on line ${problematicLine} in ${MC_jobScript} running on ${executionHost}. Exit status code was ${exitStatus}."
fi
local errorMessage=${4:-"${errorMessage}"} # Optionally use custom error message as third argument.
local format='INFO: Last 50 lines or less of %s:\n'
echo "${errorMessage}"
echo "${MC_doubleSeperatorLine}" > ${MC_failedFile}
echo "${errorMessage}" >> ${MC_failedFile}
if [ -f "${MC_jobScriptSTDERR}" ]; then
echo "${MC_singleSeperatorLine}" >> ${MC_failedFile}
printf "${format}" "${MC_jobScriptSTDERR}" >> ${MC_failedFile}
echo "${MC_singleSeperatorLine}" >> ${MC_failedFile}
tail -50 "${MC_jobScriptSTDERR}" >> ${MC_failedFile}
fi
if [ -f "${MC_jobScriptSTDOUT}" ]; then
echo "${MC_singleSeperatorLine}" >> ${MC_failedFile}
printf "${format}" "${MC_jobScriptSTDOUT}" >> ${MC_failedFile}
echo "${MC_singleSeperatorLine}" >> ${MC_failedFile}
tail -50 "${MC_jobScriptSTDOUT}" >> ${MC_failedFile}
fi
echo "${MC_doubleSeperatorLine}" >> ${MC_failedFile}
}
#
# Create tmp dir per script/job.
# To be called with with either a file or folder as first and only argument.
# Defines two globally set variables:
# 1. MC_tmpFolder: a tmp dir for this job/script. When function is called multiple times MC_tmpFolder will always be the same.
# 2. MC_tmpFile: when the first argument was a folder, MC_tmpFile == MC_tmpFolder
# when the first argument was a file, MC_tmpFile will be a path to a tmp file inside MC_tmpFolder.
#
function makeTmpDir {
#
# Compile paths.
#
local originalPath=$1
local myMD5=$(md5sum ${MC_jobScript})
myMD5=${myMD5%% *} # remove everything after the first space character to keep only the MD5 checksum itself.
local tmpSubFolder="tmp_${MC_jobScript}_${myMD5}"
local dir
local base
if [[ -d "${originalPath}" ]]; then
dir="${originalPath}"
base=''
else
base=$(basename "${originalPath}")
dir=$(dirname "${originalPath}")
fi
MC_tmpFolder="${dir}/${tmpSubFolder}/"
MC_tmpFile="$MC_tmpFolder/${base}"
echo "DEBUG ${MC_jobScript}::makeTmpDir: dir='${dir}';base='${base}';MC_tmpFile='${MC_tmpFile}'"
#
# Cleanup the previously created tmpFolder first if this script was resubmitted.
#
if [[ ${MC_tmpFolderCreated} -eq 0 && -d ${MC_tmpFolder} ]]; then
rm -rf ${MC_tmpFolder}
fi
#
# (Re-)create tmpFolder.
#
mkdir -p ${MC_tmpFolder}
MC_tmpFolderCreated=1
}
trap 'errorExitAndCleanUp HUP NA $?' HUP
trap 'errorExitAndCleanUp INT NA $?' INT
trap 'errorExitAndCleanUp QUIT NA $?' QUIT
trap 'errorExitAndCleanUp TERM NA $?' TERM
trap 'errorExitAndCleanUp EXIT NA $?' EXIT
trap 'errorExitAndCleanUp ERR $LINENO $?' ERR
touch ${MC_jobScript}.started
# Source getFile, putFile, inputs, alloutputsexist
include () {
if [[ -f "$1" ]]; then
source "$1"
echo "sourced $1"
else
echo "File not found: $1"
fi
}
getFile()
{
ARGS=($@)
NUMBER="${#ARGS[@]}";
if [ "$NUMBER" -eq "1" ]
then
myFile=${ARGS[0]}
if test ! -e $myFile;
then
echo "WARNING in getFile/putFile: $myFile is missing" 1>&2
fi
else
echo "Example usage: getData \"\$TMPDIR/datadir/myfile.txt\""
fi
}
putFile()
{
`getFile $@`
}
inputs()
{
for name in $@
do
if test ! -e $name;
then
echo "$name is missing" 1>&2
exit 1;
fi
done
}
outputs()
{
for name in $@
do
if test -e $name;
then
echo "skipped"
echo "skipped" 1>&2
exit 0;
else
return 0;
fi
done
}
alloutputsexist()
{
all_exist=true
for name in $@
do
if test ! -e $name;
then
all_exist=false
fi
done
if $all_exist;
then
echo "skipped"
echo "skipped" 1>&2
sleep 30
exit 0;
else
return 0;
fi
}
</#noparse>