tasks.py 1.72 KB
Newer Older
1
import vars
root's avatar
root committed
2
import sys
3
import json
4
import time
Krish Moodbidri's avatar
Krish Moodbidri committed
5
import signal
6
7
8

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

sys.path.append('/cm/shared/rabbitmq_agents/')
11
import rc_util
12

13
broker_url = vars.broker_url
14
15
celery = Celery('flask_user_reg', broker=broker_url)

16
socketio = SocketIO(message_queue=vars.message_queue)
Krish Moodbidri's avatar
Krish Moodbidri committed
17
timeout = 60
18

19
20
21
22
23
24
25
26
27
28
29
30
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)

Krish Moodbidri's avatar
Krish Moodbidri committed
31
32
    rc_util.rc_rmq.stop_consume()
    rc_util.rc_rmq.delete_queue()
33

34
35
36
def send_msg(event, room):
   print("Post '{}' to room '{}'".format(event,room))
   socketio.emit(event, room=room)
37

Krish Moodbidri's avatar
Krish Moodbidri committed
38
39
40
def timeout_handler(signum, frame):
    print("Process timeout, there's might some issue with agents")
    rc_util.rc_rmq.stop_consume()
41

42
@celery.task
43
def celery_create_account(json, session):
44
    room = session
45
46
47
48
49
    username= json['username'] 
    email= json['email']
    fullname= json['fullname']
    reason= json['reason']

50
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
51
    send_msg('creating account', room)
52
    print(username)
53
    rc_util.add_account(username, email, fullname, reason)
root's avatar
root committed
54
    print('sent account info')
Krish Moodbidri's avatar
Krish Moodbidri committed
55
56
57
58
59
60
    
    # Set initial timeout timer
    signal.signal(signal.SIGALRM, timeout_handler)
    signal.setitimer(signal.ITIMER_REAL, timeout)


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