Skip to main content

No project description provided

Project description

Weni EDA

weni-eda is a Python library designed to simplify the use of Event-Driven Architecture (EDA). It provides an interface that seamlessly integrates with the Django framework and RabbitMQ messaging service. The design is scalable and intended to support various integrations in the future.

Features

  • Easy integration with Django.
  • Support for RabbitMQ.
  • Simplified event handling and message dispatching.
  • Scalable design to accommodate future integrations with other frameworks and messaging services.

Installation

To install the library, use pip:

pip install weni-eda

Configuration

Django Integration

  1. Add weni-eda to your Django project:
    Add weni.eda.django.eda_app to your INSTALLED_APPS in settings.py:

    # settings.py
    INSTALLED_APPS = [
        # ... other installed apps
        'weni.eda.django.eda_app',
    ]
    
  2. Environment Variables for weni-eda Configuration

    The following environment variables are used to configure the weni-eda library. Here is a detailed explanation of each variable:

    Variable Name Examples Description
    EDA_CONSUMERS_HANDLE "example.event_driven.handle.handle_consumers" Specifies the handler module for consumer events.
    EDA_BROKER_HOST "localhost" The hostname or IP address of the message broker server.
    EDA_VIRTUAL_HOST "/" The virtual host to use when connecting to the broker.
    EDA_BROKER_PORT 5672 The port number on which the message broker is listening.
    EDA_BROKER_USER "guest" The username for authenticating with the message broker.
    EDA_BROKER_PASSWORD "guest" The password for authenticating with the message broker.

    The following variables are used only when connecting over SSL with weni.eda.django.AMQConnectionParamsFactory (used by brokers such as AmazonMQ):

    Variable Name Examples Description
    AMQ_BROKER_HOST "localhost" The hostname or IP address of the SSL message broker server.
    AMQ_VIRTUAL_HOST "/" The virtual host to use when connecting to the SSL broker.
    AMQ_BROKER_USER "guest" The username for authenticating with the SSL message broker.
    AMQ_BROKER_PASSWORD "guest" The password for authenticating with the SSL message broker.
    AMQ_BROKER_PORT 5671 The SSL port of the message broker. Defaults to 5671.
    AMQ_BROKER_HEARTBEAT 300 Heartbeat interval (seconds) negotiated with the broker. Defaults to 300.
    AMQ_BROKER_SSL_SERVER_HOSTNAME "broker.host" Hostname used for SSL certificate verification (SNI). Defaults to AMQ_BROKER_HOST.
  3. Creating your event consumers
    We provide an abstract class that facilitates the consumption of messages. To use it, you need to inherit it and declare the consume method as follows:

    from weni.eda.django.consumers import EDAConsumer
    
    
    class ExampleConsumer(EDAConsumer):
        def consume(self, message: Message):
            body = JSONParser.parse(message.body)
            self.ack()
    
    • JSONParser.parse(message.body) Converts the message arriving from RabbitMQ in JSON format to dict
    • self.ack() Confirms to RabbitMQ that the message can be removed from the queue, which prevents it from being reprocessed.
  4. Registering your event handlers:
    the EDA_CONSUMERS_HANDLE variable indicates the function that will be called when the consumer starts. this function will be responsible for mapping the messages to their respective consumers. The function must be declared as follows:

    import amqp
    
    from .example_consumer import ExampleConsumer
    
    
    def handle_consumers(channel: amqp.Channel):
        channel.basic_consume("example-queue", callback=ExampleConsumer().handle)
    

    This indicates that any message arriving at the example-queue queue will be dispatched to the ExampleConsumer consumer and will fall into its consume method.

  5. Starting to consume the queues
    To start consuming messages from the queue, you need to run the edaconsume command as follows:

    python manage.py edaconsume
    

    From then on, all messages that arrive in the queues where your application is written will be dispatched to their respective consumers.

    By default the consumer connects to the standard broker (no SSL). To connect over SSL on port 5671, pass the --params-class flag with the dotted path to the desired ParamsFactory:

    python manage.py edaconsume   # default broker (no SSL)
    python manage.py edaconsume --params-class "weni.eda.django.AMQConnectionParamsFactory"   # SSL 5671
    

Buffered consumers (optional flush backend)

By default the consume loop blocks indefinitely waiting for events, so each consumer must ack messages one by one. Some workloads (high-throughput consumers that batch DB writes) need to buffer messages and flush them periodically. For those cases the library ships an optional PyAMQPFlushConnectionBackend.

This backend drains with a timeout so it can flush buffered consumers on a fixed interval. py-amqp is single-threaded and not thread-safe, so this is the safe way to do time-based flushing (no extra IO thread).

It is fully opt-in: consumers that do not need buffering keep using the default backend. To use it, your handle_consumers(channel) must register the consumers and return an iterable of "flushable" objects, where each flushable exposes:

  • flush(): persist and ack its buffered work;
  • flush_interval (optional float): maximum seconds between flushes (defaults to 1.0).
from weni.eda.backends.pyamqp_flush_backend import PyAMQPFlushConnectionBackend
from weni.eda.django.connection_params import AMQConnectionParamsFactory


def handle_consumers(channel):
    consumer = BufferedConsumer()  # exposes flush() and flush_interval
    consumer.setup(channel)        # channel.basic_qos(...) + channel.basic_consume(...)
    return [consumer]


def run():
    params = AMQConnectionParamsFactory.get_params()
    PyAMQPFlushConnectionBackend(handle_consumers).start_consuming(params)

Returning None (or an empty iterable) from handle_consumers disables periodic flushing, making it behave like a plain timed drain loop. The backend can also be selected via settings.EDA_CONNECTION_BACKEND.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

weni_eda-0.2.0a4.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

weni_eda-0.2.0a4-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file weni_eda-0.2.0a4.tar.gz.

File metadata

  • Download URL: weni_eda-0.2.0a4.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.0 Darwin/25.3.0

File hashes

Hashes for weni_eda-0.2.0a4.tar.gz
Algorithm Hash digest
SHA256 d6cc0d9d572d0931f12483cc13f9e4df4ee3af464ffabdbeadd018cdc600d0e3
MD5 12136bb48507e234e14f9278153861fb
BLAKE2b-256 ad6518348e9d1772e878722ac62c9b1f81dd66341e735dc44f0138320e427b71

See more details on using hashes here.

File details

Details for the file weni_eda-0.2.0a4-py3-none-any.whl.

File metadata

  • Download URL: weni_eda-0.2.0a4-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.0 Darwin/25.3.0

File hashes

Hashes for weni_eda-0.2.0a4-py3-none-any.whl
Algorithm Hash digest
SHA256 44c5338e201bc46c5647cfa29550b1dd74de08467f7817eca901248f35785b0a
MD5 2c8cb8bebef8c4d9bf2fbeef9e4871a1
BLAKE2b-256 5097a5df0aa352c7d0b0d163a4fd53069ae63e1f1ff220a42c6714bad6fc433d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page