Пример #1
0
    def open(self, consumer_name: str) -> None:
        # TODO: Build then refactor with receive
        # Find the consumer
        if consumer_name not in self._consumers:
            raise ConfigurationException(
                "The consumer {} could not be found, did you register it?".
                format(consumer_name))

        consumer = self._consumers[consumer_name]
        performer = Performer(consumer_name, consumer.connection,
                              consumer.brightside_configuration,
                              consumer.consumer_factory,
                              consumer.command_processor_factory,
                              consumer.mapper_func)
        self._performers[consumer_name] = performer

        # if we have a supervisor thread
        if self._state == DispatcherState.ds_running:
            # start and add to items monitored by supervisor (running performers)
            pass
        # else
        elif self._state == DispatcherState.ds_stopped:
            # start the supervisor with the single consumer
            self._state = DispatcherState.ds_awaiting
            self.receive()
        else:
            raise MessagingException(
                "Dispatcher in a un-recognised state to open new connection; state was {}",
                self._state)
    def post(self, request: Request) -> None:
        """
        Dispatches a request over middleware. Returns when message put onto outgoing channel by producer,
        does not wait for response from a consuming application i.e. is fire-and-forget
        :param request: The request to dispatch
        :return: None
        """

        if self._producer is None:
            raise ConfigurationException(
                "Command Processor requires a BrightsideProducer to post to a Broker"
            )
        if self._message_mapper_registry is None:
            raise ConfigurationException(
                "Command Processor requires a BrightsideMessage Mapper Registry to post to a Broker"
            )

        message_mapper = self._message_mapper_registry.lookup(request)
        message = message_mapper(request)
        self._message_store.add(message)
        self._producer.send(message)
Пример #3
0
    def register(self, request_class: Request, mapper_func: Callable[[Request], BrightsideMessage]) -> None:
        """Adds a message mapper to a factory, using the requests key
        :param mapper_func: A callback that creates a BrightsideMessage from a Request
        :param request_class: A request type
        """

        key = request_class.__name__
        if key not in self._registry:
            self._registry[key] = mapper_func
        else:
            raise ConfigurationException(
                "There is already a message mapper defined for this key; there can be only one")
    def send(self, request: Request) -> None:
        """
        Dispatches a request. Expects one and one only target handler
        :param request: The request to dispatch
        :return: None, will throw a ConfigurationException if more than one handler factor is registered for the command
        """

        handler_factories = self._registry.lookup(request)
        if len(handler_factories) != 1:
            raise ConfigurationException(
                "There is no handler registered for this request")
        handler = handler_factories[0]()
        handler.handle(request)
Пример #5
0
 def lookup(self, request_class: Request) -> Callable[[Request], BrightsideMessage]:
     """
     Looks up the message mapper function associated with this class. Function should take in a Request derived class
      and return a BrightsideMessage derived class, for sending on the wire
     :param request_class:
     :return:
     """
     key = request_class.__class__.__name__
     if key not in self._registry:
         raise ConfigurationException(
             "There is no message mapper associated with this key; we require a mapper")
     else:
         return self._registry[key]
Пример #6
0
    def lookup(self, request: Request) -> List[Callable[[], Handler]]:
        """
        Looks up the handler associated with a request - matches the key on the request to a registered handler
        :param request: The request we want to find a handler for
        :return:
        """
        key = request.__class__.__name__
        if key not in self._registry:
            if request.is_command():
                raise ConfigurationException(
                    "There is no handler registered for this request")
            elif request.is_event():
                return []  # type: Callable[[] Handler]

        return self._registry[key]
Пример #7
0
 def register(self, request_class: Request, handler_factory: Callable[[], Handler]) -> None:
     """
     Register the handler for the command
     :param request_class: The command or event to dispatch. It must implement getKey()
     :param handler_factory: A factory method to create the handler to dispatch to
     :return:
     """
     key = request_class.__name__
     is_command = request_class.is_command()
     is_event = request_class.is_event()
     is_present = key in self._registry
     if is_command and is_present:
         raise ConfigurationException(
             "A handler for this request has already been registered")
     elif is_event and is_present:
         self._registry[key].append(handler_factory)
     elif is_command or is_event:
         self._registry[key] = [handler_factory]
Пример #8
0
 def _translate_message(self, message: BrightsideMessage)-> Request:
     if self._mapper_func is None:
         raise ConfigurationException("Missing Mapper Function for message topic {}".format(message.header.topic))
     return self._mapper_func(message)
Пример #9
0
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**********************************************************************i*
"""
import os

from brightside.exceptions import ConfigurationException
from brightside.messaging import BrightsideMessageType
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, DateTime, Enum
from alchemy_store.custom_types import GUID

db_uri = os.environ.get('BRIGHTER_MESSAGE_STORE_URL')

if db_uri is None:
    raise ConfigurationException(
        "Please define the BRIGHTER_MESSAGE_STORE_URL environment variable")

engine = create_engine(db_uri, echo=True)
metadata = MetaData()

messages = Table(
    'messages', metadata, Column('Id', Integer, primary_key=True),
    Column('MessageId', GUID, nullable=False),
    Column('Topic', String(255), nullable=True),
    Column('MessageType', Enum(BrightsideMessageType), nullable=True),
    Column('Timestamp', DateTime, nullable=True),
    Column('HeaderBag', String, nullable=True),
    Column('Body', String, nullable=True))

metadata.create_all(engine)