original_docstring: Simulates Marathon-style healthcheck on given service if healthcheck is enabled
  
      :param instance_config: service manifest
      :param docker_client: Docker client object
      :param container_id: Docker container id
      :param healthcheck_data: tuple url to healthcheck
      :param healthcheck_enabled: boolean
      :returns: healthcheck_passed: boolean
      
docstring: Simulates Marathon-style healthcheck on given service if healthcheck is enabled
docstring_tokens: Simulates Marathon - style healthcheck on given service if healthcheck is enabled
code: def simulate_healthcheck_on_service(
      instance_config,
      docker_client,
      container_id,
      healthcheck_mode,
      healthcheck_data,
      healthcheck_enabled,
  ):
      """Simulates Marathon-style healthcheck on given service if healthcheck is enabled
  
      :param instance_config: service manifest
      :param docker_client: Docker client object
      :param container_id: Docker container id
      :param healthcheck_data: tuple url to healthcheck
      :param healthcheck_enabled: boolean
      :returns: healthcheck_passed: boolean
      """
      healthcheck_link = PaastaColors.cyan(healthcheck_data)
      if healthcheck_enabled:
          grace_period = instance_config.get_healthcheck_grace_period_seconds()
          timeout = instance_config.get_healthcheck_timeout_seconds()
          interval = instance_config.get_healthcheck_interval_seconds()
          max_failures = instance_config.get_healthcheck_max_consecutive_failures()
  
          paasta_print('\nStarting health check via %s (waiting %s seconds before '
                       'considering failures due to grace period):' % (healthcheck_link, grace_period))
  
          # silenty start performing health checks until grace period ends or first check succeeds
          graceperiod_end_time = time.time() + grace_period
          after_grace_period_attempts = 0
          while True:
              # First inspect the container for early exits
              container_state = docker_client.inspect_container(container_id)
              if not container_state['State']['Running']:
                  paasta_print(
                      PaastaColors.red('Container exited with code {}'.format(
                          container_state['State']['ExitCode'],
                      )),
                  )
                  healthcheck_passed = False
                  break
  
              healthcheck_passed, healthcheck_output = run_healthcheck_on_container(
                  docker_client, container_id, healthcheck_mode, healthcheck_data, timeout,
              )
  
              # Yay, we passed the healthcheck
              if healthcheck_passed:
                  paasta_print("{}'{}' (via {})".format(
                      PaastaColors.green("Healthcheck succeeded!: "),
                      healthcheck_output,
                      healthcheck_link,
                  ))
                  break
  
              # Otherwise, print why we failed
              if time.time() < graceperiod_end_time:
                  color = PaastaColors.grey
                  msg = '(disregarded due to grace period)'
                  extra_msg = ' (via: {}. Output: {})'.format(healthcheck_link, healthcheck_output)
              else:
                  # If we've exceeded the grace period, we start incrementing attempts
                  after_grace_period_attempts += 1
                  color = PaastaColors.red
                  msg = '(Attempt {} of {})'.format(
                      after_grace_period_attempts, max_failures,
                  )
                  extra_msg = ' (via: {}. Output: {})'.format(healthcheck_link, healthcheck_output)
  
              paasta_print('{}{}'.format(
                  color('Healthcheck failed! {}'.format(msg)),
                  extra_msg,
              ))
  
              if after_grace_period_attempts == max_failures:
                  break
  
              time.sleep(interval)
      else:
          paasta_print('\nPaaSTA would have healthchecked your service via\n%s' % healthcheck_link)
          healthcheck_passed = True
      return healthcheck_passed
code_tokens: def simulate_healthcheck_on_service ( instance_config , docker_client , container_id , healthcheck_mode , healthcheck_data , healthcheck_enabled , ) : healthcheck_link = PaastaColors . cyan ( healthcheck_data ) if healthcheck_enabled : grace_period = instance_config . get_healthcheck_grace_period_seconds ( ) timeout = instance_config . get_healthcheck_timeout_seconds ( ) interval = instance_config . get_healthcheck_interval_seconds ( ) max_failures = instance_config . get_healthcheck_max_consecutive_failures ( ) paasta_print ( '\nStarting health check via %s (waiting %s seconds before ' 'considering failures due to grace period):' % ( healthcheck_link , grace_period ) ) graceperiod_end_time = time . time ( ) + grace_period after_grace_period_attempts = 0 while True : container_state = docker_client . inspect_container ( container_id ) if not container_state [ 'State' ] [ 'Running' ] : paasta_print ( PaastaColors . red ( 'Container exited with code {}' . format ( container_state [ 'State' ] [ 'ExitCode' ] , ) ) , ) healthcheck_passed = False break healthcheck_passed , healthcheck_output = run_healthcheck_on_container ( docker_client , container_id , healthcheck_mode , healthcheck_data , timeout , ) if healthcheck_passed : paasta_print ( "{}'{}' (via {})" . format ( PaastaColors . green ( "Healthcheck succeeded!: " ) , healthcheck_output , healthcheck_link , ) ) break if time . time ( ) < graceperiod_end_time : color = PaastaColors . grey msg = '(disregarded due to grace period)' extra_msg = ' (via: {}. Output: {})' . format ( healthcheck_link , healthcheck_output ) else : after_grace_period_attempts += 1 color = PaastaColors . red msg = '(Attempt {} of {})' . format ( after_grace_period_attempts , max_failures , ) extra_msg = ' (via: {}. Output: {})' . format ( healthcheck_link , healthcheck_output ) paasta_print ( '{}{}' . format ( color ( 'Healthcheck failed! {}' . format ( msg ) ) , extra_msg , ) ) if after_grace_period_attempts == max_failures : break time . sleep ( interval ) else : paasta_print ( '\nPaaSTA would have healthchecked your service via\n%s' % healthcheck_link ) healthcheck_passed = True return healthcheck_passed
short_docstring: Simulates Marathon-style healthcheck on given service if healthcheck is enabled
short_docstring_tokens: Simulates Marathon - style healthcheck on given service if healthcheck is enabled
comment: """Simulates Marathon-style healthcheck on given service if healthcheck is enabled
  
      :param instance_config: service manifest
      :param docker_client: Docker client object
      :param container_id: Docker container id
      :param healthcheck_data: tuple url to healthcheck
      :param healthcheck_enabled: boolean
      :returns: healthcheck_passed: boolean
      """$SEP$# silenty start performing health checks until grace period ends or first check succeeds$SEP$# First inspect the container for early exits$SEP$# Yay, we passed the healthcheck$SEP$# Otherwise, print why we failed$SEP$# If we've exceeded the grace period, we start incrementing attempts