-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer.py
64 lines (51 loc) · 1.84 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
64
from redis_om import get_redis_connection, HashModel
from dotenv import load_dotenv
import os
import time
load_dotenv()
key = "order_completed"
group = "inventory-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 Product(HashModel):
name: str
price: float
stock: int
class Meta:
database = redis
model_key_prefix = "product"
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]
try:
product = Product.get(obj['product_id'])
print(product)
product.stock -= int(obj['quantity'])
product.save()
print(f"Order {obj['pk']} completed. Updated stock for product {obj['product_id']} to {product.stock}")
except Exception as e:
print(f"Failed to update product stock: {str(e)}")
redis.xadd("refund_order", obj, '*')
except Exception as e:
print(f"Failed to read from Redis group '{group}' for key '{key}': {str(e)}")
def main():
print("Inventory consumer started. Waiting for orders to be completed...")
create_redis_group(key, group)
while True:
read_from_redis_group(group, key)
time.sleep(5)
if __name__ == "__main__":
main()