def __init__( self, context: str, factory: IContextFactory, close_loop: bool = True, retries: int = 3, ): if retries <= 0: raise ValueError("retries should be positive integer") self.context = context self.factory = factory self.close_loop = close_loop self.retries = retries Contextualized.set_default_context(context) self.registry = Registry() self.trace_segment_store = self.factory.create_trace_segment_store() self.domain_event_bus = Bus[DomainEvent]() self.projection_bus = Bus[DomainEvent]() self.resolver_bus = Bus[ApplicationMessage]() self.handler_bus = Bus[ApplicationMessage]() if self.close_loop: # Self publish outgoing domain events to # all application services self.domain_event_bus.attach(ApplicationServiceSubscriber(self)) else: # Self publish outgoing domain events to # resolver services self.domain_event_bus.attach(BusSubscriber(self.resolver_bus))
def trace( self, message: typing.Union[ApplicationCommand, ApplicationQuery, IntegrationEvent], ) -> None: if message.__trace_id__ is None: raise DefinitionError("__trace_id__ should not be NoneType") Traceable.set_default_trace_id(message.__trace_id__) self.trace_store.start_trace(message) self.handle(message) if self.sync: # Publish in current thread async integration events # coming from other contexts integration_bus = Bus[ApplicationMessage]() integration_bus.attach(ApplicationServiceSubscriber(self)) self.trace_store.watch_trace_resolution( message.__trace_id__, integration_bus=integration_bus, timeout_ms=self.sync_timeout_ms, backoff_ms=self.sync_backoff_ms, )
def add_handler(self, handler: ApplicationService) -> None: self.handler_bus.attach(ApplicationServiceSubscriber(handler))
def add_resolver(self, resolver: ApplicationService) -> None: self.resolver_bus.attach(ApplicationServiceSubscriber(resolver))