tasks.py 1.42 KB
Newer Older
1
import vars
root's avatar
root committed
2
import sys
3
import json
4
5
6
7
import time

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

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

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

15
socketio = SocketIO(message_queue=vars.message_queue)
16

17
18
19
20
21
22
23
24
25
26
27
28
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
29
30
    rc_util.rc_rmq.stop_consume()
    rc_util.rc_rmq.delete_queue()
31

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

36

37
@celery.task
38
def celery_create_account(json, session):
39
    room = session
40
41
42
43
44
    username= json['username'] 
    email= json['email']
    fullname= json['fullname']
    reason= json['reason']

45
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
46
    send_msg('creating account', room)
47
    print(username)
48
    rc_util.add_account(username, email, fullname, reason)
root's avatar
root committed
49
50
    print('sent account info')
    print('Waiting for completion...')
51
52
    rc_util.consume(username, routing_key=f'complete.{username}', callback=callback)
    send_msg('account ready', room)