#NEWLINE# def connect_job( job_id , deployment_name , token_manager = None , app_url = defaults . APP_URL , persist = False , websocket = None , data_url = None ) : #NEWLINE# #INDENT# '\n    connect to a running Juttle program by job_id\n\n    ' #NEWLINE# if ( data_url == None ) : #NEWLINE# #INDENT# data_url = get_data_url_for_job ( job_id , deployment_name , token_manager = token_manager , app_url = app_url ) #NEWLINE# #UNINDENT# if ( token_manager == None ) : #NEWLINE# #INDENT# websocket = __wss_connect ( data_url , token_manager , job_id = job_id ) #NEWLINE# #UNINDENT# pong = json . dumps ( { #NEWLINE# #INDENT# 'pong' : True , #NEWLINE# #UNINDENT# } ) #NEWLINE# if ( not persist ) : #NEWLINE# #INDENT# job_finished = False #NEWLINE# while ( not job_finished ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# data = websocket . recv ( ) #NEWLINE# if data : #NEWLINE# #INDENT# payload = json . loads ( data ) #NEWLINE# if is_debug_enabled ( ) : #NEWLINE# #INDENT# printable_payload = dict ( payload ) #NEWLINE# if ( 'points' in payload ) : #NEWLINE# #INDENT# del printable_payload [ 'points' ] #NEWLINE# printable_payload [ 'points' ] = 'NOT SHOWN' #NEWLINE# #UNINDENT# debug ( ( 'received %s' % json . dumps ( printable_payload ) ) ) #NEWLINE# #UNINDENT# if ( 'ping' in payload . keys ( ) ) : #NEWLINE# #INDENT# websocket . send ( pong ) #NEWLINE# if is_debug_enabled ( ) : #NEWLINE# #INDENT# debug ( ( 'sent %s' % json . dumps ( pong ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( ( 'job_end' in payload . keys ( ) ) and ( payload [ 'job_end' ] == True ) ) : #NEWLINE# #INDENT# job_finished = True #NEWLINE# #UNINDENT# if token_manager . is_access_token_expired ( ) : #NEWLINE# #INDENT# debug ( 'refreshing access token' ) #NEWLINE# token_obj = { #NEWLINE# #INDENT# 'accessToken' : token_manager . get_access_token ( ) , #NEWLINE# #UNINDENT# } #NEWLINE# websocket . send ( json . dumps ( token_obj ) ) #NEWLINE# #UNINDENT# if ( 'error' in payload ) : #NEWLINE# #INDENT# if ( payload [ 'error' ] == 'NONEXISTENT-JOB' ) : #NEWLINE# #INDENT# raise JutException ( ( 'Job "%s" no longer running' % job_id ) ) #NEWLINE# #UNINDENT# #UNINDENT# ( yield payload ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# debug ( ( 'payload was "%s", forcing websocket reconnect' % data ) ) #NEWLINE# raise IOError ( ) #NEWLINE# #UNINDENT# #UNINDENT# except IOError : #NEWLINE# #INDENT# if is_debug_enabled ( ) : #NEWLINE# #INDENT# traceback . print_exc ( ) #NEWLINE# #UNINDENT# retry = 1 #NEWLINE# while ( retry <= 5 ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# debug ( ( 'network error reconnecting to job %s, try %s of 5' % ( job_id , retry ) ) ) #NEWLINE# websocket = __wss_connect ( data_url , token_manager , job_id = job_id ) #NEWLINE# break #NEWLINE# #UNINDENT# except socket . error : #NEWLINE# #INDENT# if is_debug_enabled ( ) : #NEWLINE# #INDENT# traceback . print_exc ( ) #NEWLINE# #UNINDENT# retry += 1 #NEWLINE# time . sleep ( 5 ) #NEWLINE# #UNINDENT# #UNINDENT# debug ( ( 'network error reconnecting to job %s, try %s of 5' % ( job_id , retry ) ) ) #NEWLINE# websocket = __wss_connect ( data_url , token_manager , job_id = job_id ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# websocket . close ( )