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

import os
4
import time
5
import signal
6
7
import tasks

8

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

from app import create_app

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

19
global username_global
20
global room_global
21
22
23


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


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


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

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

48
    join_room(room_global)
49

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


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

    try:
59
60
        # create_account(json['username'], json['fullname'], json['reason'])
        tasks.celery_create_account(json['username'], json['fullname'], json['reason'], room_global, socketio)
61
62
63

    except Exception as e:
        print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in account creation: ", e)
64
        socketio.emit("Account creation failed", room=room_global)
65
66


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