
    C                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr	  SSK
Jr  SSK
Jr  S	rS
rSrS r   SS jr SS jr   SS jrg)z3A library used to interact with Operations objects.    )absolute_import)division)unicode_literals)
exceptions)progress_tracker)encoding)retryi` i  i  c                 v    SR                  U R                  [        R                  " U R                  5      5      $ )zReturns a human readable string representation from the operation.

Args:
  error: A string representing the raw json of the operation error.

Returns:
  A human readable string representation of the error.
z%OperationError: code={0}, message={1})formatcoder   Decodemessage)errors    5lib/googlecloudsdk/api_lib/functions/v1/operations.pyOperationErrorToStringr       s-     
1	7	7jj(//%--0
     Nc                 >   U(       a  U" 5         U(       a  UR                  5         U R                  R                  U5      nUR                  (       a)  [        R
                  " [        UR                  5      5      eU(       a  U H  nU" U5        M     UR                  $ )a  Helper function for getting the status of an operation.

Args:
  client: The client used to make requests.
  get_request: A GetOperationRequest message.
  progress_tracker: progress_tracker.ProgressTracker, A reference for the
    progress tracker to tick, in case this function is used in a Retryer.
  try_set_invoker: function to try setting invoker, see above TODO.
  on_every_poll: list of functions to execute every time we poll. Functions
    should take in Operation as an argument.

Returns:
  True if the operation succeeded without error.
  False if the operation is not yet done.

Raises:
  FunctionsError: If the operation is finished with error.
)Tick
operationsGetr   r   FunctionsErrorr   done)clientget_requestr   try_set_invokeron_every_pollopfunctions          r   _GetOperationStatusr   /   sq    2 [)"XX

#
#$:288$D
EE!rl "	.r   c           	      r   [         R                  " USS9 n[        R                  " S[        [
        S9n UR                  [        X/UUUS.S [        S9   S	S	S	5        g	! [        R                   a0    [        R                  " SR                  UR                  5      5      ef = f! , (       d  f       g	= f)
a  Wait for an operation to complete.

No operation is done instantly. Wait for it to finish following this logic:
* we wait 1s (jitter is also 1s)
* we query service
* if the operation is not finished we loop to first point
* wait limit is 1820s - if we get to that point it means something is wrong
      and we can throw an exception

Args:
  client:  The client used to make requests.
  get_request: A GetOperationRequest message.
  message: str, The string to print while polling.
  try_set_invoker: function to try setting invoker, see above TODO.
  on_every_poll: list of functions to execute every time we poll. Functions
    should take in Operation as an argument.

Returns:
  True if the operation succeeded without error.

Raises:
  FunctionsError: If the operation takes more than 1820s.
F)autotick   )exponential_sleep_multipliermax_wait_mswait_ceiling_ms)r   r   r   c                     U (       + $ )N )r   _s     r   <lambda>#_WaitForOperation.<locals>.<lambda>   s    d(r   )should_retry_ifsleep_msz Operation {0} is taking too longN)console_progress_trackerProgressTrackerr	   RetryerMAX_WAIT_MSWAIT_CEILING_MSRetryOnResultr   SLEEP_MSWaitExceptionr   r   r   name)r   r   r   r   r   ptretryers          r   _WaitForOperationr8   V   s    6  //%HBmm%&'G


"$!0,
 3  
 IH&  %%
,
3
3K4D4D
E ' IHs   B(!A!!AB%%B((
B6c                 j    Uc  SnUR                  5       nU R                  Ul        [        X&X4U5        g)a<  Initialize waiting for operation to finish.

Generate get request based on the operation and wait for an operation
to complete.

Args:
  operation: The operation which we are waiting for.
  messages: GCF messages module.
  client: GCF client module.
  notice: str, displayed when waiting for the operation to finish.
  try_set_invoker: function to try setting invoker, see above TODO.
  on_every_poll: list of functions to execute every time we poll. Functions
    should take in Operation as an argument.

Raises:
  FunctionsError: If the operation takes more than 620s.
NzWaiting for operation to finish)"CloudfunctionsOperationsGetRequestr5   r8   )	operationmessagesr   noticer   r   requests          r   Waitr?      s3    2 ^.F779'',FVmLr   )NNN)NN)__doc__
__future__r   r   r   #googlecloudsdk.api_lib.functions.v1r   googlecloudsdk.core.consoler   r-   googlecloudsdk.core.utilr   r	   r0   r1   r3   r   r   r8   r?   r'   r   r   <module>rE      sd    : '  ' : T - *$ #P GK1p Mr   