Skip to content

Commit

Permalink
Added the mq code in views.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya committed Mar 3, 2022
1 parent d55edff commit bc8e006
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion plot-weather-microservice/plot_weather/plot_graph/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import gzip
from locale import CODESET
import urllib
from datetime import datetime
import pika, sys, os

import nexradaws
import pyart
Expand Down Expand Up @@ -43,6 +45,7 @@ def createGraph(self, filename):
display.set_limits(xlim=(-500, 500), ylim=(-500, 500), ax=ax)
return plt

#The input received from the MQ will be passed to this function
def post(self, request):
b64 = []
json_str = json.dumps(request.data)
Expand All @@ -61,5 +64,63 @@ def post(self, request):
'uri':b64
}"""

# Add the mq response code

#return Response(resp)
return Response(b64,content_type='image/jpg')
#Remove this line if not required
return Response(b64,content_type='image/jpg')



""""
RABBITMQ CODE
"""""
# establish connection with rabbitmq server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

#create/ declare queue
channel.queue_declare(queue='plot_queue')


# subscribe a callback function to the queue. This callback function is called by the pika library
# It does the work and sends the response back.
def on_request(ch, method, props, body):
# our message content will be in body
n = int(body)

print(" [.] fib(%s)" % n)

# instead of fib here our plot graph function should be called and response should be saved here
response = fib(n)

ch.basic_publish(exchange='',
routing_key=props.reply_to,
properties=pika.BasicProperties(correlation_id = \
props.correlation_id),
body=str(response))
ch.basic_ack(delivery_tag=method.delivery_tag)

# We might want to run more than one server process.
# In order to spread the load equally over multiple servers we need to set the prefetch_count setting.
channel.basic_qos(prefetch_count=1)

# We declare a callback "on_request" for basic_consume, the core of the RPC server. It's executed when the request is received.
channel.basic_consume(queue='plot_queue', on_message_callback=on_request)

print(" [x] Awaiting RPC requests")
channel.start_consuming()

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()



def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)

0 comments on commit bc8e006

Please sign in to comment.