Skip to content

Commit

Permalink
Add Azure Service Bus message queueing
Browse files Browse the repository at this point in the history
Send a smaller payload (name, asset_id and location) to Azure Service Bus
queue.
  • Loading branch information
Mahmud committed Jun 2, 2016
1 parent 6389965 commit 10ca13d
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 109 deletions.
11 changes: 11 additions & 0 deletions .idea/ShadowCat.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

330 changes: 221 additions & 109 deletions .idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions ShadowCat/FlaskCat/FlaskCat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@
DB_COLL_DEVICES = DB_DATABASE.httpDevices
DB_COLL_PINGS = DB_DATABASE.pings
DB_COLL_HISTORY = DB_DATABASE.history

# Azure Service Bus configurations
SVC_BUS_NAMESPACE = 'pyzure'
SVC_BUS_ACCESS_KEY_NAME = 'RootManageSharedAccessKey'
SVC_BUS_ACCESS_KEY_VALUE = 'BhSjDnj6xsHC5tmd73qRlZKdHYWdM6RM3YyF028hPH4='
QUEUE_NAME = 'taskqueue'
QUEUE_MAX_SIZE = '5120'
QUEUE_MSG_TTL = 'PT1M'
19 changes: 19 additions & 0 deletions ShadowCat/FlaskCat/FlaskCat/ping_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from models import User
from bson.json_util import dumps
from datetime import datetime
from servicebus_queue import ServiceBusQueue
import pymongo
import utilities
import logging
Expand All @@ -11,6 +12,14 @@
CORS(app)
app.config.from_object('config')

# azure servicebus queue
svc = ServiceBusQueue(
app.config['SVC_BUS_NAMESPACE'],
app.config['SVC_BUS_ACCESS_KEY_NAME'],
app.config['SVC_BUS_ACCESS_KEY_VALUE'],
app.config['QUEUE_NAME']
)


@app.route('/api/ping', methods=['POST'])
def ping_location():
Expand All @@ -23,6 +32,8 @@ def ping_location():
data = get_payload(json_data)
add_ping(data)
add_history(data)
payload = get_azure_payload(data)
svc.send(payload)

return dumps(''), 201, {'Content-Type': 'application/json'}

Expand All @@ -40,6 +51,14 @@ def get_payload(raw_data):
)


def get_azure_payload(data):
return {
'name': data.name,
'asset_id': data.asset_id,
'location': data.point
}


def add_ping(data):
try:
app.config['DB_COLL_PINGS'].update_one(
Expand Down
33 changes: 33 additions & 0 deletions ShadowCat/FlaskCat/FlaskCat/servicebus_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from azure.servicebus import ServiceBusService, Message, Queue
import ast


class ServiceBusQueue(object):
def __init__(self,
namespace,
access_key_name,
access_key_value,
q_name,
q_max_size='5120',
msg_ttl='PT1M'):
self.bus_svc = ServiceBusService(
service_namespace=namespace,
shared_access_key_name=access_key_name,
shared_access_key_value=access_key_value
)
self.queue_options = Queue()
self.queue_options.max_size_in_megabytes = q_max_size
self.queue_options.default_message_time_to_live = msg_ttl

self.queue_name = q_name
self.bus_svc.create_queue(self.queue_name, self.queue_options)

def send(self, msg):
message = bytes(msg)
message = Message(message)
self.bus_svc.send_queue_message(self.queue_name, message)

def receive(self):
msg = self.bus_svc.receive_queue_message(self.queue_name, peek_lock=False)
data = ast.literal_eval(msg.body)
return data

0 comments on commit 10ca13d

Please sign in to comment.