Skip to content

Commit

Permalink
Fix publishing events when broker is deactivated
Browse files Browse the repository at this point in the history
The default settings are still active at import time. Running
`publish_domain_event` would use the default broker connection settings
even if `DOMAIN_EVENT_BROKER` was set to `None`.

In the transport calls, set the connection settings at runtime and don't
use the configured broker as an argument's default. This also fixes the
type annotations to indicate that `connection_settings` may be `None`.
  • Loading branch information
Pankrat committed Mar 22, 2021
1 parent 7d100dc commit e2f9d1d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [3.0.1]

### Fixed

- Don't connect to default broker when connection settings are set to `None`

## [3.0]

### Added
Expand Down
8 changes: 6 additions & 2 deletions domain_event_broker/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def publish_domain_event(routing_key: str,
domain_object_id: str = None,
uuid_string: Optional[str] = None,
timestamp: Optional[float] = None,
connection_settings: str = settings.BROKER,
connection_settings: Optional[str] = '',
) -> DomainEvent:
"""
Send a domain event to the message broker. The broker will take care of
Expand Down Expand Up @@ -49,6 +49,8 @@ def publish_domain_event(routing_key: str,
uuid_string=uuid_string,
timestamp=timestamp)
json_data = json.dumps(event.event_data)
if connection_settings == '':
connection_settings = settings.BROKER
publisher = Publisher(connection_settings)
publisher.publish(json_data, event.routing_key)
publisher.disconnect()
Expand Down Expand Up @@ -228,10 +230,12 @@ def wrapper(transport: 'Transport', *args: Any, **kwargs: Any) -> Any:
class Transport(object):

def __init__(self,
connection_settings: str = settings.BROKER,
connection_settings: Optional[str] = '',
exchange: str = "domain-events",
exchange_type: str = "topic",
):
if connection_settings == '':
connection_settings = settings.BROKER
self.exchange = exchange
self.exchange_type = exchange_type
self.context_depth = 0
Expand Down

0 comments on commit e2f9d1d

Please sign in to comment.