forked from scalablescripts/python-microservices
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.py
More file actions
45 lines (30 loc) · 1.13 KB
/
Copy pathconsumer.py
File metadata and controls
45 lines (30 loc) · 1.13 KB
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
import pika, json
from main import Product, db
params = pika.URLParameters('your_rabbitmq_url')
connection = pika.BlockingConnection(params)
channel = connection.channel()
channel.queue_declare(queue='main')
def callback(ch, method, properties, body):
print('Received in main')
data = json.loads(body)
print(data)
if properties.content_type == 'product_created':
product = Product(id=data['id'], title=data['title'], image=data['image'])
db.session.add(product)
db.session.commit()
print('Product Created')
elif properties.content_type == 'product_updated':
product = Product.query.get(data['id'])
product.title = data['title']
product.image = data['image']
db.session.commit()
print('Product Updated')
elif properties.content_type == 'product_deleted':
product = Product.query.get(data)
db.session.delete(product)
db.session.commit()
print('Product Deleted')
channel.basic_consume(queue='main', on_message_callback=callback, auto_ack=True)
print('Started Consuming')
channel.start_consuming()
channel.close()