atlan-collect / notifications / consumer.py
consumer.py
Raw
import pika, os, sys, time

from send import emailSender

def main():
  # rabbitmq connection
  connection = pika.BlockingConnection(
    pika.ConnectionParameters(host="rabbitmq")
  )

  channel = connection.channel()

  def callback(ch, method, properties, body):
    err = emailSender.notification(body)
    if err:
      ch.basic_nack(delivery_tag=method.delivery_tag)
    else:
      ch.basic_ack(delivery_tag=method.delivery_tag)
  
  channel.basic_consume(
    queue=os.environ.get("NOTIFICATION_QUEUE"),
    on_message_callback = callback
  )

  print("Waiting for messages. To exit press CTRL+C")

  channel.start_consuming()

if __name__=="__main__":
  try:
    main()
  except KeyboardInterrupt:
    print("Interrupted")
    try:
      sys.exit(0)
    except SystemExit:
      os._exit(0)