#!jinja2
# simple generic handler in the workflow bin dir:
{% set EVNTLOG = "$CYLC_WORKFLOW_LOG_DIR/events.log" %}
{% set HANDLER = "EVNTLOG={0} handler.sh".format(EVNTLOG) %}

[meta]
    title = "test all event handlers"

[scheduler]
    allow implicit tasks = True
    [[events]]
        abort on stall timeout = True
        stall timeout = PT0S
        abort on inactivity timeout = True
        inactivity timeout = PT3M
        expected task failures = 1/bar, 1/baz

[scheduling]
    [[graph]]
        R1 = """
            prep => foo & bar & baz?
            bar:submit-fail? & baz:fail? & foo => done
            done => !bar & !baz
        """
[runtime]
    [[root]]
        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 *1/foo' "${CYLC_WORKFLOW_LOG_DIR}/events.log"
            do
                sleep 1
            done
            cylc message -p WARNING 'this is a user-defined warning message'
        """
        [[[job]]]
            execution retry delays = PT3S
        [[[events]]]
            succeeded handlers = {{ HANDLER }}
            warning handlers = {{ HANDLER }}
            critical handlers = {{ HANDLER }}
            retry handlers = {{ HANDLER }}
            execution timeout = PT3S
            execution timeout handlers = {{ HANDLER }}

    [[bar]]
        # submission retry and submission failed
        platform = test platform
        [[[events]]]
            submission failed handlers = {{ HANDLER }}
            submission retry handlers = {{ HANDLER }}
        [[[job]]]
            submission retry delays = PT3S
     [[baz]]
        # submitted, submission timeout, started, failed
        # Delay in init-script to cause submission timeout.
        # (Note CYLC_WORKFLOW_LOG_DIR is not defined at this point!)
        init-script = """
            while ! grep -q 'submission timeout .*1/baz' "${CYLC_WORKFLOW_LOG_DIR}/events.log"
            do
                sleep 1
            done
        """
        script = false
        [[[events]]]
            submitted handlers = {{ HANDLER }}
            started handlers = {{ HANDLER }}
            failed handlers = {{ HANDLER }}
            submission timeout = PT3S
            submission timeout handlers = {{ HANDLER }}
