Skip to content
Snippets Groups Projects
tasks.py 1.72 KiB
Newer Older
root's avatar
root committed
import sys
Krish Moodbidri's avatar
Krish Moodbidri committed
import signal

from celery import Celery
from flask_socketio import SocketIO
root's avatar
root committed

sys.path.append('/cm/shared/rabbitmq_agents/')
broker_url = vars.broker_url
celery = Celery('flask_user_reg', broker=broker_url)

socketio = SocketIO(message_queue=vars.message_queue)
Krish Moodbidri's avatar
Krish Moodbidri committed
timeout = 60
def callback(channel, method, properties, body):
    msg = json.loads(body)
    username = msg['username']

    if msg['success']:
        print(f'Account for {username} has been created.')
    else:
        print(f"There's some issue while creating account for {username}")
        errmsg = msg.get('errmsg', [])
        for err in errmsg:
            print(err)

    rc_util.rc_rmq.stop_consume()
    rc_util.rc_rmq.delete_queue()
def send_msg(event, room):
   print("Post '{}' to room '{}'".format(event,room))
   socketio.emit(event, room=room)
Krish Moodbidri's avatar
Krish Moodbidri committed
def timeout_handler(signum, frame):
    print("Process timeout, there's might some issue with agents")
    rc_util.rc_rmq.stop_consume()
def celery_create_account(json, session):
    username= json['username'] 
    email= json['email']
    fullname= json['fullname']
    reason= json['reason']

    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
    rc_util.add_account(username, email, fullname, reason)
root's avatar
root committed
    print('sent account info')
Krish Moodbidri's avatar
Krish Moodbidri committed
    
    # Set initial timeout timer
    signal.signal(signal.SIGALRM, timeout_handler)
    signal.setitimer(signal.ITIMER_REAL, timeout)


root's avatar
root committed
    print('Waiting for completion...')
    rc_util.consume(username, routing_key=f'complete.{username}', callback=callback)
    send_msg('account ready', room)