-
Notifications
You must be signed in to change notification settings - Fork 6
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
build: local event producer #243
Merged
Merged
Changes from all commits
Commits
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
Empty file.
Empty file.
96 changes: 96 additions & 0 deletions
96
enterprise_subsidy/apps/core/management/commands/produce_enterprise_ping_event.py
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,96 @@ | ||
""" | ||
Produce a single event for enterprise-specific testing or health checks. | ||
|
||
Implements required ``APP.management.commands.*.Command`` structure. | ||
""" | ||
import logging | ||
import uuid | ||
|
||
import attr | ||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from edx_event_bus_kafka.internal.producer import create_producer | ||
from openedx_events.data import EventsMetadata | ||
from openedx_events.tooling import OpenEdxPublicSignal | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
# First define the topic that our consumer will subscribe to. | ||
ENTERPRISE_CORE_TOPIC = getattr(settings, 'EVENT_BUS_ENTERPRISE_CORE_TOPIC', 'enterprise-core') | ||
|
||
|
||
# Define the shape/schema of the data that our consumer will process. | ||
# It should be identical to the schema used to *produce* the event. | ||
@attr.s(frozen=True) | ||
class PingData: | ||
""" | ||
Attributes of a ping record. | ||
""" | ||
ping_uuid = attr.ib(type=str) | ||
ping_message = attr.ib(type=str) | ||
|
||
|
||
ENTERPRISE_PING_DATA_SCHEMA = { | ||
"ping": PingData, | ||
} | ||
|
||
# Define the key field used to serialize and de-serialize the event data. | ||
ENTERPRISE_PING_KEY_FIELD = 'ping.ping_uuid' | ||
|
||
# Define a Signal with the type (unique name) of the event to process, | ||
# and tell it about the expected schema of event data. The producer of our ping events | ||
# should emit an identical signal (same event_type and data schema). | ||
ENTERPRISE_PING_SIGNAL = OpenEdxPublicSignal( | ||
event_type="org.openedx.enterprise.core.ping.v1", | ||
data=ENTERPRISE_PING_DATA_SCHEMA | ||
) | ||
|
||
|
||
def ping_event_data(): | ||
""" | ||
Helper to produce a dictionary of ping event data | ||
that fits the schema defined above by ``PingData`` and the | ||
``data`` expected by our Ping Signal. | ||
""" | ||
return { | ||
'ping': { | ||
'ping_uuid': str(uuid.uuid4()), | ||
'ping_message': 'hello, world', | ||
} | ||
} | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Management command to produce a test event to the event bus. | ||
""" | ||
help = """ | ||
Produce a single ping event to the configured test topic. | ||
|
||
example: | ||
./manage.py produce_enterprise_ping_event | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--topic', nargs=1, required=False, | ||
help="Optional topic to produce to (without environment prefix)", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
try: | ||
producer = create_producer() | ||
# breakpoint() | ||
producer.send( | ||
signal=ENTERPRISE_PING_SIGNAL, | ||
topic=ENTERPRISE_CORE_TOPIC, | ||
event_key_field=ENTERPRISE_PING_KEY_FIELD, | ||
event_data=ping_event_data(), | ||
event_metadata=EventsMetadata( | ||
event_type=ENTERPRISE_PING_SIGNAL.event_type, | ||
), | ||
) | ||
producer.prepare_for_shutdown() # otherwise command may exit before delivery is complete | ||
except Exception: # pylint: disable=broad-exception-caught | ||
logger.exception("Error producing Kafka event") |
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 |
---|---|---|
|
@@ -34,5 +34,5 @@ tomli==2.0.1 | |
# tox | ||
tox==4.15.0 | ||
# via -r requirements/ci.in | ||
virtualenv==20.26.1 | ||
virtualenv==20.26.2 | ||
# via tox |
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.
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.
Are we going to define more than a small base set of signals in every repo, or is the idea they can be defined DRY in a shared repo?
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.
These signals are really for demo/testing purposes - our production-use signals will be defined in openedx-events: openedx/openedx-events#347