Last active
May 12, 2023 17:01
-
-
Save antunesleo/aeba5ada681e9da82d86ca6f988393ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import json | |
import sys | |
from kafka import KafkaProducer | |
from outboxexample import settings | |
import signal | |
producer = KafkaProducer(bootstrap_servers=[settings.KAFKA_HOST]) | |
def close_producer(signal, frame): | |
print("Closing Kafka producer connection...") | |
producer.close() | |
print("Kafka producer connection closed.") | |
sys.exit(0) | |
signal.signal(signal.SIGTERM, close_producer) | |
def dispatch_to_kafka(topic, message): | |
encoded_message = json.dumps(message).encode("utf-8") | |
producer.send(topic, encoded_message) | |
producer.flush() | |
print(f"{message['name']} event published to Kafka") | |
NOTES_TOPIC = "notes" | |
def publish_note_created(note_dict): | |
message = { | |
"name": "note-created", | |
"note": note_dict, | |
} | |
dispatch_to_kafka(NOTES_TOPIC, message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment