Esempio n. 1
0
def main():
  """Update the heartbeat if there is bot activity."""
  if len(sys.argv) < 2:
    print('Usage: %s <log file>' % sys.argv[0])
    return

  environment.set_bot_environment()
  logs.configure('run_heartbeat')

  log_filename = sys.argv[1]
  previous_state = None

  # Get absolute path to heartbeat script and interpreter needed to execute it.
  startup_scripts_directory = environment.get_startup_scripts_directory()
  beat_script_path = os.path.join(startup_scripts_directory, BEAT_SCRIPT)
  beat_interpreter = shell.get_interpreter(beat_script_path)
  assert beat_interpreter

  while True:
    beat_command = [
        beat_interpreter, beat_script_path,
        str(previous_state), log_filename
    ]

    try:
      previous_state = subprocess.check_output(
          beat_command, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
      logs.log_error('Failed to beat.', output=e.output)
    except Exception:
      logs.log_error('Failed to beat.')

    # See if our run timed out, if yes bail out.
    if data_handler.bot_run_timed_out():
      break
Esempio n. 2
0
def run_loop(bot_command, heartbeat_command):
  """Run infinite loop with bot's command."""
  atexit.register(stop_heartbeat)

  while True:
    update_source_code_if_needed()
    start_heartbeat(heartbeat_command)
    start_bot(bot_command)

    # See if our run timed out, if yes bail out.
    try:
      if data_handler.bot_run_timed_out():
        break
    except Exception:
      logs.log_error('Failed to check for bot run timeout.')

    sleep(LOOP_SLEEP_INTERVAL)
Esempio n. 3
0
def main():
  """Prepare the configuration options and start requesting tasks."""
  logs.configure('run_bot')

  root_directory = environment.get_value('ROOT_DIR')
  if not root_directory:
    print('Please set ROOT_DIR environment variable to the root of the source '
          'checkout before running. Exiting.')
    print('For an example, check init.bash in the local directory.')
    return

  dates.initialize_timezone_from_environment()
  environment.set_bot_environment()
  monitor.initialize()

  if not profiler.start_if_needed('python_profiler_bot'):
    sys.exit(-1)

  if environment.is_trusted_host(ensure_connected=False):
    from bot.untrusted_runner import host
    host.init()

  if environment.is_untrusted_worker():
    # Track revision since we won't go into the task_loop.
    update_task.track_revision()

    from bot.untrusted_runner import untrusted as untrusted_worker
    untrusted_worker.start_server()
    assert False, 'Unreachable code'

  while True:
    # task_loop should be an infinite loop,
    # unless we run into an exception.
    error_stacktrace, clean_exit, task_payload = task_loop()

    # Print the error trace to the console.
    if not clean_exit:
      print('Exception occurred while running "%s".' % task_payload)
      print('-' * 80)
      print(error_stacktrace)
      print('-' * 80)

    should_terminate = (
        clean_exit or errors.error_in_list(error_stacktrace,
                                           errors.BOT_ERROR_TERMINATION_LIST))
    if should_terminate:
      return

    logs.log_error(
        'Task exited with exception.',
        error_stacktrace=error_stacktrace,
        task_payload=task_payload)

    should_hang = errors.error_in_list(error_stacktrace,
                                       errors.BOT_ERROR_HANG_LIST)
    if should_hang:
      logs.log('Start hanging forever.')
      while True:
        # Sleep to avoid consuming 100% of CPU.
        time.sleep(60)

    # See if our run timed out, if yes bail out.
    if data_handler.bot_run_timed_out():
      return

    # Clear the current exception.
    sys.exc_clear()