Commit 6a84c663 authored by Mitchell Moore's avatar Mitchell Moore
Browse files

Basic RabbitMQ implementation

Add rough Todo comments for RabbitMQ implementation
Update requirements
Add tutorial code with routing capability
Remove unnecessary imports
parent 9b66e0dc
# app/__init__.py
# app/__init__.py
# local imports
from __future__ import print_function
import os
import sys
import subprocess
import time
# third-party imports
from flask import Flask, redirect, url_for, request, render_template, flash, session
from flask_wtf import FlaskForm
from flask_bootstrap import Bootstrap
from wtforms import StringField, SubmitField, TextAreaField, validators
from flask_socketio import SocketIO
# global declarations
def create_app(config_name):
......
......@@ -34,6 +34,7 @@ oauth2==1.9.0.post1
oauthlib==3.1.0
pathtools==0.1.2
pbr==5.4.3
pika==1.1.0
python-dateutil==2.8.1
python-editor==1.0.4
python-engineio==3.10.0
......
......@@ -2,6 +2,8 @@
import os
import time
import pika
import sys
from app import create_app
from flask_socketio import SocketIO
......@@ -18,6 +20,8 @@ def messageReceived(methods=['GET', 'POST']):
def check_dir(user, interval):
# Todo: Make this mothod in a consumer
"""
:param user: (string) username to check for in DB.
:param interval: (int) Frequency to check in seconds.
......@@ -47,34 +51,51 @@ def handle_my_custom_event(json, methods=['GET', 'POST']):
def ingest_data(json, methods=['GET', 'POST']):
print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json))
try:
fullname = json["fullname"]
reason = json["reason"]
username = json["username"]
time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S")
directory = "flat_db/"
complete_file_name = os.path.join(directory, time_stamp + "_" + username + ".txt")
if not os.path.exists(directory):
os.makedirs(directory)
file = open(complete_file_name, "w") # create time stamped file to be queued
file.write(fullname + "\n")
file.write(reason)
file.close()
print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
socketio.emit("creating account")
except Exception as e:
print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in directory creation: ", e)
socketio.emit("Account creation failed")
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs', exchange_type='direct')
severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(
exchange='direct_logs', routing_key=severity, body=message)
print(" [x] Sent %r:%r" % (severity, message))
connection.close()
# Todo: Make the remaining protion of this method in a RabbitMQ consumer
# try:
# fullname = json["fullname"]
# reason = json["reason"]
# username = json["username"]
#
# time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S")
# directory = "flat_db/"
# complete_file_name = os.path.join(directory, time_stamp + "_" + username + ".txt")
#
# if not os.path.exists(directory):
# os.makedirs(directory)
#
# file = open(complete_file_name, "w") # create time stamped file to be queued
#
# file.write(fullname + "\n")
# file.write(reason)
#
# file.close()
# print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
# socketio.emit("creating account")
#
# except Exception as e:
# print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in directory creation: ", e)
# socketio.emit("Account creation failed")
@socketio.on("validate creation")
def creation_confirmation(json, methods=['GET', 'POST']):
# Todo: Make this mthod in a RabbitMQ consumer
username = json["username"]
if check_dir(username, 10):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment