From 50aa3fa05e8ce6cacfba4807a8eb4fa547bdf683 Mon Sep 17 00:00:00 2001 From: ruinivist Date: Tue, 14 Apr 2026 21:09:39 +0000 Subject: [PATCH] prod msg enq --- TASK.md | 18 +++++++++--------- main.py | 7 +++++++ producer.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/TASK.md b/TASK.md index b9ed6fa..9334046 100644 --- a/TASK.md +++ b/TASK.md @@ -89,18 +89,18 @@ Queue args for `orders.q`: ## Step 3: Producer (20 min) -- [ ] `producer.py` should publish 5 JSON orders to routing key `order.created`. -- [ ] Each message should include: -- [ ] `order_id` -- [ ] `user_id` -- [ ] `amount` -- [ ] `should_fail` (set true for at least 1 order) -- [ ] `retry_count` (start at 0) -- [ ] Mark messages persistent. +- [x] `producer.py` should publish 5 JSON orders to routing key `order.created`. +- [x] Each message should include: +- [x] `order_id` +- [x] `user_id` +- [x] `amount` +- [x] `should_fail` (set true for at least 1 order) +- [x] `retry_count` (start at 0) +- [x] Mark messages persistent. Success check: -- [ ] Running `uv run producer.py` prints “Published order …” 5 times. +- [x] Running `uv run producer.py` prints “Published order …” 5 times. ## Step 4: Worker (35 min) diff --git a/main.py b/main.py index 20ac928..1d054c7 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,13 @@ channel = connection.channel() # this is A DECLARE, basically so can run multuple times # .x = exchange + +# what does durable mean? +# when broke restarts +# even if I don't run this IaC like code to create the q +# the q will exist +# it basically commits the q defns to disk +# not the message channel.exchange_declare( exchange="orders.x", exchange_type="direct", durable=True) # .dlx = dead letter exchange for failed messages diff --git a/producer.py b/producer.py index e69de29..21703e4 100644 --- a/producer.py +++ b/producer.py @@ -0,0 +1,38 @@ +import pika +import json + +conn = pika.BlockingConnection(pika.ConnectionParameters("localhost")) +ch = conn.channel() + +# can redeclare qs, but since they are in main no need to +# the op would've been idempotent if I did though + +# random objets to be conv to json +orders = [ + {"order_id": 1001, "user_id": "u1", "amount": 49.99, + "should_fail": False, "retry_count": 0}, + {"order_id": 1002, "user_id": "u2", "amount": 19.50, + "should_fail": False, "retry_count": 0}, + {"order_id": 1003, "user_id": "u3", "amount": 99.00, + "should_fail": True, "retry_count": 0}, + {"order_id": 1004, "user_id": "u4", "amount": 10.00, + "should_fail": False, "retry_count": 0}, + {"order_id": 1005, "user_id": "u5", "amount": 75.25, + "should_fail": False, "retry_count": 0}, +] + +for order in orders: + body = json.dumps(order) + ch.basic_publish( + exchange="orders.x", + routing_key="order.created", # as in the bindings + body=body, + properties=pika.BasicProperties( + delivery_mode=2, # persistent msg, committed to disk before ack + content_type="application/json" + ) + ) + + print(f"published order {order['order_id']}") + +conn.close()