run.py 2.39 KB
Newer Older
nick's avatar
nick committed
1
2
3
# run.py

import os
4
import time
5
import signal
6
7

import pika
8
9
from flask import session
from flask_socketio import SocketIO, join_room
nick's avatar
nick committed
10
11
12
13
14

from app import create_app

config_name = os.getenv('FLASK_CONFIG')
app = create_app(config_name)
15
16
17
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
socketio = SocketIO(app)

18
global username_global
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

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):
47
    # Todo: Ravi's and Louis's code goes here
48
    print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
49
50
    global username_global
    username_global = username
51
    socketio.emit("creating account")
52
53
    signal.signal(signal.SIGALRM, account_agent)
    signal.alarm(5)
54
55


56
def account_agent(*args):
57
58
    # Todo: Code to create a consumer based on the username goes here
    # Todo: Goal is to have it listening for confirmation.
59
60
61
62
    global username_global
    username = username_global
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
    socketio.emit("account ready")
63
64
65
    return True


66
67
@socketio.on('user connected')
def user_connected(json, methods=['GET', 'POST']):
68
    username = json["user"]
69
70
71
72
73

    room = str(session['uid'])

    join_room(room)

74
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' connected.')
75
    print('\t\t\t|-----Room ID: ' + room)
76
77


78
@socketio.on('request account')
79
def request_account(json, methods=['GET', 'POST']):
80
81
82
83
84
85
86
87
88
89
    print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json))

    try:
        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
90
91
if __name__ == '__main__':
    app.run(Debug=True)