Skip to content

♻️ Refactor EventBus notify method #3690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions acapy_agent/core/event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,39 @@ async def notify(self, profile: "Profile", event: Event):
event (Event): event to emit

"""
# TODO don't block notifier until subscribers have all been called?
# TODO trigger each processor but don't await?
# TODO log errors but otherwise ignore?

LOGGER.debug("Notifying subscribers: %s", event)

partials = []
for pattern, subscribers in self.topic_patterns_to_subscribers.items():
match = pattern.match(event.topic)

if not match:
continue

for subscriber in subscribers:
partials.append(
partial(
subscriber,
profile,
event.with_metadata(EventMetadata(pattern, match)),
)
)
partials = [
partial(
subscriber,
profile,
event.with_metadata(EventMetadata(pattern, match)),
)
for pattern, subscribers in self.topic_patterns_to_subscribers.items()
if (match := pattern.match(event.topic))
for subscriber in subscribers
]

for processor in partials:
if not partials:
LOGGER.info("No subscribers for %s event", event.topic)
return

LOGGER.debug("Notifying %d subscribers for %s event", len(partials), event.topic)
for coro in partials:
try:
await processor()
task = asyncio.create_task(coro)
task.add_done_callback(self._log_task_exception)
except Exception:
LOGGER.exception("Error occurred while processing event")
LOGGER.exception(
"Error occurred while scheduling %s event notification: %s",
event.topic,
coro,
)

def _log_task_exception(self, task: asyncio.Task):
"""Log exceptions in background notification tasks."""
try:
task.result()
except Exception:
LOGGER.exception("Exception in notification background task")

def subscribe(self, pattern: Pattern, processor: Callable):
"""Subscribe to an event.
Expand Down
2 changes: 2 additions & 0 deletions acapy_agent/core/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ async def notify(self, topic: str, payload: Any):
event_bus = self.inject_or(EventBus)
if event_bus:
await event_bus.notify(self, Event(topic, payload))
else:
LOGGER.warning("No event bus found for profile %s", self.name)

def __repr__(self) -> str:
"""Get a human readable string."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ async def emit_event(self, session: ProfileSession, payload: Optional[Any] = Non
payload = V20CredExRecordWebhook(**payload)
payload = payload.__dict__

await session.profile.notify(topic, payload)
await session.emit_event(topic, payload)

@property
def record_value(self) -> Mapping:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ async def emit_event(self, session: ProfileSession, payload: Optional[Any] = Non
payload = V20PresExRecordWebhook(**payload)
payload = payload.__dict__

await session.profile.notify(topic, payload)
await session.emit_event(topic, payload)

@property
def record_value(self) -> Mapping:
Expand Down
Loading