blob: a2e6ac23a6654e49c818ad2692eaf904eaf47df2 (
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
|
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:
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))
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:
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")
|