Skip to content
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

Fix publisher #14

Merged
merged 1 commit into from
Nov 15, 2024
Merged
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
2 changes: 1 addition & 1 deletion pybondi/messagebus.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ def consume(self, event: Event):
try:
consumer(event)
except Exception as exception:
logger.error(f"Error while consuming event {event}")
logger.error(f"Error {exception} while consuming event {event}")
logger.debug(exception, exc_info=True)
17 changes: 8 additions & 9 deletions pybondi/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@ class Base(ABC):
An abstract base class for a publisher.

A publisher is responsible for publishing messages to external systems
using callables as subscribers. It should implement the methods to
mantain transacional consistency and resource management using the
begin, commit, rollback and close methods.
using callable subscribers. It should implement the methods to mantain
transacional consistency and resource management using the begin, commit,
rollback and close methods.


The difference between a publisher and a messagebus is that a publisher
is responsible for publish data from the inside of the bounded context
to outside systems. A message bus is responsible for routing events and
commands within the bounded context, and handle them inside a transaction.

Messages passed to the publisher should be serializable objects and
inmutable.
Messages passed to the publisher should be serializable objects.
"""

@abstractmethod
def handle(self,topic: str, message: Any) -> None:
def publish(self, topic: str, message: Any) -> None:
"""
Receives a message from the publisher.
Publishes a message to a topic.

Args:
message: The message to be received.
message: The message to be published.
"""

@abstractmethod
Expand Down Expand Up @@ -75,7 +74,7 @@ def subscribe(self, topic: str, subscriber: Callable):
'''
self.subscribers.setdefault(topic, []).append(subscriber)

def handle(self, topic: str, message: Any):
def publish(self, topic: str, message: Any):
'''
Receives a message from the publisher.
'''
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pybondi"
version = "0.3.0"
version = "0.3.1"
description = "A lightweight library for creating event driven systems using domain driven design."
authors = ["Eric Cardozo <[email protected]>"]
license = "MIT"
Expand Down
8 changes: 4 additions & 4 deletions tests/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_subscribe_and_publish_message(publisher: Publisher):
subscriber = Mock()
publisher.subscribe("topic1", subscriber)

publisher.handle("topic1", "test message")
publisher.publish("topic1", "test message")
publisher.commit()
subscriber.assert_called_once_with("test message")

Expand All @@ -19,15 +19,15 @@ def test_multiple_subscribers_on_same_topic(publisher: Publisher):
subscriber2 = Mock()
publisher.subscribe("topic1", subscriber1)
publisher.subscribe("topic1", subscriber2)
publisher.handle("topic1", "test message")
publisher.publish("topic1", "test message")
publisher.commit()

subscriber1.assert_called_once_with("test message")
subscriber2.assert_called_once_with("test message")

def test_no_subscribers_no_errors(publisher: Publisher):
# Handle a message with no subscribers
publisher.handle("topic1", "test message")
publisher.publish("topic1", "test message")
try:
publisher.commit()
except Exception as e:
Expand All @@ -37,7 +37,7 @@ def test_rollback_message(publisher: Publisher):
subscriber = Mock()
publisher.subscribe("topic1", subscriber)

publisher.handle("topic1", "test message")
publisher.publish("topic1", "test message")
publisher.rollback()

publisher.commit()
Expand Down