Like query but, repeat and wait until match/match_type or status is fulfilled. State returns result from last
    query state in case of success or if no successful query was made within wait_for timeout.

    name
        The name of the query.

    wait_for
        Total time to wait for requests that succeed.

    request_interval
        Optional interval to delay requests by N seconds to reduce the number of requests sent.

    .. note::

        All other arguments are passed to the http.query state.

def wait_for_successful_query(name, wait_for=300, **kwargs):
    '''
    Like query but, repeat and wait until match/match_type or status is fulfilled. State returns result from last
    query state in case of success or if no successful query was made within wait_for timeout.

    name
        The name of the query.

    wait_for
        Total time to wait for requests that succeed.

    request_interval
        Optional interval to delay requests by N seconds to reduce the number of requests sent.

    .. note::

        All other arguments are passed to the http.query state.
    '''
    starttime = time.time()

    while True:
        caught_exception = None
        ret = None
        try:
            ret = query(name, **kwargs)
            if ret['result']:
                return ret
        except Exception as exc:
            caught_exception = exc

        if time.time() > starttime + wait_for:
            if not ret and caught_exception:
                # workaround pylint bug https://www.logilab.org/ticket/3207
                raise caught_exception  # pylint: disable=E0702
            return ret
        else:
            # Space requests out by delaying for an interval
            if 'request_interval' in kwargs:
                log.debug('delaying query for %s seconds.', kwargs['request_interval'])
                time.sleep(kwargs['request_interval'])