Skip to content

Commit

Permalink
defer event bus initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
keotl committed May 28, 2023
1 parent 5a5eb69 commit 92135b8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
8 changes: 5 additions & 3 deletions jivago/config/production_jivago_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ def configure_service_locator(self):
self.serviceLocator.bind(PartialContentHandler, PartialContentHandler)
self.serviceLocator.bind(HttpStatusCodeResolver, HttpStatusCodeResolver)
self.serviceLocator.bind(ObjectMapper, ObjectMapper)
self.serviceLocator.bind(EventBus, self.create_event_bus())
self.serviceLocator.bind(SynchronousEventBus, self.serviceLocator.get(EventBus))
self.serviceLocator.bind(AsyncEventBus, AsyncEventBus(self.serviceLocator.get(EventBus)))
eventBusScope = SingletonScopeCache("_JivagoDependenciesSingleton", [EventBus, SynchronousEventBus, AsyncEventBus])
self.serviceLocator.register_scope(eventBusScope)
self.serviceLocator.bind(EventBus, self.create_event_bus)
self.serviceLocator.bind(SynchronousEventBus, SynchronousEventBus)
self.serviceLocator.bind(AsyncEventBus, AsyncEventBus)
self.serviceLocator.bind(RequestScopeCache, request_scope_cache)

if not self.banner:
Expand Down
3 changes: 2 additions & 1 deletion jivago/event/async_event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from jivago.event.event_bus import EventBus
from jivago.event.synchronous_event_bus import SynchronousEventBus
from jivago.lang.annotations import Override
from jivago.lang.annotations import Inject, Override


class AsyncEventBus(EventBus):

@Inject
def __init__(self, event_bus: SynchronousEventBus, pool_size=2):
self.event_bus = event_bus
self.thread_pool = ThreadPoolExecutor(max_workers=pool_size)
Expand Down
3 changes: 2 additions & 1 deletion jivago/event/synchronous_event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

from jivago.event.dispatch.message_dispatcher import MessageDispatcher
from jivago.event.event_bus import EventBus
from jivago.lang.annotations import Override
from jivago.lang.annotations import Inject, Override
from jivago.lang.stream import Stream


class SynchronousEventBus(EventBus):
LOGGER = logging.getLogger("EventBus")

@Inject
def __init__(self, message_handlers: List[MessageDispatcher]):
self.message_handlers = message_handlers

Expand Down

0 comments on commit 92135b8

Please sign in to comment.