-
Notifications
You must be signed in to change notification settings - Fork 13
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 #73 from romank0/payload-extras-upstream
Allow to add context to payload
- Loading branch information
Showing
15 changed files
with
489 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
.. _payload_context: | ||
|
||
Payload Context | ||
=============== | ||
|
||
Sometimes it is beneficial to pass some contextual information from the trigger | ||
to the trigger listener along the payload. Examples are: | ||
|
||
- tracing information that allows to track complex request processing in a | ||
multi component system | ||
- in a multitenant system a tenant information to be able to identify the | ||
tenant that peformed an operation that triggered a notification | ||
|
||
|
||
This can be done by using **Payload Context**. This feature includes: | ||
|
||
- ability to add an additional information to the payload in the trigger | ||
- ability to filter by the fields in the context in the listener process | ||
- ability to use ``context`` fields in the listener callbacks | ||
|
||
|
||
Add ``context`` to payload in the trigger | ||
----------------------------------------- | ||
|
||
Before doing updates that produce notifications set the context that should be | ||
passed using ``pgpubsub.set_notification_context`` function. | ||
|
||
.. code-block:: python | ||
from pgpubsub import set_notification_context | ||
set_notification_context({'some-key': 'some-value'}) | ||
The setting is effective till the connection is closed. Alternatively the | ||
setting ``PGPUBSUB_TX_BOUND_NOTIFICATION_CONTEXT=True`` can be used to clean | ||
the context at the end of the current transanction. | ||
|
||
|
||
Filter by ``context`` field in the trigger listener | ||
--------------------------------------------------- | ||
|
||
Note: that the filtering is currently supported only for stored notifications that is | ||
only for channels with ``lock_notifications = True``. | ||
|
||
Define a class that implements the ``ListenerFilterProvider`` protocol and set | ||
the option ``PGPUBSUB_LISTENER_FILTER`` to its fully qualified class name. | ||
|
||
.. code-block:: python | ||
from pgpubsub import ListenerFilterProvider | ||
class TenantListenerFilterProvider(ListenerFilterProvider): | ||
def get_filter(self) -> Q: | ||
return Q(payload__context__tenant='my-tenant') | ||
# django settings | ||
PGPUBSUB_LISTENER_FILTER = 'myapp.whatever.TenantListenerFilterProvider' | ||
This configuration will skip any notifications that do not have ``tenant`` field | ||
equal to ``my-tenant`` in the payload's ``context`` field. | ||
|
||
Pass ``context`` field to the trigger listener callback | ||
------------------------------------------------------- | ||
|
||
To enable this set ``PGPUBSUB_CONTEXT_TO_LISTENERS`` to ``True`` in django | ||
settings and add a ``context`` parameter to the listener callback. | ||
|
||
.. code-block:: python | ||
# listeners.py | ||
import pgpubsub | ||
from pgpubsub.tests.channels import AuthorTriggerChannel | ||
from pgpubsub.tests.models import Author, Post | ||
@pgpubsub.post_insert_listener(AuthorTriggerChannel) | ||
def create_first_post_for_author( | ||
old: Author, new: Author, context: Dict[str, Any] | ||
): | ||
print(f'Creating first post for {new.name} with context={context}') | ||
Post.objects.create( | ||
author_id=new.pk, | ||
content='Welcome! This is your first post', | ||
date=datetime.date.today(), | ||
) | ||
# django settings | ||
PGPUBSUB_PASS_CONTEXT_TO_LISTENERS = True |
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ Table of Contents | |
notifications | ||
exactly_once_messaging | ||
recovery | ||
payload_context | ||
|
||
|
||
.. toctree:: | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pytest | ||
from django.db import connection | ||
from pgpubsub.listen import listen_to_channels | ||
|
||
@pytest.fixture() | ||
def pg_connection(): | ||
return listen_to_channels() | ||
|
||
|
||
@pytest.fixture | ||
def tx_start_time(django_db_setup): | ||
with connection.cursor() as cursor: | ||
cursor.execute("SELECT now();") | ||
return cursor.fetchone()[0] |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
def simulate_listener_does_not_receive_notifications(pg_connection): | ||
pg_connection.notifies = [] | ||
pg_connection.poll() | ||
assert 0 == len(pg_connection.notifies) |
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
Oops, something went wrong.