Skip to main content

Send and receive domain events via RabbitMQ

Project description

Domain event broker

This library provides a shallow layer on top of RabbitMQ topic exchanges for publishing and receiving domain events. Publisher and subscriber need not know about each other and can be started and stopped in any order. Each subscriber controls their own retry policy, whether they need a durable queue for the time they are down, or a dead-letter queue in case there is an error in the subscriber.

Configuration

This library needs to connect to RabbitMQ. By default, a local instance of RabbitMQ is used. This can be changed by passing an amqp URL to publish_domain_event or when instantiating Publisher or Subscriber:

from domain_event_broker import Subscriber
subscriber = Subscriber('amqp://user:password@rabbitmq-host/domain-events')

Integrations

Django

This library can be configured via your Django settings. Add domain_event_broker.django to your INSTALLED_APPS and set the DOMAIN_EVENT_BROKER in your settings:

INSTALLED_APPS = (
    'domain_event_broker.django',
    )

DOMAIN_EVENT_BROKER = 'amqp://user:password@rabbitmq-host/domain-events'

More information can be found in the documentation.

Sending events

Events can be sent by calling publish_domain_event:

from domain_event_broker import publish_domain_event
publish_domain_event('user.registered', {'user_id': user.id})

Domain events are sent immediately. When emitting domain events from within a database transaction, it's recommended to defer publishing until the transaction is committed. Using a commit hook avoids spurious domain events if a transaction is rolled back after an error.

Receiving events

Subscribers can listen to one or more domain events - controlled via the binding keys. Binding keys may contain wildcards. A queue will be created for each subscriber. RabbitMQ takes care of routing only the relevant events to this queue.

This script will receive all events that are sent in the user domain:

from domain_event_broker import Subscriber

def log_user_event(event):
    print(event)

subscriber = Subscriber()
subscriber.register(log_user_event, 'printer', ['user.*'])
subscriber.start_consuming()

Retry policy

If there is a problem consuming a message - for example a web service is down - the subscriber can raise an error to retry handling the event after the given delay:

from domain_event_broker import Subscriber

def sync_user_data(event):
    try:
        publish_to_service(event)
    except ServiceIsDown:
        raise Retry(5.0 ** event.retries) # 1s, 5s, 25s

subscriber = Subscriber()
subscriber.register(sync_user_data, 'sync_data', ['user.*'], max_retries=3)
subscriber.start_consuming()

The delayed retries are bound to the consumer, not the event. If max_retries is exceeded, the event will be dropped or dead-lettered.

Development

Make sure you have RabbitMQ installed locally for testing.

  • Create virtualenv and activate it
  • Install dependencies with pip install -r requirements.txt -r dev_requirements.txt -e .
  • Run tests with pytest

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

domain-event-broker-3.0.1.tar.gz (32.2 kB view details)

Uploaded Source

Built Distribution

domain_event_broker-3.0.1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file domain-event-broker-3.0.1.tar.gz.

File metadata

  • Download URL: domain-event-broker-3.0.1.tar.gz
  • Upload date:
  • Size: 32.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5

File hashes

Hashes for domain-event-broker-3.0.1.tar.gz
Algorithm Hash digest
SHA256 ed61b3fcb43b805d38082cc5e0ac03a8ad046dcce5709805b1bc2d36c6a76064
MD5 e57140df1c0d4ef5b4d847d6a51b6c7c
BLAKE2b-256 9b3c558cb7e93534fd08e0fdb362b62415fe78ac24c4c7138acf212b341395ea

See more details on using hashes here.

File details

Details for the file domain_event_broker-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: domain_event_broker-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5

File hashes

Hashes for domain_event_broker-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 26f3394df39bf450e0bd8c2b00a775fbb26c19043405deea316ecced90723e95
MD5 b12d8f8970e89c1db1d193063dcdfb63
BLAKE2b-256 9c17fda310379826dc0af8800b9b6906fd0f29c9662d3e41c494bb61856148e4

See more details on using hashes here.

Supported by

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