Newer
Older

Krish Moodbidri
committed
import time

Krish Moodbidri
committed
from celery import Celery
from flask_socketio import SocketIO
Ravi Tripathi
committed
sys.path.append(vars.rabbitmq_agents_loc)

Krish Moodbidri
committed
import rc_util
Mitchell Moore
committed
broker_url = vars.broker_url
celery = Celery(vars.celery_app, broker=broker_url)
Mitchell Moore
committed
socketio = SocketIO(message_queue=vars.message_queue)
Mitchell Moore
committed

Krish Moodbidri
committed
def gen_f(room):
def callback(channel, method, properties, body):
msg = json.loads(body)
username = msg['username']

Krish Moodbidri
committed
if msg['success']:
print(f'Account for {username} has been created.')
send_msg('account ready', room)
else:
print(f"There's some issue while creating account for {username}")
errmsg = msg.get('errmsg', [])
for err in errmsg:
print(err)
socketio.emit('account error', errmsg, room= room)
rc_util.rc_rmq.stop_consume()
rc_util.rc_rmq.delete_queue(queuename)

Krish Moodbidri
committed
return callback
Mitchell Moore
committed
def certify_gen_f(room):
def callback(channel, method, properties, body):
msg = json.loads(body)
username = msg['username']
if msg['success']:
print(f'Account for {username} has been certified.')
send_msg('certified', room)
else:
print(f"There's some issue while certifying account for {username}")
errmsg = msg.get('errmsg', [])
for err in errmsg:
print(err)
socketio.emit('certify error', errmsg, room= room)
rc_util.rc_rmq.stop_consume()
rc_util.rc_rmq.delete_queue(queuename)
Ravi Tripathi
committed
def send_msg(event, room):

Krish Moodbidri
committed
socketio.emit(event, room=room)
Mitchell Moore
committed
def timeout_handler(signum, frame):
print("Process timeout, there's might some issue with agents")
socketio.emit('account error', errmsg, room= room)
Mitchell Moore
committed
Mitchell Moore
committed
@celery.task
def celery_create_account(json, session):
Ravi Tripathi
committed
room = session
username= json['username']
email= json['email']
fullname= json['fullname']
reason= json['reason']
queuename= rc_util.encode_name(username)
updated_by= f'{username}'
host= vars.app_host
Mitchell Moore
committed
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
Ravi Tripathi
committed
send_msg('creating account', room)
Mitchell Moore
committed
print(username)
rc_util.add_account(username, queuename, email, fullname, reason, updated_by, host)
rc_util.consume(queuename, routing_key=f'complete.{queuename}', callback=gen_f(room))
@celery.task
def celery_certify_account(json, session):
room = session
username= json['username']
email= json['email']
fullname= json['fullname']
queuename= rc_util.encode_name(username)
updated_by= f'{username}'
print("CERTIFY : "+time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
send_msg('certifying account', room)
print(username)
rc_util.certify_account(username, queuename, 'ok', 'all', updated_by, host)
print('sent account info')
print('Waiting for certification...')
rc_util.consume(queuename, routing_key=f'certified.{queuename}', callback=certify_gen_f(room))