Exemplo n.º 1
0
def do_subscribe(opts):
    LOGGER.info('Starting subscriber...')
    try:
        dsn = 'dbname={} user={} password={} host={} port={}'.format(
            opts.db_name, opts.db_user, opts.db_password, opts.db_host,
            opts.db_port)

        database = Database(dsn)
        database.connect()
        subscriber = Subscriber(opts.connect)
        subscriber.add_handler(get_events_handler(database))
        known_blocks = database.fetch_last_known_blocks(KNOWN_COUNT)
        known_ids = [block['block_id'] for block in known_blocks]
        subscriber.start(known_ids=known_ids)

    except KeyboardInterrupt:
        sys.exit(0)

    except Exception as err:  # pylint: disable=broad-except
        LOGGER.exception(err)
        sys.exit(1)

    finally:
        try:
            database.disconnect()
            subscriber.stop()
        except UnboundLocalError:
            pass

    LOGGER.info('Subscriber shut down successfully')