-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer.py
63 lines (48 loc) · 1.49 KB
/
consumer.py
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
from redis_om import get_redis_connection, HashModel
from dotenv import load_dotenv
import os
import time
load_dotenv()
key = "refund_order"
group = "payment-group"
redis = get_redis_connection(
host=os.getenv("REDIS_HOST", "localhost"),
port=int(os.getenv("REDIS_PORT", 6379)),
db=int(os.getenv("REDIS_DB", 0)),
decode_responses=True
)
class Order(HashModel):
product_id: str
quantity: int
price: float
fee: float
total: float
status: str
class Meta:
database = redis
model_key_prefix = "order"
def create_redis_group(key, group):
try:
redis.xgroup_create(key, group, mkstream=True)
print(f"Redis group '{group}' created for key '{key}'")
except Exception as e:
print(f"Failed to create Redis group '{group}' for key '{key}': {str(e)}")
def read_from_redis_group(group, key):
try:
results = redis.xreadgroup(group, key, {key: ">"}, None)
if results != []:
for result in results:
obj = result[1][0][1]
order = Order.get(obj['pk'])
order.status = "refunded"
order.save()
print(f"Order {obj['pk']} refunded.")
except Exception as e:
print(f"Failed to read from Redis group '{group}' for key '{key}': {str(e)}")
def main():
create_redis_group(key, group)
while True:
read_from_redis_group(group, key)
time.sleep(5)
if __name__ == "__main__":
main()