Beispiel #1
0
def setup_database():
    r = RethinkDB()
    r.connect(host='localhost', port=28015).repl()

    try:
        # Create databases
        r.db_create('platform').run()
        # Create tables
        r.db('platform').table_create('videos', primary_key='video_id').run()
    except Exception as e:
        print('Database is already setup')
    else:
        print('Database setup successful')
Beispiel #2
0
def runMe():
    questions = [{
        'type': 'input',
        'message': 'Enter server IP',
        'name': 'serverIP',
        'default': 'pavela.net'
    }, {
        'type': 'input',
        'message': 'Enter your username',
        'name': 'username',
    }]
    answers = prompt(questions)
    serverIP = answers['serverIP']
    yourUserName = answers['username']

    r1 = RethinkDB()
    r2 = RethinkDB()

    def checkMessages():
        r1.connect(serverIP, 28015).repl()
        cursor = r1.table("chat").changes().run()
        for document in cursor:
            username = document['new_val']['username']
            message = document['new_val']['message']

            print(Style.BRIGHT + Fore.YELLOW +
                  "[{username}]: ".format(username=username) +
                  Style.RESET_ALL + "{message}".format(message=message))

    checkThread = threading.Thread(target=checkMessages)
    checkThread.start()
    # To stop thread: checkThread.join()

    questions = [
        {
            'type': 'input',
            'message': '>',
            'name': 'yourMessage',
        },
    ]
    while True:
        answers = prompt(questions)
        yourMessage = answers['yourMessage']
        sys.stdout.write("\033[F")  # Clears the text above
        r2.connect(serverIP, 28015).repl()
        r2.table('chat').insert([{
            "username": yourUserName,
            "message": yourMessage
        }]).run()
Beispiel #3
0
class Iface:
    def __init__(self, host="127.0.0.1", port=28015, db="arcos"):
        self.r = RethinkDB()
        self.conn = self.r.connect(host=host, port=port, db=db)

    def clear_table(self, table: str):
        self.r.table(table).delete().run(self.conn)

    def create_table(self, table: str) -> bool:
        try:
            self.r.table_create(table).run(self.conn)
            return True
        except ReqlOpFailedError:
            return False

    def get_by_uid(self, table: str, uid_name: str, uid_value: str) -> object:
        cursor = self.r.table(table).filter(self.r.row[uid_name] == uid_value).run(self.conn)
        for document in cursor:
            return document
        return None

    def insert(self, table: str, uid_name: str, uid_value: str, data: object) -> bool:
        if self.get_by_uid(table, uid_name, uid_value):
            return False
        data[uid_name] = uid_value
        self.r.table(table).insert(data).run(self.conn)
        return True

    def update(self, table: str, uid_name: str, uid_value: str, data: object) -> bool:
        if not self.get_by_uid(table, uid_name, uid_value):
            self.insert(table, uid_name, uid_value, data)
            return True
        self.r.table(table).filter(self.r.row[uid_name] == uid_value).update(data).run(self.conn)
Beispiel #4
0
def test_flow_couroutine_paradigm():

    r = RethinkDB()
    r.set_loop_type("asyncio")

    connection = yield from r.connect(os.getenv("REBIRTHDB_HOST"))

    try:
        yield from r.db_create(INTEGRATION_TEST_DB).run(connection)
    except ReqlRuntimeError:
        pass

    connection.use(INTEGRATION_TEST_DB)

    yield from r.table_create("marvel").run(connection)

    marvel_heroes = r.table('marvel')
    yield from marvel_heroes.insert({
        'id': 1,
        'name': 'Iron Man',
        'first_appearance': 'Tales of Suspense #39'
    }).run(connection)

    cursor = yield from marvel_heroes.run(connection)

    while (yield from cursor.fetch_next()):
        hero = yield from cursor.__anext__()
        assert hero['name'] == 'Iron Man'

    yield from connection.close()
Beispiel #5
0
class BaneSensor:
    def __init__(self, ip='localhost', bane_navn='Rink A'):
        self.bane_navn = bane_navn
        self.r = RethinkDB()
        self.conn = self.r.connect(ip, 28015)

    def oppdater_sensordata(self, temperatur, luftfuktighet, count=0):
        try:
            self.r.db(db_navn).table(status_tabell_navn).filter({
                'bane':
                self.bane_navn
            }).update({
                'luftfuktighet': luftfuktighet,
                'temperatur': temperatur,
            }).run(self.conn)
        except:
            if (count <= 20):
                count += 1
                self.oppdater_sensordata(temperatur, luftfuktighet, count)
            else:
                print(
                    f'{self.bane_navn}: Feilet med å laste opp data til databasen'
                )

        print(f'{self.bane_navn}: Sensordata oppdatert')
Beispiel #6
0
class DB():
    def __init__(self):
        self.dbname = APP_CONFIG['DB']['DB_NAME']
        self.tbmovie = 'movies'

        self.r = RethinkDB()
        try:
            self.conn = self.r.connect(host=APP_CONFIG['DB']['DB_HOST'],
                                       db=APP_CONFIG['DB']['DB_NAME'],
                                       user=APP_CONFIG['DB']['DB_USER'],
                                       password=APP_CONFIG['DB']['DB_PASS'],
                                       port=APP_CONFIG['DB']['DB_PORT'])
            self.create_db()
        except Exception as ex:
            print('DB_ERROR', ex)

    def create_db(self):
        if not self.r.db_list().contains(self.dbname).run(self.conn):
            self.r.db_create(self.dbname).run(self.conn)

    def create_table(self):
        if not self.r.db(self.dbname).table_list().contains(self.tbmovie).run(
                self.conn):
            self.r.db(self.dbname).table_create(self.tbmovie).run(self.conn)

    def get_conn(self):
        return self.conn

    def get_r(self):
        return self.r
Beispiel #7
0
 def connection(self):
     r = RethinkDB()
     try:
         conn = r.connect(host=self.RDB_HOST, port=self.RDB_PORT)
         return conn
     except RqlDriverError as e:
         raise
Beispiel #8
0
class RDB:
    def __init__(self):

        self.dbname = "DB"
        self.r = RethinkDB()
        self.connection = self.r.connect("127.0.0.1", "28015").repl()
        self.connection.use(self.dbname)

        self._init_db()

    def _init_db(self):
        if not self.dbname in self.r.db_list().run(self.connection):
            self.r.db_create(self.dbname).run(self.connection)

        tables = self.r.table_list().run(self.connection)
        if "meta" not in tables:
            # table with meta data independant of each blockchain
            self.r.table_create("meta", primary_key="key").run(self.connection)
        if "accounts" not in tables:
            # address table
            self.r.table_create("accounts",
                                primary_key="account_id").run(self.connection)
        if "pseudo_ids" not in tables:
            # table for pseudo_ids for mapping back-end wallets to front-end user
            self.r.table_create("pseudo_ids").run(self.connection)
        if "transactions" not in tables:
            # transaction table
            self.r.table_create("transactions",
                                primary_key="operation_id").run(
                                    self.connection)
Beispiel #9
0
def auto(member: Type[Model]):
    """Automatic database and table creation for the given type (Modelchild)."""
    if not issubclass(member, Model) or member is Model:
        return

    rdb = RethinkDB()
    conn = rdb.connect(
        host=db.HOST,
        port=db.PORT,
        db=db.DB_NAME,
        user=db.USER,
        password=db.PASSWORD,
        ssl=db.SSL,
        timeout=db.TIMEOUT,
    )

    tables = rdb.table_list().run(conn)
    if member.tablename not in tables:
        LOG.info("create table %s", member.tablename)
        rdb.table_create(member.tablename).run(conn)
        indexes = member.get_indexes()
        if indexes:
            # TODO: at this time, it's only working with simple index
            for index in indexes:
                rdb.table(member.tablename).index_create(index).run(conn)
                rdb.table(member.tablename).index_wait(index).run(conn)

    conn.close()
Beispiel #10
0
class Database:
    def __init__(self, host, port, user, password, db):
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.db = db
        self.r = RethinkDB()
        self.conn = None

    def query_results(self, query):
        """
        queries the database for games matching the given string
        """
        results = []
        if query == "":
            return results

        # query for products
        self.connect()

        cursor = self.r.table('products').filter(
            (self.r.row["app_name"].match("(?i)" + query))
            & (self.r.row["is_dlc"] == False)).limit(20).run(self.conn)

        # build result array
        for item in cursor:
            if item is not None:
                results.append(item)

        # close the database connection
        self.teardown()

        return results

    def connect(self):
        """
        opens a new connection to the database
        """
        try:
            # connect to the database
            self.conn = self.r.connect(
                host=self.host,
                port=self.port,
                db=self.db,
                user=self.user,
                password=self.password,
            )
        except RqlDriverError:
            abort(503, "No database connection could be established.")

    def teardown(self):
        """
        closes an open connection to the database
        """
        try:
            self.conn.close()
        except AttributeError:
            pass
def main():
    argument_spec = dict(
        host=dict(required=True),
        port=dict(required=False, type=int, default=28015),
        user=dict(required=False, default="admin"),
        password=dict(required=False, default="", no_log=True),
        ssl=dict(required=False, type=dict, default=None),
        table=dict(
            required=False,
            choices=[
                "table_config",
                "server_config",
                "db_config",
                "cluster_config",
                "table_status",
                "server_status",
                "current_issues",
                "users",
                "permissions",
                "jobs",
                "stats",
                "logs",
            ],
            default="server_status",
        ),
        limit=dict(required=False, type=int, default=10),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
    )

    client = RethinkDB()
    _params = {
        "host": module.params["host"],
        "port": module.params["port"],
        "user": module.params["user"],
        "password": module.params["password"],
        "ssl": module.params["ssl"],
        "db": "rethinkdb",
    }
    __res = []

    try:
        conn = client.connect(**_params)
        _res = (
            client.table(module.params["table"]).limit(module.params["limit"]).run(conn)
        )
        while True:
            try:
                __res.append(_res.next())
            except DefaultCursorEmpty:
                break
        module.exit_json(result=__res)
    except (ReqlAuthError, ReqlOpFailedError) as e:
        module.fail_json(msg=e.message)
    finally:
        conn.close(noreply_wait=False)
Beispiel #12
0
def execute_query(module):
    results = list()

    try:
        r = RethinkDB()
        r.connect(host=module.params.get('host'),
                  port=module.params.get('port'),
                  user=module.params.get('user'),
                  password=module.params.get('password')).repl()
        query_result = eval("r.{0}.run()".format(module.params.get('query')))
        if type(query_result) is dict:
            results.append(query_result)
        else:
            for document in query_result:
                results.append(document)
        return results
    except Exception as e:
        module.fail_json(msg="Error: {0}".format(e))
class RethinkDbDatabaseConnector(DatabaseConnector):
    def __init__(self):
        self.r = RethinkDB()
        self.conn = None

    def disconnect(self):
        self.conn.close()

    def connect(self, hostname, port, database):
        self.conn = self.r.connect(hostname, port, database)
        return self.r, self.conn
Beispiel #14
0
class Rethinkdb:
    def __init__(self):
        self.__r = RethinkDB()
        self.__connector = self.__r.connect(config.host, config.port, db=config.db, user=config.user, password=config.password).repl()

    async def closeDB(self):
        if self.__connector:
            self.__connector.close()
    
    def getR(self):
        return self.__r
Beispiel #15
0
 def setup(self):
     r = RethinkDB()
     self._connection = r.connect(host=self.RDB_HOST, port=self.RDB_PORT)
     try:
         r.db_create(self.APP_DB).run(self._connection)
         print(f' * [x] Database "{self.APP_DB}" created')
     except RqlRuntimeError:
         print(f' * [-] Database "{self.APP_DB}" is already exist.')
     finally:
         self._rdb = r.db(self.APP_DB)
         self._connection.close()
     self.init_tables()
    def test_database_creation(self):
        """Check, if :meth:rethinkmodel.manage.check_db() creates the database."""
        db_name = "test_creation"
        config(dbname=db_name)
        check_db()

        rdb = RethinkDB()
        conn = rdb.connect()
        dbs = rdb.db_list().run(conn)
        if db_name not in dbs:
            self.fail("Database named {db_name} was not created")
        conn.close()
Beispiel #17
0
def main():
    argument_spec = dict(host=dict(required=True),
                         port=dict(type=int, default=28015),
                         user=dict(default="admin"),
                         password=dict(default="", no_log=True),
                         ssl=dict(type=dict, default=None),
                         state=dict(choices=["present", "absent", "rename"],
                                    default="present"),
                         database=dict(required=True),
                         table=dict(required=True),
                         key=dict(required=True),
                         new_key=dict(required=False))

    module = AnsibleModule(argument_spec=argument_spec, )

    client = RethinkDB()
    _params = {
        "host": module.params["host"],
        "port": module.params["port"],
        "user": module.params["user"],
        "password": module.params["password"],
        "ssl": module.params["ssl"],
    }

    try:
        conn = client.connect(**_params)
        if module.params["state"].lower() == "present":
            _res = (client.db(module.params["database"]).table(
                module.params["table"]).index_create(
                    module.params["key"]).run(conn))
        elif module.params["state"].lower() == "rename":
            _res = (client.db(module.params["database"]).table(
                module.params["table"]).index_rename(
                    module.params["key"], module.params['new_key']).run(conn))
        else:
            _res = (client.db(module.params["database"]).table(
                module.params["table"]).index_drop(
                    module.params["key"]).run(conn))
        module.exit_json(changed=True, result=_res)
    except ReqlOpFailedError as e:
        if module.params["state"].lower(
        ) == "present" and "already exists" in e.message:
            module.exit_json(changed=False, result=e.message)
        elif module.params[
                "state"] == "absent" and "does not exist" in e.message:
            module.exit_json(changed=False, result=e.message)
        else:
            module.fail_json(msg=e.message)
    except ReqlAuthError as e:
        module.fail_json(msg=e.message)
    finally:
        conn.close(noreply_wait=False)
Beispiel #18
0
def main():
    argument_spec = dict(
        host=dict(required=True),
        port=dict(required=False, type=int, default=28015),
        user=dict(required=False, default="admin"),
        password=dict(required=False, default="", no_log=True),
        ssl=dict(required=False, type=dict, default=None),
        database=dict(required=False),
        list_databases=dict(required=False, type=bool),
        list_database_config=dict(required=False, type=bool),
        list_tables=dict(required=False, type=bool),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        required_if=(
            ("list_database_config", True, ["database"]),
            ("list_tables", True, ["database"]),
        ),
        mutually_exclusive=[("list_databases", "list_database_config",
                             "list_tables")],
    )

    client = RethinkDB()
    _params = {
        "host": module.params["host"],
        "port": module.params["port"],
        "user": module.params["user"],
        "password": module.params["password"],
        "ssl": module.params["ssl"],
        "db": "rethinkdb",
    }

    try:
        conn = client.connect(**_params)
        if module.params["list_databases"]:
            _res = client.db_list().run(conn)
            module.exit_json(databases=_res)
        elif module.params["list_database_config"]:
            _res = client.db(module.params["database"]).config().run(conn)
            module.exit_json(database_config=_res)
        elif module.params["list_tables"]:
            _res = client.db(module.params["database"]).table_list().run(conn)
            module.exit_json(tables=_res)
        else:
            module.fail_json("unknown options")
    except (ReqlAuthError, ReqlOpFailedError) as e:
        module.fail_json(msg=e.message)
    finally:
        conn.close(noreply_wait=False)
Beispiel #19
0
def searchForUser():
    rethink_db = None  #persistent connections attempt
    print("Enter Username")
    username = input()
    searchby = {'username': username}

    print()
    # Search for users in db (if rethink is down, report a crash)
    try:
        if rethink_db == None:
            rethink_db = RethinkDB()
            rethink_db.connect("433-25.csse.rose-hulman.edu", 28015).repl()
            print('search query:', searchby)
        cur = list(
            rethink_db.db('users').table("usertable").filter(searchby).run())
        if len(cur) > 0:
            for x in cur:
                print(x)
            return
        else:
            print("There are no user with that username in the system")
    except Exception as e:
        print("Cannot currently connect to RethinkDB:")
Beispiel #20
0
            def __enter__(myself):
                r = RethinkDB()
                if not hasattr(myself, 'conn') or not myself.conn  or not myself.conn.is_open():

                    myself.conn = r.connect(self.host, self.port, self.db, user=self.user, password=self.password)
                    self.log.debug(f"Connecting using connection: {myself}")

                if not myself.conn.is_open():
                    raise Exception("Cannot open a new rethinkdb connection...")

                self.log.debug(f"Repl-ing connection: {myself}")
                myself.conn.repl()

                return myself.conn
Beispiel #21
0
def init():
    red = RethinkDB()
    for _ in range(10):
        try:
            red.connect("rethink", 28015, password="").repl()
            red.db_list().run()
            break
        except:
            continue
        time.sleep(2)
    if red is None:
        print("cannot connect to db")
        exit(1)
    else:
        db_list = red.db_list().run()
        if "test" in db_list:
            red.db_drop("test").run()
        for i in dbs:
            if i not in db_list:
                red.db_create(i).run()
            for j in dbs[i]:
                if j not in red.db(i).table_list().run():
                    red.db(i).table_create(j).run()
    return red
Beispiel #22
0
def connect() -> Tuple[RethinkDB, Any]:
    """Return a RethinkDB object + connection.

    You will usually not need to call this function. Rethink:Model use
    this function to internally open and close database connection.
    """
    rdb = RethinkDB()
    connection = rdb.connect(
        host=HOST,
        port=PORT,
        db=DB_NAME,
        user=USER,
        password=PASSWORD,
        timeout=TIMEOUT,
        ssl=SSL,
    )
    return rdb, connection
Beispiel #23
0
class Database(object):
    def __init__(self, url):
        self.r = RethinkDB()
        self.conn = self.r.connect(url, 28015)

    def client(self):
        return self.r.db("olympus")

    def get_table(self, tbl):
        return self.r.db("olympus").table(tbl).run(self.conn)

    def get(self, tbl, key):
        return self.r.db("olympus").table(tbl).get(key).run(self.conn)

    def insert(self, tbl, obj):
        self.r.db("olympus").table(tbl).insert(obj, conflict="replace").run(
            self.conn)
Beispiel #24
0
def check_db():
    """Check if DB_NAME exists, or create it."""
    rdb = RethinkDB()
    conn = rdb.connect(
        host=db.HOST,
        port=db.PORT,
        user=db.USER,
        password=db.PASSWORD,
        ssl=db.SSL,
        timeout=db.TIMEOUT,
    )

    dbs = rdb.db_list().run(conn)
    if db.DB_NAME not in dbs:
        LOG.info("create database %s", db.DB_NAME)
        rdb.db_create(db.DB_NAME).run(conn)

    conn.close()
Beispiel #25
0
class Persistence():
    def __init__(self):
        self.r = RethinkDB()
        self.conn = self.r.connect("localhost", 28015)

    def insert(self, data):
        # add timestamp
        mydata = copy.deepcopy(data)
        mydata["time"] = int(time.time())
        self.last = mydata["time"]
        try:
            self.r.db('makers-covid').table('global').insert(mydata).run(
                self.conn)
        except RqlRuntimeError as err:
            print(err.message)

    def getLast(self):
        res = self.r.db('makers-covid').table('global').filter(
            self.r.row['time'] >= self.last).run(self.conn)
        return res.next()
data array is constructed in the while loop
sent to data parsing where is it mapped to the different sensors
a dictionary constructed
push data sends the dictionary to the table
"""

import serial
import time
from PythonCode.SerialReading.data_to_sent import data_sent
from rethinkdb import RethinkDB
import sys

r = RethinkDB()
try:
    # conn = r.connect("sam.soon.it", 8912).repl()
    conn = r.connect("localhost", 28015).repl()
except:
    print("pr")
    exit(0)
print("Server connected")
ARDUINO_PATH = ""
RESET_TIME = 30
DATA_LEN = 13
SYNC_TIMES = 10
synced = 0
'''
detect the OS
'''
if sys.platform.startswith('linux'):
    print("Linux")
    ARDUINO_PATH = '/dev/ttyACM0'
Beispiel #27
0
from datetime import datetime
import os
from bson import ObjectId
from rethinkdb import RethinkDB
r = RethinkDB()

app = Flask(__name__)
UPLOAD_FOLDER =os.getcwd()+"/uploads"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

client = MongoClient('mongodb+srv://mongodb:[email protected]/house?retryWrites=true&w=majority')
db = client.Api
users = db['user']

#rethinkd config
conn = r.connect('localhost', 28015).repl()

##***************=>Default Route**********************##
@app.route('/')
def index():
    return render_template('index.html')

##***************=>Users Route**********************##
@app.route("/users")
def users():
    try:
        user_arr = list()
        user_out = db['user']
        user_list = user_out.find()
        for user in user_list:
            user_arr.append(user)
Beispiel #28
0
class RethinkDBJobStore(BaseJobStore):
    """
    Stores jobs in a RethinkDB database. Any leftover keyword arguments are directly passed to
    rethinkdb's `RethinkdbClient <http://www.rethinkdb.com/api/#connect>`_.

    Plugin alias: ``rethinkdb``

    :param str database: database to store jobs in
    :param str collection: collection to store jobs in
    :param client: a :class:`rethinkdb.net.Connection` instance to use instead of providing
        connection arguments
    :param int pickle_protocol: pickle protocol level to use (for serialization), defaults to the
        highest available
    """

    def __init__(self, database='apscheduler', table='jobs', client=None,
                 pickle_protocol=pickle.HIGHEST_PROTOCOL, **connect_args):
        super(RethinkDBJobStore, self).__init__()

        if not database:
            raise ValueError('The "database" parameter must not be empty')
        if not table:
            raise ValueError('The "table" parameter must not be empty')

        self.database = database
        self.table_name = table
        self.table = None
        self.client = client
        self.pickle_protocol = pickle_protocol
        self.connect_args = connect_args
        self.r = RethinkDB()
        self.conn = None

    def start(self, scheduler, alias):
        super(RethinkDBJobStore, self).start(scheduler, alias)

        if self.client:
            self.conn = maybe_ref(self.client)
        else:
            self.conn = self.r.connect(db=self.database, **self.connect_args)

        if self.database not in self.r.db_list().run(self.conn):
            self.r.db_create(self.database).run(self.conn)

        if self.table_name not in self.r.table_list().run(self.conn):
            self.r.table_create(self.table_name).run(self.conn)

        if 'next_run_time' not in self.r.table(self.table_name).index_list().run(self.conn):
            self.r.table(self.table_name).index_create('next_run_time').run(self.conn)

        self.table = self.r.db(self.database).table(self.table_name)

    def lookup_job(self, job_id):
        results = list(self.table.get_all(job_id).pluck('job_state').run(self.conn))
        return self._reconstitute_job(results[0]['job_state']) if results else None

    def get_due_jobs(self, now):
        return self._get_jobs(self.r.row['next_run_time'] <= datetime_to_utc_timestamp(now))

    def get_next_run_time(self):
        results = list(
            self.table
            .filter(self.r.row['next_run_time'] != None)  # noqa
            .order_by(self.r.asc('next_run_time'))
            .map(lambda x: x['next_run_time'])
            .limit(1)
            .run(self.conn)
        )
        return utc_timestamp_to_datetime(results[0]) if results else None

    def get_all_jobs(self):
        jobs = self._get_jobs()
        self._fix_paused_jobs_sorting(jobs)
        return jobs

    def add_job(self, job):
        job_dict = {
            'id': job.id,
            'next_run_time': datetime_to_utc_timestamp(job.next_run_time),
            'job_state': self.r.binary(pickle.dumps(job.__getstate__(), self.pickle_protocol))
        }
        results = self.table.insert(job_dict).run(self.conn)
        if results['errors'] > 0:
            raise ConflictingIdError(job.id)

    def update_job(self, job):
        changes = {
            'next_run_time': datetime_to_utc_timestamp(job.next_run_time),
            'job_state': self.r.binary(pickle.dumps(job.__getstate__(), self.pickle_protocol))
        }
        results = self.table.get_all(job.id).update(changes).run(self.conn)
        skipped = False in map(lambda x: results[x] == 0, results.keys())
        if results['skipped'] > 0 or results['errors'] > 0 or not skipped:
            raise JobLookupError(job.id)

    def remove_job(self, job_id):
        results = self.table.get_all(job_id).delete().run(self.conn)
        if results['deleted'] + results['skipped'] != 1:
            raise JobLookupError(job_id)

    def remove_all_jobs(self):
        self.table.delete().run(self.conn)

    def shutdown(self):
        self.conn.close()

    def _reconstitute_job(self, job_state):
        job_state = pickle.loads(job_state)
        job = Job.__new__(Job)
        job.__setstate__(job_state)
        job._scheduler = self._scheduler
        job._jobstore_alias = self._alias
        return job

    def _get_jobs(self, predicate=None):
        jobs = []
        failed_job_ids = []
        query = (self.table.filter(self.r.row['next_run_time'] != None).filter(predicate)  # noqa
                 if predicate else self.table)
        query = query.order_by('next_run_time', 'id').pluck('id', 'job_state')

        for document in query.run(self.conn):
            try:
                jobs.append(self._reconstitute_job(document['job_state']))
            except Exception:
                self._logger.exception('Unable to restore job "%s" -- removing it', document['id'])
                failed_job_ids.append(document['id'])

        # Remove all the jobs we failed to restore
        if failed_job_ids:
            self.r.expr(failed_job_ids).for_each(
                lambda job_id: self.table.get_all(job_id).delete()).run(self.conn)

        return jobs

    def __repr__(self):
        connection = self.conn
        return '<%s (connection=%s)>' % (self.__class__.__name__, connection)
Beispiel #29
0
def main():
    argument_spec = dict(
        host=dict(required=True),
        port=dict(required=False, type=int, default=28015),
        user=dict(required=False, default="admin"),
        password=dict(required=False, default="", no_log=True),
        ssl=dict(required=False, type=dict, default=None),
        command=dict(required=False, choices=["rebalance", "reconfigure"]),
        database=dict(required=True),
        table=dict(required=False),
        shards=dict(required=False, type=int),
        replicas=dict(required=False, type=int),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
    )

    client = RethinkDB()
    _params = {
        "host": module.params["host"],
        "port": module.params["port"],
        "user": module.params["user"],
        "password": module.params["password"],
        "ssl": module.params["ssl"],
        "db": "rethinkdb",
    }

    try:
        conn = client.connect(**_params)

        if module.params["command"].lower() == "rebalance":
            if module.params["table"] is None:
                _res = client.db(module.params["database"]).rebalance().run(conn)
            else:
                _res = (
                    client.db(module.params["database"])
                    .table(module.params["table"])
                    .rebalance()
                    .run(conn)
                )
            if "rebalanced" in _res and _res["rebalanced"] > 0:
                module.exit_json(changed=True, result=_res)
            else:
                module.exit_json(result=_res)
        elif module.params["command"].lower() == "reconfigure":
            if module.params["table"] is None:
                _res = (
                    client.db(module.params["database"])
                    .reconfigure(
                        shards=module.params["shards"],
                        replicas=module.params["replicas"],
                    )
                    .run(conn)
                )
            else:
                _res = (
                    client.db(module.params["database"])
                    .table(module.params["table"])
                    .reconfigure(
                        shards=module.params["shards"],
                        replicas=module.params["replicas"],
                    )
                    .run(conn)
                )
            module.exit_json(changed=True, result=_res)

    except (ReqlAuthError, ReqlOpFailedError) as e:
        module.fail_json(msg=e.message)
    finally:
        conn.close(noreply_wait=False)
Beispiel #30
0
 def connect(self):
     self._session = r.connect(self._host, self._port, self._db,
                               self._username, self._password,
                               self._timeout, self._ssl)
Beispiel #31
0
def experiment_rethinkdb_folded():
    from rethinkdb import RethinkDB
    r = RethinkDB()
    conn = r.connect(db='test')
    r.db_create('test_db').run(conn)
    r.db_list().run(conn)
Beispiel #32
0
def checkLog():
    mongo_db = None
    rethink_db = None

    if red.get(mongoLSN) == None:
        red.set(mongoLSN, 0)  # Latest LSN mongo has successfully run
    if red.get(rethinkLSN) == None:
        red.set(rethinkLSN, 0)  # Latest LSN mongo has successfully run
    if red.get(currLSN) == None:
        red.set(currLSN, 1)  #Highest LSN in the log

    # Loop through
    while True:
        # Try to connect to Rethink if there is no connection
        if rethink_db == None:
            try:
                rethink_db = RethinkDB()
                rethink_db.connect("433-25.csse.rose-hulman.edu", 28015).repl()
            except:
                rethink_db = None
                print("RethinkDB is down")

        # Try to connect to MongoDB if there is no connection
        if mongo_db == None:
            try:
                mongo_db = pymongo.MongoClient(
                    'mongodb://433-27.csse.rose-hulman.edu:40002,433-25.csse.rose-hulman.edu:40000,433-26.csse.rose-hulman.edu:40001,433-28.csse.rose-hulman.edu:40003/moviedb?replicaSet=movieapp'
                )
            except:
                mongo_db = None
                print("MongoDB is down")

        m_LSN = int(red.get(mongoLSN))  # LSN mongo is currently on
        r_LSN = int(red.get(rethinkLSN))  # LSN rethink is currently on
        latest_LSN = int(red.get(currLSN)) - 1  #Highest LSN in the log

        if mongo_db != None:  #rethink_db == None
            #print("RethinkDB only down")
            if m_LSN <= latest_LSN:
                msgs = red.zrangebyscore(log, m_LSN,
                                         m_LSN)  #returns command at lsn
                if len(msgs) == 0:
                    red.incr(mongoLSN, 1)
                    continue

                msg = msgs[0].decode().split(' ')  #should only be one
                print('Mongo Reading LSN', m_LSN, ":", msgs[0])

                #run msg on mongo_db
                rethinkOnlyCommands = [
                    'ADDUSER', 'DELUSER', 'EDITUSER', 'DELMOVIEFROMLIST'
                ]
                if msg[0] == 'DELMOVIE':
                    if len(msg) < 2:
                        print("Bad formatted DELMOVIE log command at lsn",
                              m_LSN)
                    else:
                        res = delmovie(msg, mongo_db)
                        if res == 0:
                            red.incr(mongoLSN, 1)
                        else:
                            mongo_db = None

                        #need to delete from collections in rethinkDB
                elif msg[0] == 'ADDMOVIE':
                    if len(msg) < 11:
                        print("Bad formatted ADDMOVIE log command at lsn",
                              m_LSN)
                    else:
                        res = addmovie(msg, mongo_db)
                        if res == 0:  #success
                            red.incr(mongoLSN, 1)
                        else:
                            mongo_db = None

                elif msg[0] == 'EDITMOVIE':
                    if len(msg) < 14:
                        print("Bad formatted EDITMOVIE log command at lsn",
                              m_LSN)
                    else:
                        res = editmovie(msg, mongo_db)
                        if res == 0:  #success
                            red.incr(mongoLSN, 1)
                        else:
                            mongo_db = None
                        #if mongo errors, res = 1, and loop again

                elif msg[
                        0] == 'ADDMOVIETOLIST':  #Verify movie exists, else remove from log so rethink doesn't run it
                    print(
                        'Mongo will verify movie ID is valid just before adding to user list'
                    )
                    red.incr(mongoLSN, 1)
                elif msg[0] in rethinkOnlyCommands:  #skip and move on
                    print("No operation necessary for MongoDB")
                    red.incr(mongoLSN, 1)
                else:
                    print("Unknown command, skipped lsn", m_LSN)
                    red.incr(mongoLSN, 1)

        if rethink_db != None:
            if r_LSN <= latest_LSN:
                msgs = red.zrangebyscore(log, r_LSN,
                                         r_LSN)  #returns command at lsn
                if len(msgs) == 0:
                    red.incr(rethinkLSN, 1)
                    continue

                msg = msgs[0].decode().split(' ')  #should only be one
                print('Rethink Reading LSN', r_LSN, ":", msgs[0])

                mongoOnlyCommands = ['ADDMOVIE', 'EDITMOVIE']

                #have a wait variable if m_LSN < r_LSN and need mongo to do a validity search (ADDMOVIETOLIST)
                if msg[0] == 'DELUSER':
                    if len(msg) < 2:
                        print("Bad formatted DELUSER log command at lsn",
                              r_LSN)
                    else:
                        res = deluser(msg, rethink_db)
                        if res == 0:
                            red.incr(rethinkLSN, 1)
                        else:
                            rethink_db = None

                elif msg[0] == 'ADDUSER':
                    if len(msg) < 4:
                        print("Bad formatted ADDUSER log command at lsn",
                              r_LSN)
                    else:
                        res = adduser(msg, rethink_db)
                        if res == 0:  #success
                            red.incr(rethinkLSN, 1)
                        else:
                            rethink_db = None

                elif msg[0] == 'EDITUSER':
                    if len(msg) < 4:
                        print("Bad formatted EDITUSER log command at lsn",
                              r_LSN)
                    else:
                        res = edituser(msg, rethink_db)
                        if res == 0:  #success
                            red.incr(rethinkLSN, 1)
                        else:
                            rethink_db = None

                elif msg[0] == 'DELMOVIEFROMLIST':
                    if len(msg) < 3:
                        print(
                            "Bad formatted DELMOVIEFROMLIST log command at lsn",
                            r_LSN)
                    else:
                        res = delMovieFromUserList(msg, rethink_db)
                        if res == 0:  #success
                            red.incr(rethinkLSN, 1)
                        else:
                            rethink_db = None

                elif msg[
                        0] == 'ADDMOVIETOLIST':  #Verify movie exists, else remove from log so rethink doesn't run it
                    if len(msg) < 3:
                        print(
                            "Bad formatted DELMOVIEFROMLIST log command at lsn",
                            r_LSN)
                    else:

                        #search for movie, if it exists, then move on
                        res = verifyMovie(msg, mongo_db)
                        if res == 0:
                            print("Movie ID", msg[2], "exists")
                        elif res == 1:
                            print("Movie ID", msg[2], "does not exist")
                            red.incr(rethinkLSN, 1)  #skip it
                            continue
                        else:  #mongo down - can't connect; loop until get a connection
                            print(
                                "Mongo down - cannot verify movieID is valid; Must wait for mongoDB to verify valid movie ID"
                            )
                            continue

                        #if movie exists, add it to user list
                        res = addMovieToUserList(msg, rethink_db)
                        if res == 0:  #success
                            red.incr(rethinkLSN, 1)
                        else:
                            rethink_db = None

                elif msg[0] == 'DELMOVIE':
                    if len(msg) < 2:
                        print("Bad formatted DELMOVIE log command at lsn",
                              m_LSN)
                    else:
                        res = delMovieIDFromAllLists(msg, rethink_db)
                        if res == 0:
                            red.incr(rethinkLSN, 1)
                        else:
                            mongo_db = None
                elif msg[0] in mongoOnlyCommands:  #skip and move on
                    print("No operation necessary for RethinkDB")
                    red.incr(rethinkLSN, 1)
                else:
                    print("Unknown command, skipped lsn", r_LSN)
                    red.incr(rethinkLSN, 1)

        removeMessages(m_LSN, r_LSN, latest_LSN, red)