diff --git a/app/__init__.py b/app/__init__.py index a5451c13da702f4cc02e45400de13c43ad2c6da8..5f540a45c247aabc648ae3b19a2e21bc247d5437 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,21 +1,11 @@ - # 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): diff --git a/requirements.txt b/requirements.txt index 31547a2ba88d80ac665460b43427e1a953f5a56a..86d3ce5522b5e58199518a42873deefdfcadd732 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/run.py b/run.py index f5272d5d51efe88e194dd2eaa2e0ce7ddd97b0f8..d671071aa9fab6b02e2d28b470658d10c836001c 100644 --- a/run.py +++ b/run.py @@ -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):