o
    ëÉ¶d½  ã                   @   sb   d dl Z ddlmZmZmZ ddlmZ ddlmZ d dl	m
Z
 dZddd„Ze
je
je
jgZdS )é    Né   )ÚMaximumWaitTimeExceededÚWaitUntilNotSupportedÚServiceError)ÚWAIT_RESOURCE_NOT_FOUND)Úretry)ÚWorkRequesté   é   é°  Fc              
      sÞ  |  d¡r|rtdƒ‚|  d¡s|stdƒ‚|  d¡du r4|jj ¡ dkr)tdƒ‚d‡ fdd	„	}||d< |r@t|j|ƒs@td
ƒ‚d}	t	 	¡ }
d}d}	 |rht
|tƒr]t|j|ƒ|v r\|S nt|j|ƒ|krg|S n	|  d¡|ƒrq|S |  d¡r|dkr|d ||ƒ t	 	¡ |
 }||	 |kr•||kr•tdƒ‚t	 |	¡ t|	d |ƒ}	z|  d¡|d}d}|d7 }W n< tyí } z0|jdkrÉ|sÁ‚ tW  Y d}~S |jdkrâ|tk râˆ j ¡ râ|d7 }ˆ jj ¡  n‚ W Y d}~nd}~ww qK)a,  Wait until the value of the given property in the response data has the given value.

    This will block the current thread until either the
    the desired state or the maximum wait time is reached. This is only
    supported for responses resulting from GET operations. A typical use
    case is to wait on an instance until it is in a running state.

    Although this can be run on any property of the data resulting from any
    GET operation, the most common use case is to check state properties on
    operations that GET a single object.

    The wait will poll at an increasing interval up to 'max_interval_seconds'
    for a maximum total time of 'max_wait_seconds'. If the maximum time
    is exceeded, then it will raise a MaximumWaitTimeExceeded error.

    On successful completion the final Response object will be returned. The
    original Response object will not be altered.

    If any responses result in an error, then the error will be thrown as normal
    resulting in the wait being aborted.

    :param client: A client we can use to call the service to periodically retrieve data.
    :param response: A Response object resulting from a GET operation.
    :param property: A string with the name of the property from the response data to evaluate.
        For example, 'state'.
    :param state: The value of the property that will indicate successful completion of the wait.
        Type corresponds to the property type.
    :param max_interval_seconds: (optional) The maximum interval between queries, in seconds.
        Defaults to 30 seconds.
    :param max_wait_seconds: (optional) The maximum time to wait, in seconds.
        Defaults to 1200 seconds.
    :param succeed_on_not_found: (optional) A boolean determining whether or not the waiter should return successfully
        if the data we're waiting on is not found (e.g. a 404 is returned from the service). This defaults to
        False and so a 404 would cause an exception to be thrown by this function. Setting it to True may be useful
        in scenarios when waiting for a resource to be terminated/deleted since it is possible that the resource would
        not be returned by the a GET call anymore.
    :param evaluate_response: (optional) A function which can be used to evaluate the response from the GET operation.
        This is a single argument function which takes in the response from the GET operation. If this function is
        supplied, then the 'property' argument cannot be supplied. It is expected that this function return a truthy
        value to signify that a condition has passed and the wait_until function should return, and a falsey value otherwise.
    :param wait_callback: (optional) A function which will be called each time that we have to do an initial wait (i.e.
        because the property of the resource was not in the correct state, or the ``evaluate_response`` function returned
         False). This function should take two arguments - the first argument is the number of times we have checked the
         resource, and the second argument is the result of the most recent check.
    :param fetch_func: (optional) This function will be called to fetch the updated state from the server.
        This can be used if the call to check for state needs to be more complex than a single GET request.
        For example, if the goal is to wait until an item appears in a list, fetch_func can be a function
        that paginates through a full list on the server.

    :return: The final response, which will contain the property in the specified state.

        If the ``succeed_on_not_found`` parameter is set to True and the data was not then ``oci.waiter.
        WAIT_RESOURCE_NOT_FOUND`` will be returned. This is a :py:class:`~oci.util.Sentinel` which is not truthy and
        holds an internal name of ``WaitResourceNotFound``.
    Zevaluate_responsez†Invalid wait_until configuration - can not provide both evaluate_response function and property argument, only one should be specifiedzvInvalid wait_until configuration - neither a property argument, nor an evaluate_response function, have been specifiedZ
fetch_funcNÚgetz0wait_until is only supported for get operations.c                    s   t j ˆ jj| j¡S ©N)r   ZDEFAULT_RETRY_STRATEGYZmake_retrying_callÚbase_clientÚrequest©Úresponse©Úclient© ú.usr/lib/python3.10/site-packages/oci/waiter.pyÚdefault_fetch_funcS   s   z&wait_until.<locals>.default_fetch_funcz2Response data does not contain the given property.r   r   TZwait_callbackz$Maximum wait time has been exceeded.r	   r   i”  i‘  r   )r   Ú
ValueErrorÚRuntimeErrorr   ÚmethodÚlowerr   ÚhasattrÚdataÚtimeÚ
isinstanceÚtupleÚgetattrr   ÚsleepÚminr   Ústatusr   ÚMAX_RETRIES_ON_401r   Z2is_instance_principal_or_resource_principal_signerZsignerZrefresh_security_token)r   r   ÚpropertyÚstateZmax_interval_secondsZmax_wait_secondsZsucceed_on_not_foundÚkwargsr   Zsleep_interval_secondsZ
start_timeZtimes_checkedZretry_count_401Zelapsed_secondsÚser   r   r   Ú
wait_until   sj   9
ÿÿ

ÿþ€ôär)   )NNr
   r   F)r   Ú
exceptionsr   r   r   Úutilr   Ú r   Zoci.work_requests.modelsr   r$   r)   ZSTATUS_SUCCEEDEDZSTATUS_FAILEDZSTATUS_CANCELEDZ _WORK_REQUEST_TERMINATION_STATESr   r   r   r   Ú<module>   s   
 ý