Skip to content
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

feat: adding event bus functionality #1848

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions enterprise/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from logging import getLogger

from openedx_events.event_bus import get_producer

from django.db.models.signals import post_delete, post_save, pre_save
from django.dispatch import receiver

Expand Down Expand Up @@ -349,16 +351,23 @@ def enterprise_unenrollment_receiver(sender, **kwargs): # pylint: disable=un
"""
enrollment = kwargs.get('enrollment')
enterprise_enrollment = models.EnterpriseCourseEnrollment.objects.filter(
course_id=enrollment.course.course_key,
enterprise_customer_user__user_id=enrollment.user.id,
course_id=enrollment.course_id,
enterprise_customer_user__user_id=enrollment.enterprise_customer_user.user_id,
).first()
if enterprise_enrollment:
logger.info(
f"Marking EnterpriseCourseEnrollment as unenrolled for user {enrollment.user} and "
f"course {enrollment.course.course_key}"
f"Marking EnterpriseCourseEnrollment as unenrolled for user "
f"{enrollment.enterprise_customer_user.user_id} and course {enrollment.course_id}"
)
enterprise_enrollment.unenrolled_at = localized_utcnow()
enterprise_enrollment.save()
get_producer().send(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be gated behind a feature_flag?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a hard requirement but a best practice. It makes sense to have a feature flag in edx-platform or Credentials as there is likely going to be some time before the event bus is widely adopted by the Open edX Community. Relevant link to docs: https://openedx.atlassian.net/wiki/spaces/AC/pages/3508699151/How+to+start+using+the+Event+Bus#Producing

If there are any consumers of edx-enterprise outside of edx.org/2U, I would say it's a necessity. At the least, a temporary one is good in case we need to disable the functionality without a revert/code release.

signal=COURSE_UNENROLLMENT_COMPLETED,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a generic unenrollment signal but you only fire it for enterprise enrollments - i'd probably produce an event for all of these signals, not just the enterprise ones. i think this event is emitted somewhere upstream and that is where we should put that signal on the bus

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check if COUSE_ENROLLMENT_COMPLETED is not None before using it? It looks like it can potentially be None depending on if there was an ImportError.

Copy link
Contributor Author

@kiram15 kiram15 Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I didn't put the check on it was later on in the code (line 410), we have a check that doesn't connect the receiver unless it is not None. I can put another check in if this is not sufficient though, but this seems to do it already.

if COURSE_UNENROLLMENT_COMPLETED is not None: COURSE_UNENROLLMENT_COMPLETED.connect(enterprise_unenrollment_receiver)

topic='course-unenrollment-completed',
kiram15 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you look into using a topic like 'course-enrollment-lifecycle'?

Copy link
Contributor

@justinhynes justinhynes Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, my understanding is the topics that are named with "{blah]-lifecycle" are when a topic handles multiple distinct events in a single topic.

The {environment}-learning-certificate-lifecycle events receive the "certificate awarded" and "certificate revoked" events.

@kiram15 -- One thing I will note, another best practice is that the name of the topic should contain the architecture subdomain the event belongs to. In this case, it is the learning subdomain. Whatever this topic name ends up being, it should include this subdomain (e.g. learning-{topic_name}).

event_key_field='enrollment.course.course_key',
event_data={'enrollment': kwargs.get('enrollment')},
event_metadata=kwargs.get('metadata')
)


def create_enterprise_enrollment_receiver(sender, instance, **kwargs): # pylint: disable=unused-argument
Expand Down
2 changes: 1 addition & 1 deletion requirements/celery50.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
amqp==5.1.1
billiard==4.1.0
celery==5.3.1
click==8.1.6
click==8.1.7
click-didyoumean==0.3.0
click-repl==0.3.0
kombu==5.3.1
Expand Down
2 changes: 2 additions & 0 deletions requirements/common_constraints.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@



# A central location for most common version constraints
# (across edx repos) for pip-installation.
#
Expand Down
1 change: 1 addition & 0 deletions requirements/dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ futures; python_version == "2.7" # Used by caniusepython3, only needed for Pytho
edx-lint # edX pylint rules and plugins
edx-i18n-tools # For i18n_tool dummy
isort # to standardize order of imports
openedx-events>=8.3.0 # Open edX Events from Hooks Extension Framework (OEP-50)
pip-tools # Requirements file management
pycodestyle # PEP 8 compliance validation
pydocstyle # PEP 257 compliance validation
Expand Down
36 changes: 26 additions & 10 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ attrs==23.1.0
# via
# -r requirements/doc.txt
# -r requirements/test.txt
# openedx-events
# pytest
babel==2.12.1
# via
Expand Down Expand Up @@ -109,7 +110,7 @@ charset-normalizer==2.0.12
# -r requirements/test.txt
# requests
# snowflake-connector-python
click==8.1.6
click==8.1.7
# via
# -r requirements/doc.txt
# -r requirements/test-master.txt
Expand Down Expand Up @@ -158,7 +159,7 @@ cryptography==38.0.4
# -r requirements/doc.txt
# -r requirements/test-master.txt
# -r requirements/test.txt
# django-fernet-fields
# django-fernet-fields-v2
# pgpy
# pyjwt
# pyopenssl
Expand All @@ -185,7 +186,7 @@ django==3.2.20
# -r requirements/test.txt
# django-config-models
# django-crum
# django-fernet-fields
# django-fernet-fields-v2
# django-filter
# django-model-utils
# django-multi-email-field
Expand All @@ -197,6 +198,7 @@ django==3.2.20
# edx-i18n-tools
# edx-rbac
# jsonfield
# openedx-events
django-cache-memoize==0.1.10
# via
# -r requirements/doc.txt
Expand Down Expand Up @@ -300,7 +302,7 @@ edx-django-utils==5.7.0
# django-config-models
# edx-drf-extensions
# edx-rest-api-client
edx-drf-extensions==8.8.0
edx-drf-extensions==8.9.0
# via
# -r requirements/doc.txt
# -r requirements/test-master.txt
Expand All @@ -316,12 +318,13 @@ edx-opaque-keys[django]==2.4.0
# -r requirements/test-master.txt
# -r requirements/test.txt
# edx-drf-extensions
# openedx-events
edx-rbac==1.8.0
# via
# -r requirements/doc.txt
# -r requirements/test-master.txt
# -r requirements/test.txt
edx-rest-api-client==5.5.2
edx-rest-api-client==5.6.0
# via
# -r requirements/doc.txt
# -r requirements/test-master.txt
Expand All @@ -341,6 +344,8 @@ faker==19.3.0
# -r requirements/doc.txt
# -r requirements/test.txt
# factory-boy
fastavro==1.8.2
# via openedx-events
filelock==3.12.2
# via
# -r requirements/doc.txt
Expand Down Expand Up @@ -415,12 +420,14 @@ mock==3.0.5
# via
# -c requirements/constraints.txt
# -r requirements/test.txt
newrelic==8.8.1
newrelic==8.10.0
# via
# -r requirements/doc.txt
# -r requirements/test-master.txt
# -r requirements/test.txt
# edx-django-utils
openedx-events==8.5.0
# via -r requirements/dev.in
oscrypto==1.3.0
# via
# -r requirements/doc.txt
Expand Down Expand Up @@ -470,9 +477,13 @@ pip-tools==7.3.0
# via -r requirements/dev.in
pkginfo==1.9.6
# via twine
platformdirs==3.10.0
platformdirs==3.8.1
# via
# -r requirements/doc.txt
# -r requirements/test-master.txt
# -r requirements/test.txt
# pylint
# snowflake-connector-python
# virtualenv
pluggy==1.2.0
# via
Expand Down Expand Up @@ -684,7 +695,7 @@ snowballstemmer==2.2.0
# -r requirements/doc.txt
# pydocstyle
# sphinx
snowflake-connector-python==3.0.4
snowflake-connector-python==3.1.0
# via
# -r requirements/doc.txt
# -r requirements/test-master.txt
Expand Down Expand Up @@ -774,7 +785,12 @@ tomli==2.0.1
# pyproject-hooks
# tox
tomlkit==0.12.1
# via pylint
# via
# -r requirements/doc.txt
# -r requirements/test-master.txt
# -r requirements/test.txt
# pylint
# snowflake-connector-python
tox==3.28.0
# via
# -c requirements/common_constraints.txt
Expand Down Expand Up @@ -827,7 +843,7 @@ vine==5.0.0
# amqp
# celery
# kombu
virtualenv==20.24.3
virtualenv==20.24.1
# via tox
wcwidth==0.2.6
# via
Expand Down
22 changes: 15 additions & 7 deletions requirements/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ charset-normalizer==2.0.12
# -r requirements/test-master.txt
# requests
# snowflake-connector-python
click==8.1.6
click==8.1.7
# via
# -r requirements/test-master.txt
# celery
Expand All @@ -93,7 +93,7 @@ code-annotations==1.5.0
cryptography==38.0.4
# via
# -r requirements/test-master.txt
# django-fernet-fields
# django-fernet-fields-v2
# pgpy
# pyjwt
# pyopenssl
Expand All @@ -108,7 +108,7 @@ django==3.2.20
# -r requirements/test-master.txt
# django-config-models
# django-crum
# django-fernet-fields
# django-fernet-fields-v2
# django-filter
# django-model-utils
# django-multi-email-field
Expand Down Expand Up @@ -181,7 +181,7 @@ edx-django-utils==5.7.0
# django-config-models
# edx-drf-extensions
# edx-rest-api-client
edx-drf-extensions==8.8.0
edx-drf-extensions==8.9.0
# via
# -r requirements/test-master.txt
# edx-rbac
Expand All @@ -191,7 +191,7 @@ edx-opaque-keys[django]==2.4.0
# edx-drf-extensions
edx-rbac==1.8.0
# via -r requirements/test-master.txt
edx-rest-api-client==5.5.2
edx-rest-api-client==5.6.0
# via -r requirements/test-master.txt
edx-tincan-py35==1.0.0
# via -r requirements/test-master.txt
Expand Down Expand Up @@ -233,7 +233,7 @@ markupsafe==2.1.3
# via
# -r requirements/test-master.txt
# jinja2
newrelic==8.8.1
newrelic==8.10.0
# via
# -r requirements/test-master.txt
# edx-django-utils
Expand Down Expand Up @@ -262,6 +262,10 @@ pgpy==0.6.0
# via -r requirements/test-master.txt
pillow==9.5.0
# via -r requirements/test-master.txt
platformdirs==3.8.1
# via
# -r requirements/test-master.txt
# snowflake-connector-python
pluggy==1.2.0
# via pytest
prompt-toolkit==3.0.39
Expand Down Expand Up @@ -371,7 +375,7 @@ slumber==0.7.1
# edx-rest-api-client
snowballstemmer==2.2.0
# via sphinx
snowflake-connector-python==3.0.4
snowflake-connector-python==3.1.0
# via -r requirements/test-master.txt
sortedcontainers==2.4.0
# via
Expand Down Expand Up @@ -419,6 +423,10 @@ toml==0.10.2
# via pytest
tomli==2.0.1
# via doc8
tomlkit==0.12.1
# via
# -r requirements/test-master.txt
# snowflake-connector-python
typing-extensions==4.7.1
# via
# -r requirements/test-master.txt
Expand Down
Loading