#NEWLINE# def main( ) : #NEWLINE# #INDENT# flock = FlockManager ( match_found , config [ 'BLEET_TIMEOUT' ] , config [ 'SERVICE_TIMEOUT' ] ) #NEWLINE# logger . info ( 'Shepherd starting.' ) #NEWLINE# while True : #NEWLINE# #INDENT# zmq . select ( [ public , sheep ] , [ ] , [ ] , timeout = 5 ) #NEWLINE# while ( ( public . getsockopt ( zmq . EVENTS ) & zmq . POLLIN ) != 0 ) : #NEWLINE# #INDENT# request = public . recv_json ( ) #NEWLINE# logger . debug ( 'Raw test request: %s' , str ( request ) ) #NEWLINE# request = TestRequest . from_dict ( request ) #NEWLINE# try : #NEWLINE# #INDENT# submission = Submission . objects . get ( id = ObjectId ( request . submission_id ) ) #NEWLINE# #UNINDENT# except Submission . DoesNotExist as e : #NEWLINE# #INDENT# logger . warning ( 'Received test request for non-existant submission [%s].' , str ( request . submission_id ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# except bson . errors . InvalidId as e : #NEWLINE# #INDENT# logger . warning ( 'Received malformed test request. %s' , str ( e ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# assignment = Assignment . objects . get ( id = submission . assignment ) #NEWLINE# #UNINDENT# except Assignment . DoesNotExist as e : #NEWLINE# #INDENT# logger . error ( 'Received test request for a submission [%s] referencing an invalid assignment [%s].' , str ( submission . id ) , str ( submission . assignment ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( not assignment . test_harness ) : #NEWLINE# #INDENT# logger . warning ( 'Received test request for a submission [%s] referencing an assignment [%s] that does not have a test harness associated with it.' , str ( submission . id ) , str ( submission . assignment ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# test_harness = TestHarness . objects . get ( id = assignment . test_harness ) #NEWLINE# #UNINDENT# except TestHarness . DoesNotExit as e : #NEWLINE# #INDENT# logger . error ( 'Received test request for a submission [%s] referencing an assignment [%s] that references a non-existant test harness [%s].' , str ( submission . id ) , str ( submission . assignment ) , str ( assignment . test_harness ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# processed_request = InternalTestRequest ( submission . id , test_harness . config . get ( 'galah/timeout' , config [ 'BLEET_TIMEOUT' ] . seconds ) , test_harness . config . get ( 'galah/environment' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) ) #NEWLINE# logger . info ( 'Received test request.' ) #NEWLINE# flock . received_request ( processed_request ) #NEWLINE# #UNINDENT# while ( ( sheep . getsockopt ( zmq . EVENTS ) & zmq . POLLIN ) != 0 ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ( sheep_identity , sheep_message ) = router_recv_json ( sheep ) #NEWLINE# sheep_message = FlockMessage . from_dict ( sheep_message ) #NEWLINE# logger . debug ( 'Received message from sheep: %s' , str ( sheep_message ) ) #NEWLINE# #UNINDENT# except ValueError as e : #NEWLINE# #INDENT# logger . error ( "Could not decode sheep's message: %s" , str ( e ) ) #NEWLINE# logger . debug ( "Exception thrown while decoding sheep's message..." , exc_info = sys . exc_info ( ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( sheep_message . type == 'distress' ) : #NEWLINE# #INDENT# logger . warn ( 'Received distress message. Sending bloot.' ) #NEWLINE# router_send_json ( sheep , sheep_identity , FlockMessage ( 'bloot' , '' ) . to_dict ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( sheep_message . type == 'bleet' ) : #NEWLINE# #INDENT# logger . debug ( 'Sheep [%s] bleeted. Sending bloot.' , repr ( sheep_identity ) ) #NEWLINE# result = flock . sheep_bleeted ( sheep_identity ) #NEWLINE# if ( result is FlockManager . IGNORE ) : #NEWLINE# #INDENT# logger . debug ( 'Ignoring bleet.' ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( not result ) : #NEWLINE# #INDENT# router_send_json ( sheep , sheep_identity , FlockMessage ( 'identify' , '' ) . to_dict ( ) ) #NEWLINE# logger . info ( 'Unrecognized sheep [%s] connected, identify sent.' , repr ( sheep_identity ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# router_send_json ( sheep , sheep_identity , FlockMessage ( 'bloot' , '' ) . to_dict ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( sheep_message . type == 'environment' ) : #NEWLINE# #INDENT# if ( not flock . manage_sheep ( sheep_identity , sheep_message . body ) ) : #NEWLINE# #INDENT# logger . warn ( 'Received environment from an already-recognized sheep.' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( sheep_message . type == 'result' ) : #NEWLINE# #INDENT# logger . info ( 'Received test result from sheep.' ) #NEWLINE# logger . debug ( 'Received test result from sheep: %s' , str ( sheep_message . body ) ) #NEWLINE# try : #NEWLINE# #INDENT# submission_id = ObjectId ( sheep_message . body [ 'id' ] ) #NEWLINE# submission = Submission . objects . get ( id = submission_id ) #NEWLINE# test_result = TestResult . from_dict ( result . body ) #NEWLINE# try : #NEWLINE# #INDENT# test_result . save ( ) #NEWLINE# #UNINDENT# except InvalidDocument : #NEWLINE# #INDENT# logger . warn ( 'Test result is too large for the database.' , exc_info = True ) #NEWLINE# test_result = TestResult ( failed = True ) #NEWLINE# test_result . save ( ) #NEWLINE# #UNINDENT# submission . test_results = test_result . id #NEWLINE# submission . save ( ) #NEWLINE# #UNINDENT# except ( InvalidId , Submission . DoesNotExist ) as e : #NEWLINE# #INDENT# logger . warn ( 'Could not retrieve submission [%s] for test result received from sheep [%s].' , str ( submission_id ) , repr ( sheep_identity ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# router_send_json ( sheep , sheep_identity , FlockMessage ( 'bloot' , sheep_message . body [ 'id' ] ) . to_dict ( ) ) #NEWLINE# if ( not flock . sheep_finished ( sheep_identity ) ) : #NEWLINE# #INDENT# logger . info ( 'Got result from sheep [%s] who was not processing a test request.' , repr ( sheep_identity ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# ( lost_sheep , killed_sheep ) = flock . cleanup ( ) #NEWLINE# if lost_sheep : #NEWLINE# #INDENT# logger . warn ( '%d sheep lost due to bleet timeout: %s' , len ( lost_sheep ) , str ( [ repr ( i ) for i in lost_sheep ] ) ) #NEWLINE# #UNINDENT# if killed_sheep : #NEWLINE# #INDENT# logger . warn ( '%d sheep lost due to request timeout: %s' , len ( killed_sheep ) , str ( [ repr ( i ) for i in killed_sheep ] ) )