Event handling stereotype for Spakky Framework (@EventHandler, @on_event)
Project description
Spakky Event
Spakky Framework를 위한 이벤트 처리 stereotype입니다.
설치
pip install spakky-event
주요 기능
@EventHandler: 이벤트 handler 클래스용 stereotype@on_event: 메서드를 이벤트 handler로 표시하는 decorator- 타입 안전성: 이벤트 타입에 대한 타입 힌트 지원
- 비동기 지원: 이벤트 처리에 async/await 네이티브 지원
빠른 시작
이벤트 정의
이벤트는 spakky-domain의 이벤트 기반 클래스 중 하나를 상속해야 합니다.
- bounded context 내부 이벤트(in-process domain event)에는
AbstractDomainEvent를 사용합니다. - 경계를 넘는 이벤트(message broker integration)에는
AbstractIntegrationEvent를 사용합니다.
메시지 브로커 플러그인(RabbitMQ/Kafka)을 사용할 때는 이벤트를 AbstractIntegrationEvent로 정의하세요.
from spakky.core.common.mutability import immutable
from spakky.domain.models.event import AbstractIntegrationEvent
@immutable
class UserCreatedEvent(AbstractIntegrationEvent):
user_id: str
email: str
@immutable
class UserDeletedEvent(AbstractIntegrationEvent):
user_id: str
EventHandler 생성
@EventHandler stereotype과 @on_event decorator를 함께 사용합니다.
from spakky.event.stereotype.event_handler import EventHandler, on_event
@EventHandler()
class UserEventHandler:
def __init__(self, notification_service: NotificationService) -> None:
self.notification_service = notification_service
@on_event(UserCreatedEvent)
async def on_user_created(self, event: UserCreatedEvent) -> None:
await self.notification_service.send_welcome_email(event.email)
@on_event(UserDeletedEvent)
async def on_user_deleted(self, event: UserDeletedEvent) -> None:
await self.notification_service.send_goodbye_email(event.user_id)
메시지 브로커 통합
@EventHandler stereotype은 Spakky의 message broker 플러그인과 함께 동작합니다.
RabbitMQ
pip install spakky-rabbitmq
from spakky.core.application.application import SpakkyApplication
from spakky.core.application.application_context import ApplicationContext
app = (
SpakkyApplication(ApplicationContext())
.load_plugins() # Loads spakky-rabbitmq plugin
.scan()
.start()
)
Kafka
pip install spakky-kafka
from spakky.core.application.application import SpakkyApplication
from spakky.core.application.application_context import ApplicationContext
app = (
SpakkyApplication(ApplicationContext())
.load_plugins() # Loads spakky-kafka plugin
.scan()
.start()
)
API 레퍼런스
스테레오타입
| 데코레이터 | 설명 |
|---|---|
@EventHandler() |
클래스를 event handler로 표시(@Pod 확장) |
@on_event(EventType) |
메서드를 특정 event type의 handler로 표시 |
인터페이스
| 인터페이스 | 설명 |
|---|---|
IEventPublisher / IAsyncEventPublisher |
Event publish 진입점(type-based routing) |
IEventBus / IAsyncEventBus |
Integration event send 진입점(Outbox 경계) |
IEventTransport / IAsyncEventTransport |
실제 message broker transport |
IEventConsumer / IAsyncEventConsumer |
Handler callback 등록 |
IEventDispatcher / IAsyncEventDispatcher |
In-process handler dispatch |
구현체
| 클래스 | 설명 |
|---|---|
EventMediator / AsyncEventMediator |
Consumer와 Dispatcher를 결합한 in-process 구현체 |
EventPublisher / AsyncEventPublisher |
Type-based router(DomainEvent→Mediator, IntegrationEvent→EventBus) |
DirectEventBus / AsyncDirectEventBus |
기본 EventBus → EventTransport 위임 구현 |
EventHandlerRegistrationPostProcessor |
@EventHandler 메서드를 자동 등록 |
타입
| 타입 | 설명 |
|---|---|
EventRoute |
event routing metadata용 annotation 클래스 |
EventT_contra |
AbstractEvent에 bound된 TypeVar |
EventHandlerCallback |
동기 event callback용 type alias |
AsyncEventHandlerCallback |
비동기 event callback용 type alias |
에러
| 클래스 | 설명 |
|---|---|
AbstractSpakkyEventError |
event operation 기반 error |
InvalidMessageError |
message가 malformed일 때 발생 |
관련 패키지
| 패키지 | 설명 |
|---|---|
spakky-domain |
DDD building block 포함 AbstractEvent, AbstractDomainEvent, AbstractIntegrationEvent |
spakky-rabbitmq |
RabbitMQ transport(RabbitMQEventTransport) |
spakky-kafka |
Kafka transport(KafkaEventTransport) |
In-process 도메인 이벤트 발행
bounded context 내부 이벤트(DomainEvents)에는 in-process publisher를 사용합니다.
from spakky.core.application.application import SpakkyApplication
from spakky.core.application.application_context import ApplicationContext
from spakky.event import IAsyncEventPublisher
# 애플리케이션 부트스트랩(load_plugins가 event component를 자동 등록)
app = (
SpakkyApplication(ApplicationContext())
.load_plugins()
.scan()
.start()
)
# 컨테이너에서 publisher 조회
publisher = app.container.get(IAsyncEventPublisher)
await publisher.publish(UserCreatedEvent(user_id="123", email="test@example.com"))
아키텍처(ISP 준수)
in-process event system은 Interface Segregation Principle을 따릅니다.
- Consumer: event handler 등록(
register()메서드) - Dispatcher: event를 handler로 dispatch(
dispatch()메서드) - Mediator: 두 인터페이스를 단일 구현체로 결합
- Publisher: Consumer가 아니라 Dispatcher에만 의존
flowchart TD
publisher[Publisher]
dispatcher[Dispatcher]
mediator[Mediator<br/>Consumer + Dispatcher]
handler[EventHandler<br/>@on_event]
publisher --> dispatcher
dispatcher --> mediator
mediator --> handler
라이선스
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spakky_event-6.5.0.tar.gz.
File metadata
- Download URL: spakky_event-6.5.0.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfd3d7516754769647b79c181be9ae885a6973eb076fa142e1c626c7e617cf4c
|
|
| MD5 |
7bcc4c2d3e94ecb36a6f161b15cdb607
|
|
| BLAKE2b-256 |
feac466d72ddfad21350a8a88416eab2e7093213577813ca1c358cbffa3e186b
|
Provenance
The following attestation bundles were made for spakky_event-6.5.0.tar.gz:
Publisher:
release.yml on E5presso/spakky-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spakky_event-6.5.0.tar.gz -
Subject digest:
bfd3d7516754769647b79c181be9ae885a6973eb076fa142e1c626c7e617cf4c - Sigstore transparency entry: 1437042333
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@86c1d43bdc948ac432f27efeeeb2b56692d18ee4 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/E5presso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@86c1d43bdc948ac432f27efeeeb2b56692d18ee4 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file spakky_event-6.5.0-py3-none-any.whl.
File metadata
- Download URL: spakky_event-6.5.0-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3641907507d6ce6a6428e9409ea11b6264b40b00a195f2cf8dfac1d5077b2bf
|
|
| MD5 |
ffd01ab672650462fefe02f440162c0b
|
|
| BLAKE2b-256 |
f2a9ce590c1006ad593473a06f64b6174d605db869df0218006c7f0a4920ce13
|
Provenance
The following attestation bundles were made for spakky_event-6.5.0-py3-none-any.whl:
Publisher:
release.yml on E5presso/spakky-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spakky_event-6.5.0-py3-none-any.whl -
Subject digest:
b3641907507d6ce6a6428e9409ea11b6264b40b00a195f2cf8dfac1d5077b2bf - Sigstore transparency entry: 1437042345
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@86c1d43bdc948ac432f27efeeeb2b56692d18ee4 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/E5presso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@86c1d43bdc948ac432f27efeeeb2b56692d18ee4 -
Trigger Event:
workflow_dispatch
-
Statement type: