Skip to content

Instantly share code, notes, and snippets.

@antunesleo
Last active May 12, 2023 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antunesleo/aeba5ada681e9da82d86ca6f988393ea to your computer and use it in GitHub Desktop.
Save antunesleo/aeba5ada681e9da82d86ca6f988393ea to your computer and use it in GitHub Desktop.
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