Skip to content

Commit c035987

Browse files
committed
first cut of message bus [service_talks_to_messagebus]
1 parent db5bcd6 commit c035987

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/allocation/messagebus.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List, Dict, Callable, Type
2+
from allocation import email, events
3+
4+
5+
def handle(events_: List[events.Event]):
6+
while events_:
7+
event = events_.pop(0)
8+
for handler in HANDLERS[type(event)]:
9+
handler(event)
10+
11+
12+
def send_out_of_stock_notification(event: events.OutOfStock):
13+
email.send_mail(
14+
'stock@made.com',
15+
f'Out of stock for {event.sku}',
16+
)
17+
18+
19+
HANDLERS = {
20+
events.OutOfStock: [send_out_of_stock_notification],
21+
22+
} # type: Dict[Type[events.Event], List[Callable]]
23+

src/allocation/services.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Optional
33
from datetime import date
44

5-
from allocation import email, exceptions, model, unit_of_work
5+
from allocation import exceptions, messagebus, model, unit_of_work
66
from allocation.model import OrderLine
77

88

@@ -32,6 +32,5 @@ def allocate(
3232
batchref = product.allocate(line)
3333
uow.commit()
3434
return batchref
35-
except exceptions.OutOfStock:
36-
email.send_mail('stock@made.com', f'Out of stock for {line.sku}')
37-
raise
35+
finally:
36+
messagebus.handle(product.events)

0 commit comments

Comments
 (0)