def stop_policy():
    global policy_thread
    if policy_thread:
        pk_config.finish_scaling(True)
        policy_thread.join()
        policy_thread = None
    return jsonify(dict(response='OK'))
def start(policy_yaml):
  global log
  log = logging.getLogger('pk')
  policy = prepare_session(policy_yaml)
  while not pk_config.finish_scaling():
    try:
      perform_one_session(policy)
    except Exception as e:
      log.exception('Exception occured during policy execution:')
    for x in range(15):
      if pk_config.finish_scaling():
        break
      time.sleep(1)
def start_policy():
    global policy_thread
    policy_yaml = request.stream.read()
    if not policy_yaml:
        if pk_config.policy():
            policy_yaml = pk_config.policy()
        else:
            raise RequestException(400, 'Empty POST data for /policy/start')
    if policy_thread:
        raise RequestException(400, 'Policy processing is already running')
    else:
        log.info('Received policy: {0}'.format(policy_yaml))
        pk_config.finish_scaling(False)
        policy_thread = threading.Thread(
            target=policy_keeper.perform_policy_keeping, args=(policy_yaml, ))
        policy_thread.start()
    return jsonify(dict(response='OK'))