Skip to content

Commit d8f36aa

Browse files
committed
first cut of message bus [service_talks_to_messagebus]
1 parent 9fe6483 commit d8f36aa

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
21+
} # type: Dict[Type[events.Event], List[Callable]]

src/allocation/service_layer/services.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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+
if TYPE_CHECKING:
9+
from . import unit_of_work
910

1011

1112
class InvalidSku(Exception):
@@ -38,6 +39,5 @@ def allocate(
3839
batchref = product.allocate(line)
3940
uow.commit()
4041
return batchref
41-
except model.OutOfStock:
42-
email.send_mail('stock@made.com', f'Out of stock for {line.sku}')
43-
raise
42+
finally:
43+
messagebus.handle(product.events)

0 commit comments

Comments
 (0)