run.py 1.78 KB
Newer Older
1
2
3
# run.py

import os
4
import time
5
6
import signal

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

from app import create_app

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

18
global username_global
19

20

21
22
23
24
25
26
27
28
def create_account(username, fullname, reason):
    # Todo: Ravi's and Louis's code goes here
    print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
    global username_global
    username_global = username
    socketio.emit("creating account")
    signal.signal(signal.SIGALRM, account_agent)
    signal.alarm(5)
29

Mitchell Moore's avatar
Mitchell Moore committed
30

31
32
33
34
35
36
37
38
def account_agent(*args):
    # 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")
    return True
39
40


41
42
43
@socketio.on('user connected')
def user_connected(json, methods=['GET', 'POST']):
    username = json["user"]
44

45
    room = str(session['uid'])
46

47
    join_room(room)
48

Mitchell Moore's avatar
Mitchell Moore committed
49
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' connected.')
50
    print('\t\t\t|-----Room ID: ' + room)
51
52


53
54
@socketio.on('request account')
def request_account(json, methods=['GET', 'POST']):
Mitchell Moore's avatar
Mitchell Moore committed
55
    print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json))
56

57
58
59
60
61
    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)
62
63
64
        socketio.emit("Account creation failed")


65
if __name__ == '__main__':
66
67
   # app.run()
    socketio.run(app)