예제 #1
0
 def __init__(self, connData):
     super(RDBReader, self).__init__()
     rdb.set_loop_type('tornado')
     #self.conn = rdb.connect(host=connData["host"], port=connData["port"], db=connData["db"], auth_key=connData["auth_key"])
     self.conn = rdb.connect(host=connData["host"],
                             port=connData["port"],
                             db=connData["db"])
예제 #2
0
파일: checks.py 프로젝트: Phxntxm/Bonfire
async def db_check():
    """Used to check if the required database/tables are setup"""
    db_opts = config.db_opts

    r.set_loop_type('asyncio')
    # First try to connect, and see if the correct information was provided
    try:
        conn = await r.connect(**db_opts)
    except r.errors.ReqlDriverError:
        print("Cannot connect to the RethinkDB instance with the following information: {}".format(db_opts))

        print("The RethinkDB instance you have setup may be down, otherwise please ensure you setup a"
              " RethinkDB instance, and you have provided the correct database information in config.yml")
        quit()
        return

    # Get the current databases and check if the one we need is there
    dbs = await r.db_list().run(conn)
    if db_opts['db'] not in dbs:
        # If not, we want to create it
        print('Couldn\'t find database {}...creating now'.format(db_opts['db']))
        await r.db_create(db_opts['db']).run(conn)
        # Then add all the tables
        for table, key in required_tables.items():
            print("Creating table {}...".format(table))
            await r.table_create(table, primary_key=key).run(conn)
        print("Done!")
    else:
        # Otherwise, if the database is setup, make sure all the required tables are there
        tables = await r.table_list().run(conn)
        for table, key in required_tables.items():
            if table not in tables:
                print("Creating table {}...".format(table))
                await r.table_create(table, primary_key=key).run(conn)
        print("Done checking tables!")
	def __init__(self, ident, table):
		super(RDBReader, self).__init__()
		
		ioloop.IOLoop.current().add_callback(self.print_changes)
		rdb.set_loop_type('tornado')
		self.ident = ident
		self.table = table
예제 #4
0
def server():
    """
        Starts main dispatch server
    """
    allowed_domains = []

    opts = docopt(__doc__, version="0.0.1")

    logging.basicConfig(level=getattr(logging, opts.pop('loglevel')))
    r.set_loop_type("asyncio")

    with suppress(KeyError):
        allowed_domains = opts.pop('allowed_domains').split(',')
        allowed_domains = [a.strip() for a in allowed_domains]

    app = web.Application()
    app['rethinkdb'] = opts

    default_opts = aiohttp_cors.ResourceOptions(
        allow_credentials=True, expose_headers="*", allow_headers="*")
    cors = aiohttp_cors.setup(app, defaults={
        dom: default_opts for dom in allowed_domains})

    cors.add(cors.add(app.router.add_resource('/')).add_route('*', Dispatcher))
    cors.add(cors.add(app.router.add_resource('/ws')).add_route('*', wshandle))
    web.run_app(app)
예제 #5
0
 def setup(self, config):
     """
     Setups the database configuartion and sets the RethinkDB loop type to
     asyncio to be compatible with aiohttp.
     """
     r.set_loop_type("asyncio")
     self.config = config
예제 #6
0
    async def connect_to_rethinkdb(self):
        """Create our connection to rethinkdb.  This is what we use for storing user locations.
        This will create the table and stuff if necessary"""
        if r is not None:
            r.set_loop_type("asyncio")
        print("connecting to rethinkdb....")
        if 'rethinkdb' not in self.bot.api_keys:
            print(
                "Looks like you don't have a rethinkdb password specified in the api key file."
            )
            print_why_rethinkdb()
        print("Good, had password in api key file")
        self.conn = await r.connect("localhost",
                                    RETHINKDB_PORT,
                                    user='******',
                                    password=self.bot.api_keys['rethinkdb'])
        print("Connected to rethinkdb")

        try:
            tables = await r.db("lamp").table_list().run(self.conn)
        except r.errors.ReqlOpFailedError:
            await r.db_create("lamp").run(self.conn)
            await r.db("lamp").table_create("user_locations").run(self.conn)
            print("Created db and table for lamp user location registration")
        else:
            if "user_locations" not in tables:
                await r.db("lamp").table_create("user_locations").run(self.conn
                                                                      )
                print("Created table for lamp user location registration")
예제 #7
0
async def create_connection():
    """Create a new connection to RethinkDB for async interactions."""
    env = Env()
    r.set_loop_type("asyncio")
    connection = await r.connect(host=env("DB_HOST"),
                                 port=env("DB_PORT"),
                                 db=env("DB_NAME"))
    return connection
예제 #8
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.conn = None
     r.set_loop_type("asyncio")
     self.register_listener(self._connect_rethinkdb, "before_server_start")
     self.search = None
     self.websocket_manager = None
     self.version_cache = None
예제 #9
0
 def initialize(self):
     app = self.application
     if app.conn is None:
         host = app.rethinkdb_host
         port = app.rethinkdb_port
         db = app.rethinkdb_db
         rethinkdb.set_loop_type("tornado")
         app.conn = rethinkdb.connect(host=host, port=port, db=db)
예제 #10
0
 def __init__(self, *args, **kwargs):
     super(ConnectBot, self).__init__(*args, **kwargs)
     self.credentials = credentials
     r.set_loop_type('gevent')
     self.rethink = r.connect(host=self.credentials['rethink_db']['ip'], port=self.credentials['rethink_db']['port'],
                              password=self.credentials['rethink_db']['password'], db='connect')
     self.connect_py = connect.Client()
     self.up_time = datetime.datetime.utcnow()
     self.process = psutil.Process()
예제 #11
0
파일: engine.py 프로젝트: 83tb/rethink-lms
def main():
    """ Async main method. It needed to be async due to r.connect is async . """
    parse_command_line()
    db_name = "engine"
    setup_db(db_name)
    r.set_loop_type("tornado")
    db = yield r.connect("localhost", db=db_name)
    http_server = httpserver.HTTPServer(EngineApp(db))
    http_server.listen(options.port)
예제 #12
0
def get_db_conn():
    """Yields a RethinkDB connection"""
    r.set_loop_type("tornado")
    try:
        conn = r.connect(host=RETHINK_HOST,
                           port=RETHINK_PORT,
                           db=DB_NAME)
    except r.RqlRuntimeError:
        conn = r.connect(host=RETHINK_HOST, port=RETHINK_PORT)
    return conn
예제 #13
0
async def open_connections(app):
    LOGGER.warning('opening database connection')
    r.set_loop_type('asyncio')
    app.config.DB_CONN = await r.connect(host=app.config.DB_HOST,
                                         port=app.config.DB_PORT,
                                         db=app.config.DB_NAME)

    app.config.VAL_CONN = Connection(app.config.VALIDATOR_URL)

    LOGGER.warning('opening validator connection')
    app.config.VAL_CONN.open()
예제 #14
0
def main():
    """ Async main method. It needed to be async due to r.connect is async . """
    parse_command_line()
    db_name = "rechat"
    setup_db(db_name)
    r.set_loop_type("tornado")

    db = yield r.connect("localhost", db=db_name)
    #Single db connection for everything thanks a lot Ben and Jeese
    http_server = httpserver.HTTPServer(RechatApp(db))
    http_server.listen(options.port)
예제 #15
0
def main():
    """ Async main method. It needed to be async due to r.connect is async . """
    parse_command_line()
    db_name = "rechat"
    setup_db(db_name)
    r.set_loop_type("tornado")

    db = yield r.connect("localhost", db=db_name)
    #Single db connection for everything thanks a lot Ben and Jeese
    http_server = httpserver.HTTPServer(RechatApp(db))
    http_server.listen(options.port)
예제 #16
0
파일: rest.py 프로젝트: zh0uquan/myslice-v2
def connect():
    try :

        r.set_loop_type("tornado")

        return r.connect(host=Config.rethinkdb["host"], port=Config.rethinkdb["port"], db=Config.rethinkdb['db'])

    except r.RqlDriverError :

        logger.error("Can't connect to RethinkDB")
        raise SystemExit("Can't connect to RethinkDB")
예제 #17
0
async def open_connections(app):
    logging.debug("Tryng to initialise db")
    r.set_loop_type('asyncio')
    result = await r.connect(port=app.config.DATABASE["port"],
                             host=app.config.DATABASE["ip"],
                             db=app.config.DATABASE["dbname"],
                             user=app.config.DATABASE["user"],
                             password=app.config.DATABASE["password"])
    #app.config.VAL_CONN = Connection(app.config.VALIDATOR_URL)
    #app.config.VAL_CONN.open()
    logging.debug("Rethinkdb connection made")
    return result
예제 #18
0
async def open_connections(app):
    LOGGER.warning('opening database connection')
    r.set_loop_type('asyncio')
    app.config.DB_CONN = await r.connect(
        host=app.config.DB_HOST,
        port=app.config.DB_PORT,
        db=app.config.DB_NAME)

    app.config.VAL_CONN = Connection(app.config.VALIDATOR_URL)

    LOGGER.warning('opening validator connection')
    app.config.VAL_CONN.open()
예제 #19
0
파일: config.py 프로젝트: Phxntxm/Bonfire
async def remove_content(table, key):
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    try:
        result = await r.table(table).get(key).delete().run(conn)
    except r.ReqlOpFailedError:
        result = {}
        pass

    await conn.close()
    if table == 'server_settings':
        loop.create_task(cache[table].update())
    return result.get('deleted', 0) > 0
예제 #20
0
async def replace_content(table, content, key):
    # This method is here because .replace and .update can have some different functionalities
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    try:
        result = await r.table(table).get(key).replace(content).run(conn)
    except r.ReqlOpFailedError:
        result = {}

    await conn.close()
    if table == 'server_settings':
        loop.create_task(cache[table].update())
    return result.get('replaced', 0) > 0 or result.get('unchanged', 0) > 0
예제 #21
0
async def remove_content(table, key):
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    try:
        result = await r.table(table).get(key).delete().run(conn)
    except r.ReqlOpFailedError:
        result = {}
        pass

    await conn.close()
    if table == 'server_settings':
        loop.create_task(cache[table].update())
    return result.get('deleted', 0) > 0
예제 #22
0
async def filter_content(table: str, r_filter):
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    try:
        cursor = await r.table(table).filter(r_filter).run(conn)
        content = await _convert_to_list(cursor)
        if len(content) == 0:
            content = None
    except (IndexError, r.ReqlOpFailedError):
        content = None

    await conn.close()
    return content
예제 #23
0
파일: config.py 프로젝트: Phxntxm/Bonfire
async def replace_content(table, content, key):
    # This method is here because .replace and .update can have some different functionalities
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    try:
        result = await r.table(table).get(key).replace(content).run(conn)
    except r.ReqlOpFailedError:
        result = {}

    await conn.close()
    if table == 'server_settings':
        loop.create_task(cache[table].update())
    return result.get('replaced', 0) > 0 or result.get('unchanged', 0) > 0
예제 #24
0
파일: config.py 프로젝트: Phxntxm/Bonfire
async def filter_content(table: str, r_filter):
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    try:
        cursor = await r.table(table).filter(r_filter).run(conn)
        content = await _convert_to_list(cursor)
        if len(content) == 0:
            content = None
    except (IndexError, r.ReqlOpFailedError):
        content = None

    await conn.close()
    return content
예제 #25
0
파일: config.py 프로젝트: dddlr/Bonfire
async def remove_content(table, r_filter=None):
    if r_filter is None:
        r_filter = {}
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    try:
        result = await r.table(table).filter(r_filter).delete().run(conn)
    except r.ReqlOpFailedError:
        result = {}
        pass
    await conn.close()
    if table == 'prefixes' or table == 'custom_permissions':
        loop.create_task(cache[table].update())
    return result.get('deleted', 0) > 0
예제 #26
0
파일: server.py 프로젝트: johsam/srv-ab
def main():

    define("port", default=8080, help="bind to this port", type=int)
    define("listen", default="127.0.0.1", help="listen address", type=str)
    define("debug", default=False, help="debug", type=bool)
    define("rethinkdb_host",
           default="localhost",
           help="rethinkdb hostname",
           type=str)
    define("rethinkdb_db",
           default="",
           help="rethinkdb default database",
           type=str)

    tornado.options.parse_command_line()

    applicaton = Application()
    http_server = tornado.httpserver.HTTPServer(applicaton, xheaders=True)
    http_server.listen(options.port, address=options.listen)

    # Setup signal handlers
    signal.signal(signal.SIGINT,
                  lambda sig, frame: ioloop.add_callback_from_signal(exit))
    signal.signal(signal.SIGTERM,
                  lambda sig, frame: ioloop.add_callback_from_signal(exit))
    atexit.register(shutdownHandler)

    # Setup logging
    my_log_format = '%(color)s%(asctime)s %(levelname)1.1s [%(module)s:%(lineno)d]%(end_color)s %(message)s'
    my_log_formatter = LogFormatter(fmt=my_log_format,
                                    datefmt='%Y-%m-%d %H:%H:%S',
                                    color=False)

    for handler in logging.getLogger().handlers:
        handler.setFormatter(my_log_formatter)

    try:
        _conn = r.connect(options.rethinkdb_host, 28015, options.rethinkdb_db)
    except:  # pylint: disable=bare-except
        logging.error("Could not connect to rethinkdb on host '%s'",
                      options.rethinkdb_host,
                      exc_info=False)
        sys.exit(1)

    # Fire up our server

    logging.info('Server started on %s:%d', options.listen, options.port)

    r.set_loop_type("tornado")
    ioloop.start()
예제 #27
0
    def __init__(self, loop_type: str = "gevent"):
        self.host = os.environ.get("RETHINKDB_HOST", "127.0.0.1")
        self.port = os.environ.get("RETHINKDB_PORT", "28016")
        self.database = os.environ.get("RETHINKDB_DATABASE", "pythondiscord")
        self.log = logging.getLogger()
        self.conn = None

        rethinkdb.set_loop_type(loop_type)

        with self.get_connection(connect_database=False) as conn:
            try:
                rethinkdb.db_create(self.database).run(conn)
                self.log.debug(f"Database created: '{self.database}'")
            except rethinkdb.RqlRuntimeError:
                self.log.debug(f"Database found: '{self.database}'")
예제 #28
0
파일: config.py 프로젝트: dddlr/Bonfire
async def replace_content(table, content, r_filter=None):
    # This method is here because .replace and .update can have some different functionalities
    if r_filter is None:
        r_filter = {}
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    try:
        result = await r.table(table).filter(r_filter).replace(content).run(
            conn)
    except r.ReqlOpFailedError:
        await conn.close()
        result = {}
    await conn.close()
    if table == 'prefixes' or table == 'custom_permissions':
        loop.create_task(cache[table].update())
    return result.get('replaced', 0) > 0 or result.get('unchanged', 0) > 0
예제 #29
0
async def update_content(table, content, key):
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    # This method is only for updating content, so if we find that it doesn't exist, just return false
    try:
        # Update based on the content and filter passed to us
        # rethinkdb allows you to do many many things inside of update
        # This is why we're accepting a variable and using it, whatever it may be, as the query
        result = await r.table(table).get(key).update(content).run(conn)
    except r.ReqlOpFailedError:
        result = {}

    await conn.close()
    if table == 'server_settings':
        loop.create_task(cache[table].update())
    return result.get('replaced', 0) > 0 or result.get('unchanged', 0) > 0
예제 #30
0
파일: config.py 프로젝트: dddlr/Bonfire
async def get_content(table: str, r_filter=None):
    if r_filter is None:
        r_filter = {}
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    try:
        cursor = await r.table(table).filter(r_filter).run(conn)
        content = await _convert_to_list(cursor)
        if len(content) == 0:
            content = None
    except (IndexError, r.ReqlOpFailedError):
        content = None
    await conn.close()
    if table == 'prefixes' or table == 'custom_permissions':
        loop.create_task(cache[table].update())
    return content
예제 #31
0
파일: config.py 프로젝트: Phxntxm/Bonfire
async def update_content(table, content, key):
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    # This method is only for updating content, so if we find that it doesn't exist, just return false
    try:
        # Update based on the content and filter passed to us
        # rethinkdb allows you to do many many things inside of update
        # This is why we're accepting a variable and using it, whatever it may be, as the query
        result = await r.table(table).get(key).update(content).run(conn)
    except r.ReqlOpFailedError:
        result = {}

    await conn.close()
    if table == 'server_settings':
        loop.create_task(cache[table].update())
    return result.get('replaced', 0) > 0 or result.get('unchanged', 0) > 0
예제 #32
0
async def _get_content(key: str):
    # We need to make sure we're using asyncio
    r.set_loop_type("asyncio")
    # Just connect to the database
    conn = await r.connect(**db_opts)
    # We should only ever get one result, so use it if it exists, otherwise return none
    try:
        cursor = await r.table(key).run(conn)
        await conn.close()
        items = list(cursor.items)[0]
    except (IndexError, r.ReqlOpFailedError):
        await conn.close()
        return {}
    # Rethink db stores an internal id per table, delete this and return the rest
    del items['id']
    return items
예제 #33
0
def setup():
    conn = r.connect(RDB_HOST, RDB_PORT)

    def safe_run(rql):
        try:
            return rql.run(conn)
        except r.RqlRuntimeError:
            return False

    print("database init db and tables")
    safe_run(r.db_create(RDB_NAME))
    safe_run(r.db(RDB_NAME).table_create("tasks", primary_key='id'))
    safe_run(r.db(RDB_NAME).table_create('devices', primary_key='id'))
    print("database init done")
    conn.close()

    r.set_loop_type('tornado')
예제 #34
0
    def start(self):
        ''' Start the event loop. '''
        self._logger.info('Starbelly is starting...')
        r.set_loop_type('asyncio')
        sys.excepthook = async_excepthook
        loop = asyncio.get_event_loop()
        self._main_task = asyncio.ensure_future(self.run())
        loop.run_until_complete(self._main_task)

        # Check if any tasks weren't properly cleaned up.
        tasks = [t for t in asyncio.Task.all_tasks() if not t.done()]
        if len(tasks) > 0:
            self._logger.error('There are %d unfinished tasks: %r',
                len(tasks), tasks)

        loop.close()
        self._logger.info('Starbelly has stopped.')
예제 #35
0
파일: config.py 프로젝트: Phxntxm/Bonfire
async def add_content(table, content):
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    # First we need to make sure that this entry doesn't exist
    # For all rethinkDB cares, multiple entries can exist with the same content
    # For our purposes however, we do not want this
    try:
        result = await r.table(table).insert(content).run(conn)
    except r.ReqlOpFailedError:
        # This means the table does not exist
        await r.table_create(table).run(conn)
        await r.table(table).insert(content).run(conn)
        result = {}

    await conn.close()
    if table == 'server_settings':
        loop.create_task(cache[table].update())
    return result.get('inserted', 0) > 0
예제 #36
0
async def add_content(table, content):
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)
    # First we need to make sure that this entry doesn't exist
    # For all rethinkDB cares, multiple entries can exist with the same content
    # For our purposes however, we do not want this
    try:
        result = await r.table(table).insert(content).run(conn)
    except r.ReqlOpFailedError:
        # This means the table does not exist
        await r.table_create(table).run(conn)
        await r.table(table).insert(content).run(conn)
        result = {}

    await conn.close()
    if table == 'server_settings':
        loop.create_task(cache[table].update())
    return result.get('inserted', 0) > 0
예제 #37
0
    async def init(cls,
                   scheme,
                   create_scheme=True,
                   loop_type="asyncio",
                   event_loop=False):

        r.set_loop_type(loop_type)

        if event_loop:
            asyncio.set_event_loop(event_loop)

        self = Thinker()
        self.db = await r.connect(host=scheme['host'],
                                  db=scheme['db'],
                                  port=scheme['port'])
        if create_scheme:
            await initiator.create_tables(scheme=scheme, connection=self.db)
        return self
예제 #38
0
def main():
    global conn
    tornado.options.options.parse_command_line()
    r.set_loop_type('tornado')
    conn = yield r.connect(host='127.0.0.1', db='gec')
    settings = {
        'static_path': os.path.join(os.path.dirname(__file__), 'static'),
        'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
    }
    application = tornado.web.Application([
        (r'/', IndexHandler),
        (r'/course', CourseHandler),
        (r'/course/(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})', CourseDetailHandler),
        (r'/course/(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})/page/(\d+)', CourseDetailPageHandler),
        (r'/about', AboutHandler),
        (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': 'static'}),
    ], **settings)
    application.listen(tornado.options.options.port, '127.0.0.1', xheaders=True)
예제 #39
0
 def get(self, *args):
     self.finish()
     id = self.get_argument("id")
     value = self.get_argument("value")
     #value = 400
     #print(self.get_body_argument("balkanbox", default=None, strip=False))
     #subItem = "flask"
     print(value)
     #print("set loop")
     r.set_loop_type("tornado")
     print("connect")
     conn = r.connect('35.166.62.31', 28015, db='poeapi')
     #print(conn)
     connection = yield conn
     print("getting cursor")
     cursor = yield r.table('itemList').filter(
         lambda doc: doc['itemName'].match("(?i)" + value)).filter(
             (r.row["price"] < 999)
             & (r.row["price"] > 0)).changes(squash=True).run(connection)
     #cursor = yield r.table('itemList').filter((r.row["price"] < 999) & (r.row["price"] > 0)).changes().run(connection)
     print("prepare to loop")
     while (yield cursor.fetch_next()):
         item = yield cursor.next()
         item = item["new_val"]
         #print(item)
         #time.sleep(1)
         tempSTD = 0
         avg = 0
         itemPrice = 9999
         try:
             tempItem = yield r.table('lookUp').get(
                 item['itemName']).run(connection)
             tempSTD = tempItem["STD"]
             avg = tempItem["avgPrice"]
             itemPrice = item["price"]
         except:
             print('error getting price')
         if (itemPrice <= avg - tempSTD):
             #print(item)
             data = {"id": id, "value": json.dumps(item)}
             data = json.dumps(data)
             for c in cl:
                 c.write_message(data)
예제 #40
0
    def cmd_debug(self, *args):
        """
        Comando 'debug', responsável por iniciar o servidor de desenvolvimento.
        """
        port = int(args[0]) if args else self.port
        ServerLog.info(code='dev_server_starting', args=[port])
        app = Application(routers, debug=True, autoreload=False,
                          cookie_secret=config('SECRET_KEY'),
                          login_url='/api/v1/usuarios/entrar/',
                          xsrf_cookies=False)

        app.listen(port)
        r.set_loop_type('tornado')
        ioloop = IOLoop.current()
        ioloop.add_callback(
            callback=ServerLog.info,
            code='server_started',
            args=[self.host, port]
        )
        ioloop.start()
예제 #41
0
    def __init__(self, **kwargs):
        logging.basicConfig(level=logging.INFO)
        r.set_loop_type("asyncio")

        if "shard_count" not in kwargs:
            kwargs['shard_count'] = os.environ.get("SHARD_COUNT")

        if "shard_ids" not in kwargs:
            kwargs['shard_ids'] = int_list_or_none(os.environ.get("SHARD_IDS"))

        kwargs['command_prefix'] = os.environ.get("COMMAND_PREFIX") or "h!"

        super().__init__(**kwargs)
        self.remove_command("help")
        self.logger = logging.getLogger("highlightpy.core.client")
        self.logger.info("Creating the RethinkDB connection.")
        self.rethink_connection = loop.run_until_complete(
            self._get_rethink_connection())
        self.token = os.environ['BOT_TOKEN']
        self.logger.info("Checking the database/tables.")
        loop.run_until_complete(create_tables(self.rethink_connection))
예제 #42
0
파일: checks.py 프로젝트: shadeyg56/Bonfire
async def db_check():
    """Used to check if the required database/tables are setup"""
    db_opts = config.db_opts

    r.set_loop_type('asyncio')
    # First try to connect, and see if the correct information was provided
    try:
        conn = await r.connect(**db_opts)
    except r.errors.ReqlDriverError:
        print(
            "Cannot connect to the RethinkDB instance with the following information: {}"
            .format(db_opts))

        print(
            "The RethinkDB instance you have setup may be down, otherwise please ensure you setup a"
            " RethinkDB instance, and you have provided the correct database information in config.yml"
        )
        quit()
        return

    # Get the current databases and check if the one we need is there
    dbs = await r.db_list().run(conn)
    if db_opts['db'] not in dbs:
        # If not, we want to create it
        print('Couldn\'t find database {}...creating now'.format(
            db_opts['db']))
        await r.db_create(db_opts['db']).run(conn)
        # Then add all the tables
        for table, key in required_tables.items():
            print("Creating table {}...".format(table))
            await r.table_create(table, primary_key=key).run(conn)
        print("Done!")
    else:
        # Otherwise, if the database is setup, make sure all the required tables are there
        tables = await r.table_list().run(conn)
        for table, key in required_tables.items():
            if table not in tables:
                print("Creating table {}...".format(table))
                await r.table_create(table, primary_key=key).run(conn)
        print("Done checking tables!")
예제 #43
0
 def cmd_serve(self, *args):
     """
     Comando 'serve', responsável por iniciar o servidor de produção.
     """
     port = int(args[0]) if args else self.port
     ServerLog.info(code='dev_server_starting', args=[port])
     app = Application(
         routers, cookie_secret=config('SECRET_KEY'),
         login_url='/api/v1/usuarios/entrar/',
         xsrf_cookies=False)
     sockets = netutil.bind_sockets(port)
     process.fork_processes(self.fork_processes)
     server = HTTPServer(app)
     server.add_sockets(sockets)
     r.set_loop_type('tornado')
     ioloop = IOLoop.current()
     ioloop.add_callback(
         callback=ServerLog.info,
         code='server_started',
         args=[self.host, port]
     )
     ioloop.start()
def listen_payments():
    import rethinkdb as r
    r.set_loop_type('tornado')
    print('Entering listen_payments')
    global play_until
    b = Bigchain()
    conn = yield b.conn
    feed = yield r.table('bigchain').changes().run(conn)

    while (yield feed.fetch_next()):
        block = yield feed.next()
        if not block['old_val']:
            coins = get_coin_from_block(block['new_val'])
            update_shares(block['new_val'])
            if coins:
                print('received coins')
            if time.time() > play_until:
                play_until = time.time() + 10 * len(coins)
            else:
                play_until += 10 * len(coins)
        else:
            get_coin_shares_from_block(block['new_val'])
예제 #45
0
파일: config.py 프로젝트: Phxntxm/Bonfire
async def get_content(table, key=None):
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)

    try:
        if key:
            cursor = await r.table(table).get(key).run(conn)
        else:
            cursor = await r.table(table).run(conn)
        if cursor is None:
            content = None
        elif type(cursor) is not dict:
            content = await _convert_to_list(cursor)
            if len(content) == 0:
                content = None
        else:
            content = cursor
    except (IndexError, r.ReqlOpFailedError):
        content = None

    await conn.close()
    return content
예제 #46
0
def listen_payments():
    import rethinkdb as r
    r.set_loop_type('tornado')
    print('Entering listen_payments')
    global play_until
    b = Bigchain()
    conn = yield b.conn
    feed = yield r.table('bigchain').changes().run(conn)

    while (yield feed.fetch_next()):
        block = yield feed.next()
        if not block['old_val']:
            coins = get_coin_from_block(block['new_val'])
            update_shares(block['new_val'])
            if coins:
                print('received coins')
            if time.time() > play_until:
                play_until = time.time() + 10 * len(coins)
            else:
                play_until += 10 * len(coins)
        else:
            get_coin_shares_from_block(block['new_val'])
예제 #47
0
async def get_content(table, key=None):
    r.set_loop_type("asyncio")
    conn = await r.connect(**db_opts)

    try:
        if key:
            cursor = await r.table(table).get(key).run(conn)
        else:
            cursor = await r.table(table).run(conn)
        if cursor is None:
            content = None
        elif type(cursor) is not dict:
            content = await _convert_to_list(cursor)
            if len(content) == 0:
                content = None
        else:
            content = cursor
    except (IndexError, r.ReqlOpFailedError):
        content = None

    await conn.close()
    return content
예제 #48
0
def init_db(app):
    rethinkdb.set_loop_type('gevent')
    db.init_app(app)
예제 #49
0
async def prepare():
    global connection
    r.set_loop_type('asyncio')
    connection = await r.connect(host="localhost", port=28015)
예제 #50
0
import rethinkdb
rethinkdb.set_loop_type("asyncio")

__version__ = "0.2.2-pre"

# constants
ALL             = 0
DECLARED_ONLY   = 1
UNDECLARED_ONLY = 2

from .errors import *
from .db import *
from .values_and_valuetypes import *
from .field import *
from .document import *
예제 #51
0
import rethinkdb as r
from logbook import error

r.set_loop_type('asyncio')

def make_rdb_connection(config):
    try:
        db_config = config['database']

        args = dict(db_config.items())
        args.update({
            'host': db_config['host'],
            'port': db_config.get('port', 28015),
            'db': db_config.get('db', 'iron-strategy')
        })

        return r.connect(**args)
    except KeyError as e:
        error('Config error: could not find key: {}'.format(e))
    except Exception as e:
        error('Could not establish the database connection: {}'.format(e))
예제 #52
0
import functools
import os
import logging

from tornado import websocket, web, ioloop
from tornado.gen import coroutine

import rethinkdb as r

from init_accounts import get_bigchain

clients = []
bigchain = get_bigchain()

# from http://blog.hiphipjorge.com/django-and-realtime-using-django-with-tornado-and-rethinkdb/
r.set_loop_type('tornado')


logger = logging.getLogger('tornado')


@coroutine
def print_changes(db_table):
    conn = yield bigchain.conn
    feed = yield r.table(db_table).changes().run(conn)
    while (yield feed.fetch_next()):
        change = yield feed.next()
        block = get_block_from_change(change, db_table)
        for client in clients:
            for tx in block:
                # TODO: use REQL for filtering
예제 #53
0
 def __init__(self, bot):
     self.bot = bot
     self.db_name = self.bot.config.rname
     self.db = None
     r.set_loop_type("asyncio")
     self.ready = False
예제 #54
0
#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
"""
ATTEMPT TO TEST A SIMPLE AIOHTTP ASYNCIO API SERVER (without AUTOBAHN).
"""

import asyncio
import json
import rethinkdb as r

from aiohttp import web
from datetime import datetime
from rethinkdb.errors import ReqlOpFailedError
from uuid import uuid4

r.set_loop_type("asyncio")

def create_db_and_table():
    try:
        r.db_create('ep16').run(conn)
    except ReqlOpFailedError:
        pass
    try:
        r.db('ep16').table_create('scores').run(conn)
    except ReqlOpFailedError:
        pass

def json_default(obj):
    if isinstance(obj, datetime):
        return obj.strftime('%Y%m%dT%H:%M:%S+00:00')
예제 #55
0
def connect():
    r.set_loop_type("asyncio")
    r.connect('192.168.99.100', 32769).repl()
예제 #56
0
 def __init__(self, *args, **kwargs):
     super(RethinkdbDiscoveryPolicy, self).__init__(*args, **kwargs)
     self.feed_table = kwargs.get(u'feed_table', u'feed')
     self.change_key = kwargs.get(u'change_key', u'new_val')
     rethinkdb.set_loop_type(u"tornado")
import rethinkdb as r
from tornado import ioloop, gen
from search import handle_query
from utils import get_dt

r.set_loop_type("tornado")
def changed_keys(change):
    ck = []
    new_val = change.get('new_val', {})
    if not new_val:
        new_val = {}
    old_val = change.get('old_val', {}) # ugh default = {} not working
    if not old_val:
        old_val = {}
    for key in new_val:
        print 'old_val', old_val
        if not key in old_val:
            ck.append(key)
        elif change['new_val'][key] != change['old_val'][key]:
            ck.append(key)
    for key in old_val:
        print 'new_val', new_val
        if not key in change.get('new_val', {}):
            ck.append(key)
        elif change['new_val'][key] != change['old_val'][key]:
            ck.append(key)
    return list(set(ck))

@gen.coroutine
def update_row_counts(table):
    conn = yield r.connect(host="localhost", port=28015)
	def __init__(self, connData, sensors):
		super(RDBReader, self).__init__()
		self.sensors = sensors
		rdb.set_loop_type('tornado')
		#self.conn = rdb.connect(host=connData["host"], port=connData["port"], db=connData["db"], auth_key=connData["auth_key"])
		self.conn = rdb.connect(host=connData["host"], port=connData["port"], db=connData["db"])