-
Notifications
You must be signed in to change notification settings - Fork 0
/
redisResetLoad.py3
184 lines (147 loc) · 4.74 KB
/
redisResetLoad.py3
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# -*- coding: utf-8 -*-
import psycopg2, psycopg2.extras
import select
import redis
import json
from settings import *
def postgresConnection():
try:
conn = psycopg2.connect(
database=PG_DB_NAME,
user=PG_USER,
password=PG_PASS,
host=PG_HOST,
port=PG_PORT
)
curs = conn.cursor()
except (Exception,psycopg2.DatabaseError) as e:
logger.error("Postgres connection is not ready yet. Error: " + str(e))
return [conn, curs]
def redisConnection():
try:
pool = redis.ConnectionPool(
host=R_HOST,
port=R_PORT,
db=R_DB
)
r = redis.Redis(connection_pool=pool)
except Exception as e:
logger.error("Redis connection is not ready yet. Error: " + str(e))
return r
def listen(pgcon, pgcur, channel):
try:
pgcon.poll()
notify = pgcon.notifies.pop()
notify_args = {}
notify_args['pid'] = notify.pid
notify_args['channel'] = notify.channel
notify_args['payload'] = json.loads(notify.payload)
logger.info("NOTIFY received:" +json.dumps(notify_args))
return notify.payload
except Exception as e:
logger.error("NOTIFY by the channel: " +channel+" not received. Error: " + str(e))
return None
def loadRedisBrands(r,pgcon,pgcur):
try:
pgcur.execute("BEGIN")
pgcur.execute("LISTEN notify_channel_tbl_brands")
pgcur.callproc("core_schema.udf_load_redis_brand")
nrows = pgcur.fetchone()
pgcur.execute("COMMIT")
logger.info("Executed successfully udf_load_redis_brand")
if (nrows[0] != None):
current_row = 0
while(current_row != nrows[0]):
payload = listen(pgcon,pgcur,'notify_channel_tbl_brands')
if (payload is not None):
json_response = json.loads(payload)
key = json_response['redisHash']
valuesDict = {}
valuesDict['brand_id'] = str(json_response['brand_id'])
valuesDict['name'] = json_response['redisHash']
# Send values to Redis
r.hmset(key,valuesDict)
logger.info("Values sent to redis successfully: "+key+","+json.dumps(valuesDict))
current_row+=1
return True
else:
return False
except Exception as e:
logger.error("Fail in loadRedisBrands. Error: "+ str(e))
return False
def loadRedisCategories(r,pgcon,pgcur):
try:
pgcur.execute("BEGIN")
pgcur.execute("LISTEN notify_channel_tbl_categories")
pgcur.callproc("core_schema.udf_load_redis_categories")
nrows = pgcur.fetchone()
pgcur.execute("COMMIT")
logger.info("Executed successfully udf_load_redis_categories")
if (nrows[0] != None):
current_row = 0
while(current_row != nrows[0]):
payload = listen(pgcon,pgcur,'notify_channel_tbl_categories')
if (payload is not None):
json_response = json.loads(payload)
key = json_response['redisHash']
valuesDict = {}
valuesDict['category_id'] = str(json_response['category_id'])
valuesDict['name'] = json_response['redisHash']
# Send values to Redis
r.hmset(key,valuesDict)
logger.info("Values sent to redis successfully: "+key+","+json.dumps(valuesDict))
current_row+=1
return True
else:
return False
except Exception as e:
logger.error("Fail in loadRedisCategories. Error: "+ str(e))
return False
def loadRedisBrandsCategories(r,pgcon,pgcur):
try:
pgcur.execute("BEGIN")
pgcur.execute("LISTEN notify_channel_tbl_brands_categories")
pgcur.callproc("core_schema.udf_load_redis_brands_categories")
nrows = pgcur.fetchone()
pgcur.execute("COMMIT")
logger.info("Executed successfully udf_load_redis_brands_categories")
if (nrows[0] != None):
current_row = 0
while(current_row != nrows[0]):
payload = listen(pgcon,pgcur,'notify_channel_tbl_brands_categories')
if (payload is not None):
json_response = json.loads(payload)
key = json_response['redisHash']
valuesDict = {}
valuesDict['brands_categories_id'] = str(json_response['brands_categories_id'])
valuesDict['brand_id'] = str(json_response['brand_id'])
valuesDict['categories_id'] = str(json_response['categories_id'])
# Send values to Redis
r.hmset(key,valuesDict)
logger.info("Values sent to redis successfully: "+key+","+json.dumps(valuesDict))
current_row+=1
return True
else:
return False
except Exception as e:
logger.error("Fail in loadRedisBrandsCategories. Error: "+ str(e))
return False
def redisResetLoad(r,pgcon,pgcur):
try:
r.flushdb()
if (loadRedisBrands(r,pgcon,pgcur) == True
and loadRedisCategories(r,pgcon,pgcur) == True
and loadRedisBrandsCategories(r,pgcon,pgcur) == True):
print("Redis has been populated successfully")
else:
print("Error: Redis hasn't been populated")
except Exception as e:
logger.error("Fail in redisResetLoad. Error: "+ str(e))
def main():
r = redisConnection()
pgcon, pgcur = postgresConnection()
redisResetLoad(r,pgcon,pgcur)
pgcon.close()
pgcur.close()
if __name__ == '__main__':
main()