run.py 2 KB
Newer Older
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
11
12
13
14
15

from app import create_app

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

19
20
# global username_global
# global room_global
21

22

23
24
25
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')
26
    socketio.emit("creating account", room=room_global)
27
28
    signal.signal(signal.SIGALRM, account_agent)
    signal.alarm(5)
29

Mitchell Moore's avatar
Mitchell Moore committed
30

31
32
33
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.
34
35
36
    username = session['username']
    room = str(session['uid'])

37
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
38
    socketio.emit("account ready", room=room)
39
    return True
40
41


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

45
    username = json["user"]
46
    room = str(session['uid'])
47

48
    join_room(room)
49

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


54
55
@socketio.on('request account')
def request_account(json, methods=['GET', 'POST']):
Mitchell Moore's avatar
Mitchell Moore committed
56
    print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json))
57
58
59
    print(json)
    room = str(session['uid'])
    socketio.emit("creating account", room=room)
60
    try:
61
62
        # create_account(json['username'], json['fullname'], json['reason'])
        tasks.celery_create_account(json['username'], json['fullname'], json['reason'], room, socketio)
63
64
65

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


69
if __name__ == '__main__':
70
   # app.run()
71
    socketio.run(app, host='0.0.0.0')