-
-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from onefinestay/rework_standalone_event_disp…
…acher standalone event dispatcher to connect on demand
- Loading branch information
Showing
8 changed files
with
42 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,41 @@ | ||
from contextlib import contextmanager | ||
|
||
from kombu.common import maybe_declare | ||
from kombu.pools import producers, connections | ||
from kombu import Connection | ||
|
||
from nameko.constants import DEFAULT_RETRY_POLICY | ||
from nameko.events import get_event_exchange | ||
from nameko.events import get_event_exchange, Event | ||
from nameko.messaging import AMQP_URI_CONFIG_KEY | ||
|
||
|
||
@contextmanager | ||
def event_dispatcher(container_service_name, nameko_config, **kwargs): | ||
""" Yield a function that dispatches events claiming to originate from | ||
def event_dispatcher(nameko_config, **kwargs): | ||
""" Returns a function that dispatches events claiming to originate from | ||
a service called `container_service_name`. | ||
Enables services not hosted by nameko to dispatch events into a nameko | ||
cluster. | ||
""" | ||
conn = Connection(nameko_config[AMQP_URI_CONFIG_KEY]) | ||
exchange = get_event_exchange(container_service_name) | ||
|
||
kwargs = kwargs.copy() | ||
retry = kwargs.pop('retry', True) | ||
retry_policy = kwargs.pop('retry_policy', DEFAULT_RETRY_POLICY) | ||
|
||
with connections[conn].acquire(block=True) as connection: | ||
maybe_declare(exchange, connection) | ||
def dispatch(service_name, event_type, event_data): | ||
conn = Connection(nameko_config[AMQP_URI_CONFIG_KEY]) | ||
|
||
with producers[conn].acquire(block=True) as producer: | ||
exchange = get_event_exchange(service_name) | ||
if isinstance(event_type, type) and issubclass(event_type, Event): | ||
event_type = event_type.type | ||
|
||
def dispatch(evt): | ||
msg = evt.data | ||
routing_key = evt.type | ||
with connections[conn].acquire(block=True) as connection: | ||
maybe_declare(exchange, connection) | ||
with producers[conn].acquire(block=True) as producer: | ||
msg = event_data | ||
routing_key = event_type | ||
producer.publish( | ||
msg, exchange=exchange, routing_key=routing_key, | ||
retry=retry, retry_policy=retry_policy, **kwargs) | ||
|
||
yield dispatch | ||
msg, | ||
exchange=exchange, | ||
routing_key=routing_key, | ||
retry=retry, | ||
retry_policy=retry_policy, | ||
**kwargs) | ||
return dispatch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters