| 1 |
#!/bin/bash |
| 2 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 3 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 4 |
# |
| 5 |
# This program is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
#------------------------------------------------------------------------------- |
| 18 |
# Test that a user script can have its own TERM signal trap. |
| 19 |
|
| 20 |
. "$(dirname "$0")/test_header" |
| 21 |
set_test_number 5 |
| 22 |
|
| 23 |
init_workflow "${TEST_NAME_BASE}" <<'__WORKFLOW__' |
| 24 |
[cylc] |
| 25 |
[[events]] |
| 26 |
abort on inactivity timeout = True |
| 27 |
abort on stall timeout = True |
| 28 |
stall timeout = PT0S |
| 29 |
inactivity timeout = PT1M |
| 30 |
[scheduling] |
| 31 |
[[graph]] |
| 32 |
R1 = foo |
| 33 |
[runtime] |
| 34 |
[[foo]] |
| 35 |
script = """ |
| 36 |
# ignore TERM signal for the next child |
| 37 |
trap "" TERM |
| 38 |
( |
| 39 |
cylc__job__poll_grep_workflow_log "Workflow shutting down" |
| 40 |
sleep 5 |
| 41 |
echo "The undead child shall speak" >>"${CYLC_TASK_LOG_ROOT}.testout" |
| 42 |
) & |
| 43 |
trap "echo 'TERM got trapped' >>'${CYLC_TASK_LOG_ROOT}.testout'; wait" TERM |
| 44 |
# this child will be terminated if job script is a process leader |
| 45 |
( |
| 46 |
sleep 15 |
| 47 |
echo "You shall never see this" >>"${CYLC_TASK_LOG_ROOT}.testout" |
| 48 |
) & |
| 49 |
echo "Here we go..." >>"${CYLC_TASK_LOG_ROOT}.testout" |
| 50 |
wait |
| 51 |
""" |
| 52 |
err-script = """ |
| 53 |
wait |
| 54 |
echo "Exit with code ${CYLC_TASK_USER_SCRIPT_EXITCODE:-unknown}" \ |
| 55 |
>>"${CYLC_TASK_LOG_ROOT}.testout" |
| 56 |
""" |
| 57 |
__WORKFLOW__ |
| 58 |
|
| 59 |
|
| 60 |
run_ok "${TEST_NAME_BASE}-validate" cylc validate "${WORKFLOW_NAME}" |
| 61 |
# Needs to be detaching: |
| 62 |
workflow_run_ok "${TEST_NAME_BASE}-run" cylc play "${WORKFLOW_NAME}" |
| 63 |
|
| 64 |
JOB_LOG_ROOT="${WORKFLOW_RUN_DIR}/log/job/1/foo/01/job" |
| 65 |
|
| 66 |
# Kill the job and see what happened |
| 67 |
poll_grep "CYLC_JOB_PID=" "${JOB_LOG_ROOT}.status" |
| 68 |
JOB_PID=$(awk -F= '/CYLC_JOB_PID=/{print $2}' "${JOB_LOG_ROOT}.status") |
| 69 |
kill -s "TERM" "${JOB_PID}" |
| 70 |
poll_workflow_stopped |
| 71 |
|
| 72 |
# Workflow is down after failed message |
| 73 |
grep_ok "CYLC_JOB_EXIT=TERM" "${JOB_LOG_ROOT}.status" |
| 74 |
|
| 75 |
# The job should be still running and waiting for the user script |
| 76 |
grep_fail "The undead child shall speak" "${JOB_LOG_ROOT}.testout" |
| 77 |
|
| 78 |
poll_grep "Exit with code" "${JOB_LOG_ROOT}.testout" |
| 79 |
cmp_ok "${JOB_LOG_ROOT}.testout" - <<__EOF__ |
| 80 |
Here we go... |
| 81 |
TERM got trapped |
| 82 |
The undead child shall speak |
| 83 |
Exit with code 143 |
| 84 |
__EOF__ |
| 85 |
|
| 86 |
purge |
| 87 |
exit |