Debian Codegraph @

cylc/flow/etc/job.sh

raw
1 #!/bin/bash
2 # ^ NOTE: this script is not invoked directly so the shell is decided by
3 # the calling script, this just helps shellcheck lint the file
4
5 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
6 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ###############################################################################
22 # Bash functions for a cylc job.
23 ###############################################################################
24
25 ###############################################################################
26 # The main function for a cylc job.
27 cylc__job__main() {
28 # Export CYLC_ workflow and task environment variables
29 cylc__job__inst__cylc_env
30 # Turn on xtrace in debug mode
31 if "${CYLC_DEBUG:-false}"; then
32 if [[ -n "${BASH:-}" ]]; then
33 PS4='+[\D{%Y%m%dT%H%M%S%z}]\u@\h '
34 exec 19>>"${CYLC_RUN_DIR}/${CYLC_WORKFLOW_ID}/log/job/${CYLC_TASK_JOB}/job.xtrace"
35 export BASH_XTRACEFD=19
36 >&2 echo "Sending DEBUG MODE xtrace to job.xtrace"
37 fi
38 set -x
39 fi
40 # Init-Script
41 cylc__job__run_inst_func 'init_script'
42 # Start error and vacation traps
43 typeset signal_name=
44 for signal_name in ${CYLC_FAIL_SIGNALS}; do
45 # shellcheck disable=SC2064
46 trap "cylc__job_err ${signal_name}" "${signal_name}"
47 done
48 for signal_name in ${CYLC_VACATION_SIGNALS:-}; do
49 # shellcheck disable=SC2064
50 trap "cylc__job_vacation ${signal_name}" "${signal_name}"
51 done
52 set -euo pipefail
53 # Write job self-identify
54 USER="${USER:-$(whoami)}"
55 typeset host="${HOSTNAME:-}"
56 if [[ -z "${host}" ]]; then
57 if [[ "$(uname)" == 'AIX' ]]; then
58 # On AIX the hostname command has no '-f' option
59 host="$(hostname).$(namerslv -sn 2>'/dev/null' | awk '{print $2}')"
60 else
61 host="$(hostname -f)"
62 fi
63 fi
64 # Developer Note:
65 # We were using a HERE document for writing info here until we notice that
66 # Bash uses temporary files for HERE documents, which can be inefficient.
67 echo "Workflow : ${CYLC_WORKFLOW_ID}"
68 echo "Job : ${CYLC_TASK_JOB} (try ${CYLC_TASK_TRY_NUMBER})"
69 echo "User@Host: ${USER}@${host}"
70 echo
71 # Derived environment variables
72 export CYLC_WORKFLOW_RUN_DIR="${CYLC_RUN_DIR}/${CYLC_WORKFLOW_ID}"
73 export CYLC_WORKFLOW_LOG_DIR="${CYLC_WORKFLOW_RUN_DIR}/log/scheduler"
74 export CYLC_WORKFLOW_SHARE_DIR="${CYLC_WORKFLOW_RUN_DIR}/share"
75 export CYLC_WORKFLOW_WORK_DIR="${CYLC_WORKFLOW_RUN_DIR}/work"
76 export CYLC_TASK_CYCLE_POINT="${CYLC_TASK_JOB%%/*}"
77 export CYLC_TASK_NAME="${CYLC_TASK_JOB#*/}"
78 CYLC_TASK_NAME="${CYLC_TASK_NAME%/*}"
79 if [[ "${CYLC_CYCLING_MODE}" != "integer" ]]; then # i.e. date-time cycling
80 export ISODATETIMECALENDAR="${CYLC_CYCLING_MODE}"
81 export ISODATETIMEREF="${CYLC_TASK_CYCLE_POINT}"
82 fi
83 # The "10#" part ensures that the submit number is interpreted in base 10.
84 # Otherwise, a zero padded number will be interpreted as an octal.
85 export CYLC_TASK_SUBMIT_NUMBER="$((10#${CYLC_TASK_JOB##*/}))"
86 export CYLC_TASK_ID="${CYLC_TASK_CYCLE_POINT}/${CYLC_TASK_NAME}"
87 export CYLC_TASK_LOG_DIR="${CYLC_WORKFLOW_RUN_DIR}/log/job/${CYLC_TASK_JOB}"
88 export CYLC_TASK_LOG_ROOT="${CYLC_TASK_LOG_DIR}/job"
89 if [[ -n "${CYLC_TASK_WORK_DIR_BASE:-}" ]]; then
90 # Note: value of CYLC_TASK_WORK_DIR_BASE may contain variable
91 # substitution syntax including some of the derived variables above, so
92 # it can only be derived at point of use.
93 CYLC_TASK_WORK_DIR_BASE="$(eval echo "${CYLC_TASK_WORK_DIR_BASE}")"
94 else
95 CYLC_TASK_WORK_DIR_BASE="${CYLC_TASK_CYCLE_POINT}/${CYLC_TASK_NAME}"
96 fi
97 export CYLC_TASK_WORK_DIR="${CYLC_WORKFLOW_WORK_DIR}/${CYLC_TASK_WORK_DIR_BASE}"
98 export CYLC_TASK_SHARE_CYCLE_DIR="${CYLC_WORKFLOW_SHARE_DIR}/cycle/${CYLC_TASK_CYCLE_POINT}"
99 typeset contact="${CYLC_WORKFLOW_RUN_DIR}/.service/contact"
100 if [[ -f "${contact}" ]]; then
101 # (contact file not present for polled platforms)
102 CYLC_WORKFLOW_HOST="$(sed -n 's/^CYLC_WORKFLOW_HOST=//p' "${contact}")"
103 CYLC_WORKFLOW_OWNER="$(sed -n 's/^CYLC_WORKFLOW_OWNER=//p' "${contact}")"
104 export CYLC_WORKFLOW_HOST CYLC_WORKFLOW_OWNER
105 # BACK COMPAT: DEPRECATED environment variables
106 # remove at:
107 # Cylc8.x
108 export CYLC_SUITE_HOST="${CYLC_WORKFLOW_HOST}"
109 export CYLC_SUITE_OWNER="${CYLC_WORKFLOW_OWNER}"
110 fi
111
112 # BACK COMPAT: DEPRECATED environment variables
113 # remove at:
114 # Cylc8.x
115 export CYLC_SUITE_SHARE_DIR="${CYLC_WORKFLOW_SHARE_DIR}"
116 export CYLC_SUITE_SHARE_PATH="${CYLC_WORKFLOW_SHARE_DIR}"
117 export CYLC_SUITE_NAME="${CYLC_WORKFLOW_ID}"
118 export CYLC_SUITE_LOG_DIR="${CYLC_WORKFLOW_LOG_DIR}"
119 export CYLC_SUITE_INITIAL_CYCLE_POINT="${CYLC_WORKFLOW_INITIAL_CYCLE_POINT}"
120 export CYLC_SUITE_INITIAL_CYCLE_TIME="${CYLC_WORKFLOW_INITIAL_CYCLE_POINT}"
121 export CYLC_SUITE_FINAL_CYCLE_POINT="${CYLC_WORKFLOW_FINAL_CYCLE_POINT}"
122 export CYLC_SUITE_FINAL_CYCLE_TIME="${CYLC_WORKFLOW_FINAL_CYCLE_POINT}"
123 export CYLC_SUITE_WORK_DIR="${CYLC_WORKFLOW_WORK_DIR}"
124 export CYLC_SUITE_UUID="${CYLC_WORKFLOW_UUID}"
125 export CYLC_SUITE_RUN_DIR="${CYLC_WORKFLOW_RUN_DIR}"
126 export CYLC_SUITE_DEF_PATH="${CYLC_WORKFLOW_RUN_DIR}"
127 export CYLC_TASK_CYCLE_TIME="${CYLC_TASK_CYCLE_POINT}"
128 export CYLC_TASK_WORK_PATH="${CYLC_TASK_WORK_DIR}"
129
130 # Send task started message
131 cylc message -- "${CYLC_WORKFLOW_ID}" "${CYLC_TASK_JOB}" 'started' &
132 CYLC_TASK_MESSAGE_STARTED_PID=$!
133 # System paths:
134 # * workflow directory (installed run-dir first).
135 export PATH="${CYLC_WORKFLOW_RUN_DIR}/share/bin:${CYLC_WORKFLOW_RUN_DIR}/bin:${PATH}"
136 export PYTHONPATH="${CYLC_WORKFLOW_RUN_DIR}/share/lib/python:${CYLC_WORKFLOW_RUN_DIR}/lib/python:${PYTHONPATH:-}"
137 # Create share and work directories
138 mkdir -p "${CYLC_TASK_SHARE_CYCLE_DIR}" || true
139 mkdir -p "$(dirname "${CYLC_TASK_WORK_DIR}")" || true
140 mkdir -p "${CYLC_TASK_WORK_DIR}"
141 cd "${CYLC_TASK_WORK_DIR}"
142 # Env-Script, User Environment, Pre-Script, Script and Post-Script
143 # Run user scripts in subshell to protect cylc job script from interference.
144 # Waiting on background process allows signal traps to trigger immediately.
145 cylc__job__run_user_scripts &
146 CYLC_TASK_USER_SCRIPT_PID=$!
147 wait "${CYLC_TASK_USER_SCRIPT_PID}" || {
148 # Check return code for signals (value greater than 128).
149 typeset ret_code="$?"
150 if ((ret_code > 128)); then
151 # Trigger the EXIT trap if the process exited due to a signal.
152 exit "$ret_code"
153 else
154 # Trigger ERR trap while preserving the exit code
155 # (NB: Bash versions are buggy and neither return statement nor
156 # subshelled exit won't do here.)
157 cylc__set_return "$ret_code"
158 fi
159 }
160 # Empty work directory remove
161 cd
162 rmdir "${CYLC_TASK_WORK_DIR}" 2>'/dev/null' || true
163 # Send task succeeded message
164 wait "${CYLC_TASK_MESSAGE_STARTED_PID}" 2>'/dev/null' || true
165 cylc message -- "${CYLC_WORKFLOW_ID}" "${CYLC_TASK_JOB}" 'succeeded' || true
166 # (Ignore shellcheck "globbing and word splitting" warning here).
167 # shellcheck disable=SC2086
168 trap '' ${CYLC_VACATION_SIGNALS:-} ${CYLC_FAIL_SIGNALS}
169 # Execute success exit script
170 cylc__job__run_inst_func 'exit_script'
171 exit 0
172 }
173
174 ###############################################################################
175 # Run user scripts.
176 cylc__job__run_user_scripts() {
177 typeset func_name=
178 for func_name in 'env_script' 'user_env' 'pre_script' \
179 'script' 'post_script'; do
180 cylc__job__run_inst_func "${func_name}"
181 done
182 }
183
184 ###############################################################################
185 # Set last return code (needed to work around Bash bugs in ERR trapping).
186 cylc__set_return() {
187 return "${1:-0}"
188 }
189
190 ###############################################################################
191 # Disable selected or all (if no arguments given) fail traps.
192 # Globals:
193 # CYLC_FAIL_SIGNALS
194 cylc__job__disable_fail_signals() {
195 if [[ "$#" == '0' ]]; then
196 CYLC_FAIL_SIGNALS=
197 else
198 typeset signal=
199 for signal in "$@"; do
200 CYLC_FAIL_SIGNALS="${CYLC_FAIL_SIGNALS/${signal}/}"
201 done
202 fi
203 }
204
205 ###############################################################################
206 # Wait for background `cylc message started` command to finish.
207 # Globals:
208 # CYLC_TASK_MESSAGE_STARTED_PID
209 # Returns:
210 # 0 (always success)
211 cylc__job__wait_cylc_message_started() {
212 if [[ -n "${CYLC_TASK_MESSAGE_STARTED_PID:-}" ]]; then
213 while kill -s 0 -- "${CYLC_TASK_MESSAGE_STARTED_PID}" 2>'/dev/null'; do
214 sleep 1
215 done
216 fi
217 }
218
219 ###############################################################################
220 # Poll existence of pattern from workflow log for up to a minute.
221 cylc__job__poll_grep_workflow_log() {
222 local TIMEOUT="$(($(date +%s) + 60))" # wait 1 minute
223 while ! grep -s "$@" "${CYLC_WORKFLOW_LOG_DIR}/log"; do
224 sleep 1
225 if (($(date +%s) > TIMEOUT)); then
226 echo "ERROR: poll timed out: grep -s $* ${CYLC_WORKFLOW_LOG_DIR}/log" >&2
227 exit 1
228 fi
229 done
230 }
231
232 ###############################################################################
233 # Run a function in the job instance file, if possible.
234 # Arguments:
235 # func_name: name of function without the "cylc__job__inst__" prefix
236 # Returns:
237 # return 0, or the return code of the function if called
238 cylc__job__run_inst_func() {
239 typeset func_name="$1"
240 shift 1
241 if typeset -f "cylc__job__inst__${func_name}" 1>'/dev/null' 2>&1; then
242 "cylc__job__inst__${func_name}" "$@"
243 fi
244 }
245
246 ###############################################################################
247 # Send message (and possibly run err script) before job exit.
248 # Globals:
249 # CYLC_FAIL_SIGNALS
250 # CYLC_TASK_LOG_ROOT
251 # CYLC_TASK_MESSAGE_STARTED_PID
252 # CYLC_TASK_USER_SCRIPT_PID
253 # CYLC_TASK_USER_SCRIPT_EXITCODE
254 # CYLC_VACATION_SIGNALS
255 # Arguments:
256 # signal: trapped or given signal
257 # run_err_script (boolean): run job err script or not
258 # messages (remaining arguments):
259 # messages to send back to the scheduler,
260 # see "cylc help message" for format of messages.
261 # Returns:
262 # exit ${CYLC_TASK_USER_SCRIPT_EXITCODE}
263 cylc__job_finish_err() {
264 CYLC_TASK_USER_SCRIPT_EXITCODE="${CYLC_TASK_USER_SCRIPT_EXITCODE:-$?}"
265 typeset signal="$1"
266 typeset run_err_script="$2"
267 shift 2
268 # (Ignore shellcheck "globbing and word splitting" warning here).
269 # shellcheck disable=SC2086
270 trap '' ${CYLC_VACATION_SIGNALS:-} ${CYLC_FAIL_SIGNALS}
271 if [[ -n "${CYLC_TASK_MESSAGE_STARTED_PID:-}" ]]; then
272 wait "${CYLC_TASK_MESSAGE_STARTED_PID}" 2>'/dev/null' || true
273 fi
274 # Propagate real signals to entire process group, if we are a group leader,
275 # otherwise just to the backgrounded user script.
276 if [[ -n "${CYLC_TASK_USER_SCRIPT_PID:-}" ]] &&
277 [[ "${signal}" != "ERR" ]] && [[ "${signal}" != "EXIT" ]]; then
278 kill -s "${signal}" -- "-$$" 2>'/dev/null' ||
279 kill -s "${signal}" -- "${CYLC_TASK_USER_SCRIPT_PID}" 2>'/dev/null' || true
280 fi
281 grep -q "^CYLC_JOB_EXIT=" "${CYLC_TASK_LOG_ROOT}.status" ||
282 cylc message -- "${CYLC_WORKFLOW_ID}" "${CYLC_TASK_JOB}" "$@" &
283 CYLC_TASK_MESSAGE_FINISHED_PID=$!
284 if "${run_err_script}"; then
285 cylc__job__run_inst_func 'err_script' "${signal}" >&2
286 fi
287 wait "${CYLC_TASK_MESSAGE_FINISHED_PID}"
288 exit "${CYLC_TASK_USER_SCRIPT_EXITCODE}"
289 }
290
291 ###############################################################################
292 # Wrap cylc__job_finish_err to abort with a user-defined error message.
293 # Globals:
294 # CYLC_TASK_USER_SCRIPT_EXITCODE
295 cylc__job_abort() {
296 CYLC_TASK_USER_SCRIPT_EXITCODE="1"
297 cylc__job_finish_err "EXIT" false "CRITICAL: aborted/\"${1}\""
298 }
299
300 ###############################################################################
301 # Wrap cylc__job_finish_err for job preempt/vacation signal trap.
302 cylc__job_vacation() {
303 cylc__job_finish_err "${1}" false "WARNING: vacated/${1}"
304 }
305
306 ###############################################################################
307 # Wrap cylc__job_finish_err for automatic job exit signal trap.
308 cylc__job_err() {
309 cylc__job_finish_err "${1}" true "CRITICAL: failed/${1}"
310 }
311
312 ###############################################################################
313 # Handle dummy job cycle point specific success or failure.
314 # Globals:
315 # CYLC_TASK_TRY_NUMBER
316 # CYLC_TASK_CYCLE_POINT
317 # CYLC_CYCLING_MODE
318 # Arguments:
319 # fail_try_1_only (boolean): fail only on 1st try
320 # fail_cycle_points (remaining arguments):
321 # 'all': fail all cycle points
322 # 'P1 P2 P3 ...': fail these cycle points
323 # Returns:
324 # 0 - dummy job succeed
325 # 1 - dummy job fail
326 cylc__job__dummy_result() {
327 typeset fail_try_1_only="$1"; shift
328 typeset fail_cycle_points="$*"
329 typeset fail_this_point=false
330 if [[ "${fail_cycle_points}" == *all* ]]; then
331 # Fail all points.
332 fail_this_point=true
333 else
334 # Fail some or no points.
335 if [[ "${CYLC_CYCLING_MODE}" == "integer" ]]; then
336 for POINT in ${fail_cycle_points}; do
337 if ((CYLC_TASK_CYCLE_POINT == POINT)); then
338 fail_this_point=true
339 break
340 fi
341 done
342 else
343 for POINT in ${fail_cycle_points}; do
344 if cylc cyclepoint --equal="$POINT"; then
345 fail_this_point=true
346 break
347 fi
348 done
349 fi
350 fi
351 if ! $fail_this_point || \
352 ($fail_try_1_only && ((CYLC_TASK_TRY_NUMBER > 1)) ); then
353 echo "(dummy job succeed)"
354 return 0
355 else
356 >&2 echo "(dummy job fail)"
357 return 1
358 fi
359 }