# Change queue limit by reload and check number of running tasks to confirm
# that the new limit is being applied. This test is potentially flaky because
# it checks task status using the "cylc workflow-state" DB command, and DB task
# states (like the datastore too) only get updated once per main loop.

[scheduling]
    [[ queues ]]
        [[[ q1 ]]]
            limit = 5 # marker
            members = reloader, FAM
    [[graph]]
        R1 = """reloader:start => FAM
                   reloader => monitor"""
[runtime]
    [[FAM]]
        script = sleep 10
    [[a,b,c,d,e,f,g,h,i,j,k,l]]
        inherit = FAM
    [[reloader]]
        script = """
# change the limit from 5 to 3:
perl -pi -e 's/(limit = )5( # marker)/\1 3 \2/' $CYLC_WORKFLOW_RUN_DIR/flow.cylc
# reload:
cylc reload "${CYLC_WORKFLOW_ID}"
cylc__job__poll_grep_workflow_log 'Reload completed'
"""
    [[monitor]]
        script = """
        cylc__job__wait_cylc_message_started
        while true; do
            RUNNING=$(cylc dump -l -t "${CYLC_WORKFLOW_ID}" | grep running | wc -l)
            # Should be max of: monitor plus 3 members of q1
            echo "RUNNING $RUNNING"
            if ((RUNNING > 4)); then
                break
            fi
            sleep 1
            SUCCEEDED=$(cylc workflow-state "${CYLC_WORKFLOW_ID}//*/*:succeeded" --max-polls=1 | wc -l)
            echo "SUCCEEDED $SUCCEEDED"
            if ((SUCCEEDED==13)); then
                break
            fi
            sleep 1
        done
        if ((RUNNING > 4)); then
            false
        else
            true
        fi
"""
