From 8436882783fb2fed76d9a2bde86e66fbe30ec30c Mon Sep 17 00:00:00 2001 From: Christopher Baklid Date: Tue, 6 Feb 2018 22:32:22 +0100 Subject: adds gunicorn server_ready hook to set up database --- gunicorn_config.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 gunicorn_config.py (limited to 'gunicorn_config.py') diff --git a/gunicorn_config.py b/gunicorn_config.py new file mode 100644 index 00000000..c4869dd3 --- /dev/null +++ b/gunicorn_config.py @@ -0,0 +1,44 @@ +def when_ready(server): + """ server hook that only runs when the gunicorn master process loads """ + + import traceback + import rethinkdb as r + + try: + server.log.info("rethinkdb initialising") + + construct = [ + {'DB':'test', 'TABLE':'test', 'INDEXES': ['test']}, + ] + for struct in construct: + DB = struct['DB'] + TABLE = struct['TABLE'] + INDEXES = struct['INDEXES'] + + conn = r.connect(host='pdrdb', port=28016, db=DB) + + # Check if database exists, if not create it + db_exists = r.db_list().contains(DB).run(conn) + if not db_exists: + log.info('adding database {0}'.format(DB)) + r.db_create(DB).run(conn) + + # Check if table exist, if not create it + table_exists = r.db(DB).table_list().contains(TABLE).run(conn) + if not table_exists: + log.info('adding table {0}'.format(TABLE)) + result = r.db(DB).table_create(TABLE).run(conn) + + # Check if index exists if not add it + rtable = r.db(DB).table(TABLE) + current_indexes = rtable.index_list().run(conn) + for index in INDEXES: + if index not in current_indexes: + log.info('adding index {0}'.format(index)) + rtable.index_create(index).run(conn) + + server.log.info("rethinkdb ready") + + except: + server.log.error(traceback.format_exc()) + server.log.error("rethinkdb failed to initialise") -- cgit v1.2.3 From a38d5503f52fdef6b3f910e85f70a5b999fd04b8 Mon Sep 17 00:00:00 2001 From: Christopher Baklid Date: Tue, 6 Feb 2018 23:32:19 +0100 Subject: minor fixes --- gunicorn_config.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'gunicorn_config.py') diff --git a/gunicorn_config.py b/gunicorn_config.py index c4869dd3..a2e6ac23 100644 --- a/gunicorn_config.py +++ b/gunicorn_config.py @@ -8,25 +8,25 @@ def when_ready(server): server.log.info("rethinkdb initialising") construct = [ - {'DB':'test', 'TABLE':'test', 'INDEXES': ['test']}, + {'DB': 'test', 'TABLE': 'test', 'INDEXES': ['test']}, ] for struct in construct: DB = struct['DB'] TABLE = struct['TABLE'] INDEXES = struct['INDEXES'] - conn = r.connect(host='pdrdb', port=28016, db=DB) + conn = r.connect(host='pdrdb', port=28016, db=DB) # Check if database exists, if not create it db_exists = r.db_list().contains(DB).run(conn) if not db_exists: - log.info('adding database {0}'.format(DB)) + server.log.info('adding database {0}'.format(DB)) r.db_create(DB).run(conn) # Check if table exist, if not create it table_exists = r.db(DB).table_list().contains(TABLE).run(conn) if not table_exists: - log.info('adding table {0}'.format(TABLE)) + server.log.info('adding table {0}'.format(TABLE)) result = r.db(DB).table_create(TABLE).run(conn) # Check if index exists if not add it @@ -34,11 +34,11 @@ def when_ready(server): current_indexes = rtable.index_list().run(conn) for index in INDEXES: if index not in current_indexes: - log.info('adding index {0}'.format(index)) + server.log.info('adding index {0}'.format(index)) rtable.index_create(index).run(conn) server.log.info("rethinkdb ready") - except: + except Exception as e: server.log.error(traceback.format_exc()) server.log.error("rethinkdb failed to initialise") -- cgit v1.2.3 From 95542464d4bf7b4405179d14adc0a61f5cc26bae Mon Sep 17 00:00:00 2001 From: Christopher Baklid Date: Tue, 6 Feb 2018 23:34:59 +0100 Subject: last fix --- gunicorn_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gunicorn_config.py') diff --git a/gunicorn_config.py b/gunicorn_config.py index a2e6ac23..2b5c52ea 100644 --- a/gunicorn_config.py +++ b/gunicorn_config.py @@ -27,7 +27,7 @@ def when_ready(server): table_exists = r.db(DB).table_list().contains(TABLE).run(conn) if not table_exists: server.log.info('adding table {0}'.format(TABLE)) - result = r.db(DB).table_create(TABLE).run(conn) + r.db(DB).table_create(TABLE).run(conn) # Check if index exists if not add it rtable = r.db(DB).table(TABLE) -- cgit v1.2.3 From 9e2b4956598d08676c49315550b56a0ae19b680a Mon Sep 17 00:00:00 2001 From: Christopher Baklid Date: Wed, 7 Feb 2018 22:06:31 +0100 Subject: update gunicorn_config (#2) * update gunicorn_config this changes the initialisation step of the database to use the environment variables --- gunicorn_config.py | 60 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 29 deletions(-) (limited to 'gunicorn_config.py') diff --git a/gunicorn_config.py b/gunicorn_config.py index 2b5c52ea..34d9fa55 100644 --- a/gunicorn_config.py +++ b/gunicorn_config.py @@ -1,41 +1,43 @@ 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") - construct = [ - {'DB': 'test', 'TABLE': 'test', 'INDEXES': ['test']}, - ] - for struct in construct: - DB = struct['DB'] - TABLE = struct['TABLE'] - INDEXES = struct['INDEXES'] - - conn = r.connect(host='pdrdb', port=28016, db=DB) - - # Check if database exists, if not create it - db_exists = r.db_list().contains(DB).run(conn) - if not db_exists: - server.log.info('adding database {0}'.format(DB)) - r.db_create(DB).run(conn) - - # Check if table exist, if not create it - table_exists = r.db(DB).table_list().contains(TABLE).run(conn) - if not table_exists: - server.log.info('adding table {0}'.format(TABLE)) - r.db(DB).table_create(TABLE).run(conn) - - # Check if index exists if not add it - rtable = r.db(DB).table(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) + 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") -- cgit v1.2.3