Created
February 15, 2020 03:46
-
-
Save naveed125/88b6726e9c925ececa0c05edc07ca4f4 to your computer and use it in GitHub Desktop.
Flask server that accepts jobs and pushes tasks to a RabbitMQ queue.
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
from flask import Flask | |
import pika | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
return 'OK' | |
@app.route('/add-job/<cmd>') | |
def add(cmd): | |
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq')) | |
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 " [x] Sent: %s" % cmd | |
if __name__ == '__main__': | |
app.run(debug=True, host='0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment