#NEWLINE# def _run( ) : #NEWLINE# #INDENT# logger = logging . getLogger ( ( 'galah.sheep.%s' % threading . currentThread ( ) . name ) ) #NEWLINE# logger . info ( 'Consumer starting.' ) #NEWLINE# virtual_suite = get_virtual_suite ( config [ 'VIRTUAL_SUITE' ] ) #NEWLINE# consumer = virtual_suite . Consumer ( logger ) #NEWLINE# shepherd = universal . context . socket ( zmq . DEALER ) #NEWLINE# shepherd . linger = 0 #NEWLINE# shepherd . connect ( config [ 'shepherd/SHEEP_SOCKET' ] ) #NEWLINE# while ( not universal . exiting ) : #NEWLINE# #INDENT# logger . info ( 'Waiting for virtual machine to become available...' ) #NEWLINE# machine_id = consumer . prepare_machine ( ) #NEWLINE# def bleet( ) : #NEWLINE# #INDENT# shepherd . send_json ( FlockMessage ( 'bleet' , '' ) . to_dict ( ) ) #NEWLINE# return ( datetime . datetime . now ( ) + ( config [ 'shepherd/BLEET_TIMEOUT' ] / 2 ) ) #NEWLINE# #UNINDENT# logger . info ( 'Ready for test request. Sending initial bleet.' ) #NEWLINE# next_bleet_time = bleet ( ) #NEWLINE# shepherd_blooted = False #NEWLINE# while True : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# message = exithelpers . recv_json ( shepherd , timeout = max ( 1 , ( ( next_bleet_time - datetime . datetime . now ( ) ) . seconds * 1000 ) ) ) #NEWLINE# message = FlockMessage ( identification [ 'type' ] , message [ 'body' ] ) #NEWLINE# #UNINDENT# except exithelpers . Timeout : #NEWLINE# #INDENT# if ( not shepherd_blooted ) : #NEWLINE# #INDENT# raise universal . ShepherdLost ( ) #NEWLINE# #UNINDENT# logger . debug ( 'Sending bleet.' ) #NEWLINE# next_bleet_time = bleet ( ) #NEWLINE# shepherd_blooted = False #NEWLINE# continue #NEWLINE# #UNINDENT# if ( message . type == 'bloot' ) : #NEWLINE# #INDENT# logger . debug ( 'Got bloot.' ) #NEWLINE# shepherd_blooted = True #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( message . type == 'identify' ) : #NEWLINE# #INDENT# logger . info ( 'Received request to identify. Sending environment.' ) #NEWLINE# shepherd_blooted = True #NEWLINE# identification = FlockMessage ( type = 'environment' , body = universal . environment ) #NEWLINE# shepherd . send_json ( identification . to_dict ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( message . type == 'request' ) : #NEWLINE# #INDENT# logger . info ( 'Test request received, running tests.' ) #NEWLINE# logger . debug ( 'Test request: %s' , str ( message ) ) #NEWLINE# result = consumer . run_test ( machine_id , message . body ) #NEWLINE# if ( result is None ) : #NEWLINE# #INDENT# result = { #NEWLINE# #INDENT# 'failed' : True , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# result [ 'id' ] = str ( message . body [ 'submission' ] [ 'id' ] ) #NEWLINE# logger . info ( 'Testing completed, sending results to shepherd.' ) #NEWLINE# logger . debug ( 'Raw test results: %s' , str ( result ) ) #NEWLINE# shepherd . send_json ( FlockMessage ( 'result' , result ) . to_dict ( ) ) #NEWLINE# deadline = ( datetime . datetime . now ( ) + datetime . timedelta ( seconds = 30 ) ) #NEWLINE# while True : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# confirmation = exithelpers . recv_json ( shepherd , timeout = max ( 1 , ( ( deadline - datetime . datetime . now ( ) ) . seconds * 1000 ) ) ) #NEWLINE# confirmation = FlockMessage ( confirmation [ 'type' ] , confirmation [ 'body' ] ) #NEWLINE# #UNINDENT# except exithelpers . Timeout : #NEWLINE# #INDENT# raise universal . ShepherdLost ( result = result ) #NEWLINE# #UNINDENT# logger . debug ( 'Received message: %s' , str ( confirmation ) ) #NEWLINE# if ( ( confirmation . type == 'bloot' ) and ( confirmation . body == result [ 'id' ] ) ) : #NEWLINE# #INDENT# shepherd_blooted = True #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# break