blob: 34d9fa5506fee62fa1b4032e60822ebd81f3ec99 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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")
|