-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: [FC-0074] add docs about creating and consuming events #439
Open
mariajgrimaldi
wants to merge
32
commits into
MJG/event-design-suggestions-adr
Choose a base branch
from
MJG/how-to-create-events
base: MJG/event-design-suggestions-adr
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
187f16e
docs: refactor names for consistency in how-tos
mariajgrimaldi 29c4c3a
docs: add docs about creating and consuming events
mariajgrimaldi f3e0d8e
docs: add assumptions for how-tos
mariajgrimaldi 874f4f3
docs: add more considerations to keep in mind
mariajgrimaldi 8f3da1c
docs: add consequences for ADR
mariajgrimaldi 5809e22
docs: add note about consumption
mariajgrimaldi 9029d47
docs: add note about considering support over time for the service
mariajgrimaldi c8ec268
docs: move identify triggering logic to previous step
mariajgrimaldi 297069f
docs: use implement instead of write
mariajgrimaldi 28845d0
docs: add note about data stability over time
mariajgrimaldi f8d1497
docs: split tests needed for the new event
mariajgrimaldi e30b79f
docs: add note about triggering only when facts happen
mariajgrimaldi d63020c
docs: add independent step for consuming the event
mariajgrimaldi dad6fcc
docs: add note about adding tests to the triggering logic
mariajgrimaldi b5d1cab
docs: add an example of testing integration
mariajgrimaldi 7e66121
docs: add more details about example code
mariajgrimaldi 287908a
fix: add plural for contribution process
mariajgrimaldi 8206324
docs: clarify identify triggering logic
mariajgrimaldi 6ad6bcf
docs: clarify where to place an event
mariajgrimaldi ae80dce
docs: add note about inspecting event content to implement custom logic
mariajgrimaldi 992e7a2
docs: use example from openedx-events-2-zapier plugin
mariajgrimaldi 4f259e6
docs: match description for new use case
mariajgrimaldi e540e29
docs: add notes about reviewing the event content for the use case
mariajgrimaldi 84c4639
docs: add note about django plugins
mariajgrimaldi 13e3d5f
docs: add note about OEP-49 handler patterns
mariajgrimaldi d124da8
docs: add note about identify event to consume
mariajgrimaldi f3f8141
refactor: fix typing error
mariajgrimaldi caba18f
docs: add notes about implementing receivers
mariajgrimaldi 5c22b93
docs: add suggestion about avoiding suffixes in data field names
mariajgrimaldi 37e3ae4
refactor: address PR review
mariajgrimaldi 7980afc
docs: drop unnecessary assumption
mariajgrimaldi bdccd4c
docs: add reference to openedx-events-2-zapier
mariajgrimaldi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 was deleted.
Oops, something went wrong.
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,132 @@ | ||
Consume an Open edX Event | ||
========================= | ||
|
||
You have two ways of consuming an Open edX event, within the same service or in a different service. In this guide, we will show you how to consume an event within the same service. For consuming events across services, see :doc:`../how-tos/use-the-event-bus-to-broadcast-and-consume-events`. | ||
|
||
.. note:: We encourage you to also consider the practices outlined in the :doc:`../decisions/0016-event-design-practices` ADR for event consumption. | ||
|
||
Throughout this guide, we will implement the use case to send the enrollment data to a webhook when a user enrolls in a course to better illustrate the steps involved in creating a consumer for an event. | ||
|
||
Assumptions | ||
----------- | ||
|
||
- You have a development environment set up using `Tutor`_. | ||
- You have a basic understanding of Python and Django. | ||
- You have a basic understanding of Django signals. If not, you can review the `Django Signals Documentation`_. | ||
- You are familiar with the terminology used in the project, such as the terms :term:`Event Type` or :term:`Event Receiver`. If not, you can review the :doc:`../reference/glossary` documentation. | ||
|
||
Steps | ||
----- | ||
|
||
To consume an event within the same service, follow these steps: | ||
|
||
Step 1: Understand your Use Case and Identify the Event to Consume | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Before you start consuming an event, you should understand the use case and the event you want to consume, for this review the `list of events`_ available in the Open edX platform. In this case, we want to send the enrollment data to a webhook when a user enrolls in a course. You should review the event definition and payload to understand the data that is being passed to the event receiver and how you can use it to implement the custom logic. | ||
|
||
In our example, we want to send the enrollment data to a webhook when a user enrolls in a course. We will consume the ``COURSE_ENROLLMENT_CREATED`` event, which is triggered every time a user enrolls in a course. You can review the event definition and payload to understand the data that is being passed to the event receiver and how you can use it to implement the request to the webhook. | ||
|
||
Step 2: Install Open edX Events | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
First, add the ``openedx-events`` plugin into your dependencies so the library's environment recognizes the event you want to consume. You can install ``openedx-events`` by running: | ||
|
||
.. code-block:: bash | ||
|
||
pip install openedx-events | ||
|
||
This will mainly make the events available for your CI/CD pipeline and local development environment. If you are using the Open edX platform, the library should be already be installed in the environment so no need to install it. | ||
|
||
Step 3: Create a Event Receiver and Connect it to the Event | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
An :term:`Event Receiver` is simply a function that listens for a specific event and executes custom logic in response to the event being triggered. You can create an event receiver by using the Django signal receivers decorator. Here's an example of an event receiver that listens for the ``COURSE_ENROLLMENT_CREATED`` event and creates a notification preference for the user: | ||
|
||
.. code-block:: python | ||
|
||
from openedx_events import COURSE_ENROLLMENT_CREATED | ||
from django.dispatch import receiver | ||
|
||
@receiver(COURSE_ENROLLMENT_CREATED) | ||
def send_enrollment_data_to_webhook(signal, sender, enrollment, metadata, **kwargs): | ||
# Custom logic to send enrollment data to a webhook | ||
pass | ||
|
||
- The Django dispatcher will call the ``send_enrollment_data_to_webhook`` function when the ``COURSE_ENROLLMENT_CREATED`` event is triggered by using the ``receiver`` decorator. In this case, that would be every time a user enrolls in a course. | ||
- Consider using asynchronous tasks to handle the event processing to avoid blocking the main thread and improve performance. Also, make sure to handle exceptions and errors gracefully to avoid silent failures and improve debugging. You should also consider not creating a tight coupling between receivers and other services, if doing so is necessary consider using the event bus to broadcast the event. | ||
- When implementing the receiver, inspect the event payload to understand the data that is being passed to the event receiver by reviewing the ``data.py`` file of the event you are consuming. For example, the ``COURSE_ENROLLMENT_CREATED`` event has the following payload: | ||
|
||
.. code-block:: python | ||
|
||
# Location openedx_events/learning/data.py | ||
COURSE_ENROLLMENT_CREATED = OpenEdxPublicSignal( | ||
event_type="org.openedx.learning.course.enrollment.created.v1", | ||
data={ | ||
"enrollment": CourseEnrollmentData, | ||
} | ||
) | ||
|
||
- This event has a single field called ``enrollment`` which is an instance of the ``CourseEnrollmentData`` class. You can review the ``CourseEnrollmentData`` class to understand the data that is available to you and how you can use it to implement the custom logic. | ||
- The ``metadata`` parameter contains the Open edX-specific metadata for the event, such as the event version and timestamp when the event was sent. You can use this metadata to understand more about the event and its context. | ||
|
||
These event receivers are usually implemented independently of the service in an `Open edX Django plugins`_ and are registered in the ``handlers.py`` (according to `OEP-49`_) file of the plugin. You can review the ``handlers.py`` file of the `openedx-events-2-zapier`_ plugin to understand how the event receivers are implemented and connected to the events. | ||
|
||
.. TODO: change receivers.py in openedx-events-2-zapier to handlers.py | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be done before the PR merges? |
||
|
||
Consider the following when implementing the event receiver: | ||
|
||
- Limit each receiver to a single responsibility to make the code easier to maintain and test. | ||
- Keep the receiver logic simple and focused on the specific task it needs to perform. | ||
- Consider the performance implications of the receiver and avoid adding unnecessary complexity or overhead, considering that receivers will be executed each time the event is triggered. Consider using asynchronous tasks to handle the event processing to avoid blocking the main thread and improve performance. | ||
- Implement error handling and logging in the pipeline step to handle exceptions and provide useful information for debugging, considering both development and production environments. | ||
|
||
Step 4: Test the Event Receiver | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Given the design of Open edX Events, you can include the events definitions in your test suite to ensure that the event receiver is working as expected. You can use the ``send_event`` method to trigger the event and test the event receiver. Here's an example of how you can test the event receiver: | ||
|
||
.. code-block:: python | ||
|
||
from openedx_events import send_event, COURSE_ENROLLMENT_CREATED | ||
|
||
def test_send_enrollment_data_to_webhook(self): | ||
# Trigger the event | ||
enrollment_data = CourseEnrollmentData( | ||
user=UserData( | ||
pii=UserPersonalData( | ||
username=self.user.username, | ||
email=self.user.email, | ||
name=self.user.profile.name, | ||
), | ||
id=self.user.id, | ||
is_active=self.user.is_active, | ||
), | ||
course=CourseData( | ||
course_key=self.course.id, | ||
display_name=self.course.display_name, | ||
), | ||
mode=self.course_enrollment.mode, | ||
is_active=self.course_enrollment.is_active, | ||
creation_date=self.course_enrollment.created, | ||
) | ||
|
||
COURSE_ENROLLMENT_CREATED.send_event( | ||
enrollment=enrollment_data | ||
) | ||
|
||
# Assert that the request was sent to the webhook with the correct data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you missing an assert statement here? |
||
|
||
- In the test suite, you can use the ``send_event`` method to trigger the event and pass the necessary data to the event receiver. In this case, we are passing the user, course and enrollment data to the event receiver as the triggering logic would do. | ||
- After triggering the event, you can assert that the event receiver executed the custom logic as expected. In this case, we are checking that the request was sent to the webhook with the correct data. | ||
|
||
You can review this example to understand how you can test the event receiver and ensure that the custom logic is executed when the event is triggered in the `openedx-events-2-zapier`_ plugin. | ||
|
||
This way you can ensure that the event receiver is working as expected and that the custom logic is executed when the event is triggered. If the event definition or payload changes in any way, you can catch the error in the test suite instead of in production. | ||
|
||
.. _Tutor: https://docs.tutor.edly.io/ | ||
.. _Django Signals Documentation: https://docs.djangoproject.com/en/4.2/topics/signals/ | ||
.. _openedx-events-2-zapier: https://github.com/eduNEXT/openedx-events-2-zapier | ||
.. _Open edX Django plugins: https://docs.openedx.org/en/latest/developers/concepts/platform_overview.html#new-plugin | ||
.. _OEP-49: https://docs.openedx.org/projects/openedx-proposals/en/latest/best-practices/oep-0049-django-app-patterns.html#signals | ||
.. _list of events: https://docs.openedx.org/projects/openedx-events/en/latest/reference/events.html |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small language tweak for easier reading