run.py 1.87 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
global room_global
20
21
22


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


30
def account_agent(*args):
31
32
    # Todo: Code to create a consumer based on the username goes here
    # Todo: Goal is to have it listening for confirmation.
33
    global username_global
34
35
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username_global)
    socketio.emit("account ready", room=room_global)
36
37
38
    return True


39
40
@socketio.on('user connected')
def user_connected(json, methods=['GET', 'POST']):
41
42
    global username_global
    global room_global
43

44
45
    username_global = json["user"]
    room_global = str(session['uid'])
46

47
    join_room(room_global)
48

49
50
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username_global + ' connected.')
    print('\t\t\t|-----Room ID: ' + room_global)
51
52


53
@socketio.on('request account')
54
def request_account(json, methods=['GET', 'POST']):
55
56
57
58
59
60
61
    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)
62
        socketio.emit("Account creation failed", room=room_global)
63
64


nick's avatar
nick committed
65
if __name__ == '__main__':
66
67
   # app.run()
    socketio.run(app, host='0.0.0.0')