
                             d Z ddlZddlmZ ddlmZ ddlmZ ddlZddlm	Z	 ddlm
Z ddlmZ dd	lmZ ddlm
Z dd
l
mZ ddl
mZ  G d dej$                        Zy)zThe BigQuery CLI wait command.    N)Optional)app)flags)
client_job)utils)bigquery_command)bq_cached_client)bq_error)bq_id_utilsc                   l     e Zd ZdZdedej                  f fdZdej                  fde
e   fdZ xZS )Waitzwait [<job_id>] [<secs>]namefvc                     t         t        |   ||       t        j                  ddd|       t        j
                  ddd|       | j                  |       y )Nfail_on_errorTzsWhen done waiting for the job, exit the process with an error if the job is still running, or ended with a failure.)flag_valueswait_for_statusDONEz;Wait for the job to have a certain status. Default is DONE.)superr   __init__r   DEFINE_booleanDEFINE_string_ProcessCommandRc)selfr   r   	__class__s      $platform/bq/frontend/command_wait.pyr   zWait.__init__   s]    	$tR(		@ 
E	 	2     returnc           	      X   	 t        j                  |      }t
        j                  j                         }|sZt        j                  |ddg      }t        |      dk7  r"t        j                  dt        |      fz        |j                         }n0t        j                  ||t        j                   j"                        }	 t        j$                  |||| j&                        }t)        j*                  |t-        j.                  j0                  j2                  di |d	   d
       | j4                  rt        j6                  |      rdS dS # t        $ r t        j                  d|      w xY w# t8        $ r}t;                t;        |       Y d}~nd}~ww xY w| j4                  rdS dS )aF  Wait some number of seconds for a job to finish.

    Poll job_id until either (1) the job is DONE or (2) the
    specified number of seconds have elapsed. Waits forever
    if unspecified. If no job_id is specified, and there is
    only one running job, we poll that job.

    Examples:
      bq wait # Waits forever for the currently running job.
      bq wait job_id  # Waits forever
      bq wait job_id 100  # Waits 100 seconds
      bq wait job_id 0  # Polls if a job is done, then returns immediately.
      # These may exit with a non-zero status code to indicate "failure":
      bq wait --fail_on_error job_id  # Succeeds if job succeeds.
      bq wait --fail_on_error job_id 100  # Succeeds if job succeeds in 100 sec.

    Arguments:
      job_id: Job ID to wait on.
      secs: Number of seconds to wait (must be >= 0).
    zInvalid wait time: PENDINGRUNNING)bqclientstate_filter   z)No job_id provided, found %d running jobs)id_fallbacks
identifierdefault_location)r#   job_referencewaitstatusjobReferenceshow)custom_formatr   N )bq_client_utilsNormalizeWait
ValueErrorr   
UsageErrorr	   ClientGetr   ListJobRefslenr
   BigqueryErrorpopGetJobReferencebq_flagsLOCATIONvalueWaitJobr   frontend_utilsPrintObjectInfor   ApiClientHelperJobReferenceCreater   IsFailedJobStopIterationprint)r   job_idsecsclientrunning_jobsr)   jobes           r   RunWithArgszWait.RunWithArgs,   s   ,>**40d $$((*F++I(>l 
\	a	$$73|;L:NN
 	
 #&&(m%55#,,22m
%%%	c $$


%
%
2
2
9
9 N#  $$)D)DS)IQPqPC  >NNd<==>D  gAhh ""1))s*   E BE1 
E1 "E.1	F:FF)__name__
__module____qualname__usagestrr   
FlagValuesr   sysmaxsizer   intrM   __classcell__)r   s   @r   r   r      s<    
(%3 E$4$4 "  " >* >*r   r   )__doc__rT   typingr   abslr   r   r;   clientsr   r   r0   frontendr   r	   r?   r
   r   BigqueryCmdr   r/   r   r   <module>r^      sA    $ 
      , % % ,  R*'' R*r   