Skip to content

Instantly share code, notes, and snippets.

@naveed125
Created February 15, 2020 03:46
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 naveed125/88b6726e9c925ececa0c05edc07ca4f4 to your computer and use it in GitHub Desktop.
Save naveed125/88b6726e9c925ececa0c05edc07ca4f4 to your computer and use it in GitHub Desktop.
Flask server that accepts jobs and pushes tasks to a RabbitMQ queue.
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