#!jinja2
# simple generic handler in the suite bin dir:
{% set HANDLER = "handler.sh" %}
{% set EVNTLOG = "$CYLC_SUITE_LOG_DIR/events.log" %}

[meta]
    title = "test all event handlers"

[cylc]
    [[environment]]
        EVNTLOG = {{ EVNTLOG }}
    [[events]]
        abort on stalled = True
        abort on inactivity = True
        inactivity = PT3M
    [[reference test]]
        expected task failures = bar.1, baz.1

[scheduling]
    [[graph]]
        R1 = """
            prep => foo & bar & baz
            bar:submit-fail & baz:fail & foo => done
            done => !bar & !baz
        """
[runtime]
    [[root]]
        # make the event handler log available to gcylc:
        extra log files = {{ EVNTLOG }}
        script = "true" # fast

    [[prep]]
        script = printf "%-20s %-8s %s\n" EVENT TASK MESSAGE > {{ EVNTLOG }}
    [[foo]]
        # timeout, retry, warning, succeeded
        script = """
test "${CYLC_TASK_TRY_NUMBER}" -gt 1
while ! grep -q 'execution timeout *foo\.1' "${CYLC_SUITE_LOG_DIR}/events.log"
do
    sleep 1
done
cylc task message -p WARNING 'this is a user-defined warning message'
"""
        [[[job]]]
            execution retry delays = PT3S
        [[[events]]]
            succeeded handler = {{ HANDLER }}
            warning handler = {{ HANDLER }}
            critical handler = {{ HANDLER }}
            retry handler = {{ HANDLER }}
            execution timeout = PT3S
            execution timeout handler = {{ HANDLER }}

    [[bar]]
        # submission retry and submission failed
        [[[events]]]
            submission failed handler = {{ HANDLER }}
            submission retry handler = {{ HANDLER }}
        [[[job]]]
            submission retry delays = PT3S
        [[[remote]]]
            host = NOHOST.NODOMAIN

     [[baz]]
        # submitted, submission timeout, started, failed
        init-script = """
while ! grep -q 'submission timeout *baz\.1' "${CYLC_SUITE_LOG_DIR}/events.log"
do
    sleep 1
done
"""
        script = false
        [[[events]]]
            submitted handler = {{ HANDLER }}
            started handler = {{ HANDLER }}
            failed handler = {{ HANDLER }}
            submission timeout = PT3S
            submission timeout handler = {{ HANDLER }}
