run.py 1.94 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
20
# global username_global
# 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
35
36
37
38
    username = session['username']
    room = str(session['uid'])

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


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

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

48
    join_room(room)
49

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


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

58
59
    room = str(session['uid'])

60
    try:
61
        # create_account(json['username'], json['fullname'], json['reason'])
62
        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


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