def main(ctx, chains, data_path): gevent.get_hub().exception_stream = DummyStream() chain_rpc_urls = parse_chain_rpc_urls(chains) if ctx.invoked_subcommand: ctx.obj = dict(chain_rpc_urls=chain_rpc_urls, data_path=Path(data_path))
def main(): # Disable printing stack traces by the hub, which breaks the TUI and # duplicates the traces. # # The code must be written with proper error handling, which requires # linking the result of every greenlet to its parent. Linking results in # error propagation, the behavior is that if a child greenlet dies because # of an exception, the exception is re-raised in the parent. If the # exception ends up reaching top-level (killing main) the full stacktrace # will be printed. gevent.get_hub().exception_stream = DummyStream()
def main( scenario_file, keystore_file, password, chains, data_path, auth, mailgun_api_key, ): gevent.get_hub().exception_stream = DummyStream() scenario_basename = basename(scenario_file.name) log_file_name = f'scenario-player_{scenario_basename}_{datetime.now():%Y-%m-%dT%H:%M:%S}.log' click.secho(f'Writing log to {log_file_name}', fg='yellow') configure_logging( {'': 'INFO', 'raiden': 'DEBUG', 'scenario_player': 'DEBUG'}, debug_log_file_name=log_file_name, _first_party_packages=frozenset(['raiden', 'scenario_player']), ) log_buffer = None if sys.stdout.isatty(): log_buffer = UrwidLogWalker([]) for handler in logging.getLogger('').handlers: if isinstance(handler, logging.StreamHandler): handler.terminator = None handler.formatter = NonStringifyingProcessorFormatter( UrwidLogRenderer(), foreign_pre_chain=LOGGING_PROCESSORS, ) handler.stream = log_buffer break with open(keystore_file, 'r') as keystore: account = Account(json.load(keystore), password, keystore_file) log.info("Using account", account=to_checksum_address(account.address)) # Collect tasks collect_tasks(tasks) chain_rpc_urls = defaultdict(list) for chain_name, chain_rpc_url in chains: chain_rpc_urls[chain_name].append(chain_rpc_url) runner = ScenarioRunner(account, chain_rpc_urls, auth, Path(data_path), scenario_file) ui = ScenarioUI(runner, log_buffer, log_file_name) ui_greenlet = ui.run() success = False try: try: runner.run_scenario() success = True log.info('Run finished', result='success') send_notification_mail( runner.notification_email, f'Scenario successful {scenario_file.name}', 'Success', mailgun_api_key, ) except ScenarioAssertionError as ex: log.error('Run finished', result='assertion errors') send_notification_mail( runner.notification_email, f'Assertion mismatch in {scenario_file.name}', str(ex), mailgun_api_key, ) except ScenarioError as ex: log.error('Run finished', result='scenario error') send_notification_mail( runner.notification_email, f'Invalid scenario {scenario_file.name}', traceback.format_exc(), mailgun_api_key, ) except Exception: log.exception('Exception while running scenario') send_notification_mail( runner.notification_email, f'Error running scenario {scenario_file.name}', traceback.format_exc(), mailgun_api_key, ) finally: try: if sys.stdout.isatty(): ui.set_success(success) log.warning('Press q to exit') while not ui_greenlet.dead: gevent.sleep(1) finally: if not ui_greenlet.dead: ui_greenlet.kill(ExitMainLoop) ui_greenlet.join()
def main(scenario_file, keystore_file, password, rpc_url, auth, mailgun_api_key): gevent.get_hub().exception_stream = DummyStream() scenario_basename = basename(scenario_file.name) log_file_name = f'scenario-player_{scenario_basename}_{datetime.now():%Y-%m-%dT%H:%M:%S}.log' click.secho(f'Writing log to {log_file_name}', fg='yellow') configure_logging( {'': 'INFO', 'raiden': 'DEBUG', 'scenario_player': 'DEBUG'}, debug_log_file_name=log_file_name, _first_party_packages=frozenset(['raiden', 'scenario_player']), ) log_buffer = LogBuffer() for handler in logging.getLogger('').handlers: if isinstance(handler, logging.StreamHandler): handler.stream = log_buffer break with open(keystore_file, 'r') as keystore: account = Account(json.load(keystore), password, keystore_file) log.info("Using account", account=to_checksum_address(account.address)) # Collect tasks collect_tasks(tasks) runner = ScenarioRunner(account, rpc_url, auth, scenario_file) terminal = Terminal() # Disable line wrapping print(terminal.rmam, end='') gevent.spawn(_ui, terminal, runner, log_file_name, log_buffer) try: assert_errors = runner.run_scenario() if assert_errors: log.error('Run finished', result='assertion errors') else: log.info('Run finished', result='success') if runner.notification_email: if not mailgun_api_key: log.error("Can't send notification mail. No API key provided") return 1 log.info('Sending notification mail') if assert_errors: send_notification_mail( runner.notification_email, f'Unexpected channel balances in {scenario_file.name}', json.dumps(assert_errors), mailgun_api_key, ) else: send_notification_mail( runner.notification_email, f'Scenario successful {scenario_file.name}', 'Success', mailgun_api_key, ) except Exception: if runner.notification_email and mailgun_api_key: send_notification_mail( runner.notification_email, f'Error running scenario {scenario_file.name}', traceback.format_exc(), mailgun_api_key, ) log.exception('Exception while running scenario') finally: try: if terminal.is_a_tty: log.warning('Press Ctrl-C to exit') while True: gevent.sleep(1) finally: # Re-enable line wrapping print(terminal.smam, end='')
def main(ctx): gevent.get_hub().exception_stream = DummyStream()