|
| 1 | +import importlib |
| 2 | +from types import ModuleType |
| 3 | +from typing import Iterable, Optional |
| 4 | + |
| 5 | +from dispatcher import producers |
| 6 | +from dispatcher.brokers.base import BaseBroker |
| 7 | +from dispatcher.config import LazySettings |
| 8 | +from dispatcher.config import settings as global_settings |
| 9 | +from dispatcher.main import DispatcherMain |
| 10 | + |
| 11 | +""" |
| 12 | +Creates objects from settings, |
| 13 | +This is kept separate from the settings and the class definitions themselves, |
| 14 | +which is to avoid import dependencies. |
| 15 | +""" |
| 16 | + |
| 17 | +# ---- Service objects ---- |
| 18 | + |
| 19 | + |
| 20 | +def get_broker_module(broker_name) -> ModuleType: |
| 21 | + "Static method to alias import_module so we use a consistent import path" |
| 22 | + return importlib.import_module(f'dispatcher.brokers.{broker_name}') |
| 23 | + |
| 24 | + |
| 25 | +def get_async_broker(broker_name: str, broker_config: dict, **overrides) -> BaseBroker: |
| 26 | + """ |
| 27 | + Given the name of the broker in the settings, and the data under that entry in settings, |
| 28 | + return the asyncio broker object. |
| 29 | + """ |
| 30 | + broker_module = get_broker_module(broker_name) |
| 31 | + kwargs = broker_config.copy() |
| 32 | + kwargs.update(overrides) |
| 33 | + return broker_module.AsyncBroker(**kwargs) |
| 34 | + |
| 35 | + |
| 36 | +def producers_from_settings(settings: LazySettings = global_settings) -> Iterable[producers.BaseProducer]: |
| 37 | + producer_objects = [] |
| 38 | + for broker_name, broker_kwargs in settings.brokers.items(): |
| 39 | + broker = get_async_broker(broker_name, broker_kwargs) |
| 40 | + producer = producers.BrokeredProducer(broker=broker) |
| 41 | + producer_objects.append(producer) |
| 42 | + |
| 43 | + for producer_cls, producer_kwargs in settings.producers.items(): |
| 44 | + producer_objects.append(getattr(producers, producer_cls)(**producer_kwargs)) |
| 45 | + |
| 46 | + return producer_objects |
| 47 | + |
| 48 | + |
| 49 | +def from_settings(settings: LazySettings = global_settings) -> DispatcherMain: |
| 50 | + """ |
| 51 | + Returns the main dispatcher object, used for running the background task service. |
| 52 | + You could initialize this yourself, but using the shared settings allows for consistency |
| 53 | + between the service, publisher, and any other interacting processes. |
| 54 | + """ |
| 55 | + producers = producers_from_settings(settings=settings) |
| 56 | + return DispatcherMain(settings.service, producers) |
| 57 | + |
| 58 | + |
| 59 | +# ---- Publisher objects ---- |
| 60 | + |
| 61 | + |
| 62 | +def get_sync_broker(broker_name, broker_config) -> BaseBroker: |
| 63 | + """ |
| 64 | + Given the name of the broker in the settings, and the data under that entry in settings, |
| 65 | + return the synchronous broker object. |
| 66 | + """ |
| 67 | + broker_module = get_broker_module(broker_name) |
| 68 | + return broker_module.SyncBroker(**broker_config) |
| 69 | + |
| 70 | + |
| 71 | +def get_sync_publisher_from_settings(publish_broker: Optional[str] = None, settings: LazySettings = global_settings) -> BaseBroker: |
| 72 | + if publish_broker: |
| 73 | + pass |
| 74 | + elif len(settings.brokers) == 1: |
| 75 | + publish_broker = list(settings.brokers.keys())[0] |
| 76 | + elif 'default_broker' in settings.publish: |
| 77 | + publish_broker = settings.publish['default_broker'] |
| 78 | + else: |
| 79 | + raise RuntimeError(f'Could not determine which broker to publish with between options {list(settings.brokers.keys())}') |
| 80 | + |
| 81 | + return get_sync_broker(publish_broker, settings.brokers[publish_broker]) |
| 82 | + |
| 83 | + |
| 84 | +def get_async_publisher_from_settings(settings: LazySettings = global_settings, **overrides) -> BaseBroker: |
| 85 | + """ |
| 86 | + An asynchronous publisher is the ideal choice for submitting control-and-reply actions. |
| 87 | + This returns an asyncio broker of the default publisher type. |
| 88 | +
|
| 89 | + If channels are specified, these completely replace the channel list from settings. |
| 90 | + For control-and-reply, this will contain only the reply_to channel, to not receive |
| 91 | + unrelated traffic. |
| 92 | + """ |
| 93 | + publish_broker = settings.publish['default_broker'] |
| 94 | + return get_async_broker(publish_broker, settings.brokers[publish_broker], **overrides) |
0 commit comments