#NEWLINE# def ServeHealthCheck( self , env , start_response , remote_addr ) : #NEWLINE# #INDENT# 'Serve health checks.\n\n    It handles three cases:\n    1. MVM agent is handling health check complexity:\n       (1) Return ok with a 200.\n    2. The request comes locally:\n       (1) The IsLastSuccessful parameter will be ignored.\n       (2) If there is no the previous remote check with IsLastHealthy, or that\n           check has passed longer than self.check_interval_sec, it will return\n           unhealthy.\n       (3) Otherwise, returns status based value of self.is_last_successful.\n    3. The request comes remotely:\n       (1) If there is no IsLastSuccessful parameter, just return status from\n           HealthHandler.\n       (2) Otherwise, it sets self.is_last_successful based on the value of\n           IsLastSuccessful parameter from the query string ("yes" for True,\n           otherwise False), and then returns status from HealthHandler.\n\n    Args:\n      env: The WSGI environment for the application.\n      start_response: The WSGI start_response for the application.\n      remote_addr: The address where the request comes from.\n\n    Returns:\n      The WSGI body text iterable.\n    ' #NEWLINE# if ( os . environ . get ( 'USE_MVM_AGENT' ) == 'true' ) : #NEWLINE# #INDENT# start_response ( '200 OK' , [ ] ) #NEWLINE# return [ 'ok' ] #NEWLINE# #UNINDENT# query_string = env . get ( 'QUERY_STRING' , '' ) #NEWLINE# parameters = urlparse . parse_qs ( query_string ) #NEWLINE# is_last_successful_list = parameters . get ( 'IsLastSuccessful' , None ) #NEWLINE# if self . _CheckIsLocalAddress ( remote_addr ) : #NEWLINE# #INDENT# remote_check_valid = True #NEWLINE# if ( not self . is_last_successful_time ) : #NEWLINE# #INDENT# remote_check_valid = False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# time_offset_seconds = ( time . time ( ) - self . is_last_successful_time ) #NEWLINE# if ( time_offset_seconds > ( self . check_interval_sec * HEALTH_CHECK_INTERVAL_OFFSET_RATIO ) ) : #NEWLINE# #INDENT# remote_check_valid = False #NEWLINE# #UNINDENT# #UNINDENT# if ( self . is_last_successful and remote_check_valid ) : #NEWLINE# #INDENT# start_response ( '200 OK' , [ ] ) #NEWLINE# return [ 'ok' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# start_response ( '500 Internal Server Error' , [ ] ) #NEWLINE# if ( not self . is_last_successful ) : #NEWLINE# #INDENT# logging . warning ( 'unhealthy because IsLastSuccessful is False' ) #NEWLINE# #UNINDENT# if ( not remote_check_valid ) : #NEWLINE# #INDENT# logging . warning ( 'unhealthy because remote health check is not valid.' ) #NEWLINE# #UNINDENT# return [ 'unhealthy' ] #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( is_last_successful_list is not None ) : #NEWLINE# #INDENT# if ( is_last_successful_list [ 0 ] . lower ( ) == 'yes' ) : #NEWLINE# #INDENT# self . is_last_successful = True #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( is_last_successful_list [ 0 ] . lower ( ) == 'no' ) : #NEWLINE# #INDENT# self . is_last_successful = False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . is_last_successful = False #NEWLINE# logging . warning ( 'Wrong value for parameter IsLastSuccessful: %s' , is_last_successful_list ) #NEWLINE# #UNINDENT# #UNINDENT# self . is_last_successful_time = time . time ( ) #NEWLINE# #UNINDENT# return internal_app ( env , remote_check_valid )