diff options
Diffstat (limited to 'gunicorn_config.py')
-rw-r--r-- | gunicorn_config.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/gunicorn_config.py b/gunicorn_config.py new file mode 100644 index 00000000..34d9fa55 --- /dev/null +++ b/gunicorn_config.py @@ -0,0 +1,46 @@ +def when_ready(server): + """ server hook that only runs when the gunicorn master process loads """ + + import os + import traceback + import rethinkdb as r + + try: + server.log.info("rethinkdb initialising") + + DB_HOST = os.environ.get("RETHINKDB_HOST") + DB_PORT = os.environ.get("RETHINKDB_PORT") + DB_DATABASE = os.environ.get("RETHINKDB_DATABASE") + DB_TABLE = os.environ.get("RETHINKDB_TABLE") + indexes = ['test'] + + conn = r.connect(host=DB_HOST, port=DB_PORT, db=DB_DATABASE) + + # Check if database exists, if not create it + db_exists = r.db_list().contains(DB_DATABASE).run(conn) + + if not db_exists: + server.log.info('adding database {0}'.format(DB_DATABASE)) + r.db_create(DB_DATABASE).run(conn) + + # Check if table exist, if not create it + table_exists = r.db(DB_DATABASE).table_list().contains(DB_TABLE).run(conn) + + if not table_exists: + server.log.info('adding table {0}'.format(DB_TABLE)) + r.db(DB_DATABASE).table_create(DB_TABLE).run(conn) + + # Check if index exists if not add it + rtable = r.db(DB_DATABASE).table(DB_TABLE) + current_indexes = rtable.index_list().run(conn) + + for index in indexes: + if index not in current_indexes: + server.log.info('adding index {0}'.format(index)) + rtable.index_create(index).run(conn) + + server.log.info("rethinkdb ready") + + except Exception as e: + server.log.error(traceback.format_exc()) + server.log.error("rethinkdb failed to initialise") |