Skip to content

Instantly share code, notes, and snippets.

@DhruvaDave
Last active December 27, 2020 17:24
Show Gist options
  • Save DhruvaDave/f0f71094a3d0b2980c4f7a5a0390d4a1 to your computer and use it in GitHub Desktop.
Save DhruvaDave/f0f71094a3d0b2980c4f7a5a0390d4a1 to your computer and use it in GitHub Desktop.
from flask import Flask
import pika
app = Flask(__name__)
@app.route('/')
def index():
return 'OK'
@app.route('/create-job/<msg>')
def add(cmd):
try:
connection = pika.BlockingConnection(pika.ConnectionParameters(host="rabbitmq"))
except pika.exceptions.AMQPConnectionError as exc:
print("Failed to connect to RabbitMQ service. Message wont be sent.")
return
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
channel.basic_publish(
exchange='',
routing_key='task_queue',
body=cmd,
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
))
connection.close()
return " ___ Sent: %s" % cmd
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
FROM ubuntu:18.04
RUN apt-get update -y && \
apt-get install -y python3-pip python3-dev
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install pika && \
pip3 install -r requirements.txt
COPY . /app
ENTRYPOINT [ "python3" ]
CMD [ "app.py" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment