Skip to content

Commit 833c3e7

Browse files
committed
first cut of message bus [service_talks_to_messagebus]
1 parent ba56cc2 commit 833c3e7

File tree

2 files changed

+27
-6
lines changed

2 files changed

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

src/allocation/service_layer/services.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from __future__ import annotations
2-
from typing import Optional
2+
from typing import Optional, TYPE_CHECKING
33
from datetime import date
44

5-
from allocation.adapters import email
65
from allocation.domain import model
76
from allocation.domain.model import OrderLine
8-
from allocation.service_layer import unit_of_work
7+
from . import messagebus
8+
9+
if TYPE_CHECKING:
10+
from . import unit_of_work
911

1012

1113
class InvalidSku(Exception):
@@ -38,6 +40,5 @@ def allocate(
3840
batchref = product.allocate(line)
3941
uow.commit()
4042
return batchref
41-
except model.OutOfStock:
42-
email.send_mail("stock@made.com", f"Out of stock for {line.sku}")
43-
raise
43+
finally:
44+
messagebus.handle(product.events)

0 commit comments

Comments
 (0)