Skip to content
Snippets Groups Projects
run.py 2.28 KiB
Newer Older
nick's avatar
nick committed
# run.py

import os
import time
Mitchell Moore's avatar
Mitchell Moore committed
import pika
from flask_socketio import SocketIO
nick's avatar
nick committed

from app import create_app

config_name = os.getenv('FLASK_CONFIG')
app = create_app(config_name)
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
Mitchell Moore's avatar
Mitchell Moore committed
socketio = SocketIO(app)
nick's avatar
nick committed

nick's avatar
nick committed

def messageReceived(methods=['GET', 'POST']):
    print('message was received!!!')


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.
    :return: (boolean) if account has been registered.
    """
    seconds = 0

    while seconds < 600:
        querystring = "_" + user + ".done"

        for filename in os.listdir("flat_db/"):
            if filename.endswith(querystring):
                return True
        time.sleep(interval)
        seconds = seconds + interval

    return False


def create_account(username, fullname, reason):
Mitchell Moore's avatar
Mitchell Moore committed
    print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
Mitchell Moore's avatar
Mitchell Moore committed
    socketio.emit("creating account")
    signal.signal(signal.SIGALRM, account_agent)
    signal.alarm(5)
    # Todo: Code to create a consumer based on the username goes here
    # Todo: Goal is to have it listening for confirmation.
    global username_global
    username = username_global
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
    socketio.emit("account ready")
@socketio.on('user connected')
def user_connected(json, methods=['GET', 'POST']):
    username = json["user"]
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' connected.')
Mitchell Moore's avatar
Mitchell Moore committed
@socketio.on('request account')
def request_account(json, methods=['GET', 'POST']):
    print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json))
        create_account(json['username'], json['fullname'], json['reason'])

    except Exception as e:
        print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in account creation: ", e)
        socketio.emit("Account creation failed")
nick's avatar
nick committed
if __name__ == '__main__':
Mitchell Moore's avatar
Mitchell Moore committed
   # app.run()
    socketio.run(app)