Commit 74c915c8 authored by Mitchell Moore's avatar Mitchell Moore Committed by Krish Moodbidri
Browse files

Implemented celery to work with sockeio in flask. Added room to facilitate communication

- fix var in run.py
- Romove old functions
- Sending a message to the client webapp to start layover before starting celery task
- Commenting out socketio emit before calling celery task
- modify the message_queue in socketio app
- Adding on_room fuction taht gets triggered when connection is made, and we need to join a room for the communcation to take place between client and server
- Removing unused functions and commented out lines
parent 17966431
...@@ -5,67 +5,42 @@ import time ...@@ -5,67 +5,42 @@ import time
import signal import signal
import tasks import tasks
from flask import session from flask import session
from flask_socketio import SocketIO, join_room from flask_socketio import SocketIO, join_room
from app import create_app from app import create_app
from gevent import monkey
monkey.patch_all(subprocess=True)
config_name = os.getenv('FLASK_CONFIG') config_name = os.getenv('FLASK_CONFIG')
app = create_app(config_name) app = create_app(config_name)
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#' app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
socketio = SocketIO(app) socketio = SocketIO(app, message_queue='amqp://reggie:reggie@ohpc:5672/socketio')
# global username_global
# global room_global
def create_account(username, fullname, reason): @socketio.on('connect')
# Todo: Ravi's and Louis's code goes here def socket_connect():
print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue') pass
socketio.emit("creating account", room=room_global)
signal.signal(signal.SIGALRM, account_agent)
signal.alarm(5)
@socketio.on('join_room')
def on_room():
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.
username = session['username']
room = str(session['uid']) room = str(session['uid'])
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
socketio.emit("account ready", room=room)
return True
@socketio.on('user connected')
def user_connected(json, methods=['GET', 'POST']):
username = json["user"]
room = str(session['uid'])
join_room(room) join_room(room)
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' connected.')
print('\t\t\t|-----Room ID: ' + room) print('\t\t\t|-----Room ID: ' + room)
@socketio.on('request account') @socketio.on('request account')
def request_account(json, methods=['GET', 'POST']): def request_account(json, methods=['GET', 'POST']):
print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json)) print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json))
sid = str(session['uid'])
room = str(session['uid']) print("Room: {}".format(sid))
try: try:
# create_account(json['username'], json['fullname'], json['reason']) tasks.celery_create_account.delay(json['username'], json['fullname'], json['reason'], session=sid)
tasks.celery_create_account(json['username'], json['fullname'], json['reason'], room, socketio)
except Exception as e: except Exception as e:
print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in account creation: ", e) print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in account creation: ", e)
socketio.emit("Account creation failed", room) socketio.emit("Account creation failed", room)
if __name__ == '__main__': if __name__ == '__main__':
# app.run()
socketio.run(app, host='0.0.0.0') socketio.run(app, host='0.0.0.0')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment