tasks.py 1.5 KB
Newer Older
1
2
3
4
from celery import Celery
import time
from flask_socketio import SocketIO
import subprocess
5
import vars
root's avatar
root committed
6
import sys
7
import json
root's avatar
root committed
8
9

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

from gevent import monkey
monkey.patch_all(subprocess=True)

15
broker_url = vars.broker_url
16
17
celery = Celery('flask_user_reg', broker=broker_url)

18
socketio = SocketIO(message_queue=vars.message_queue)
19

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

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

39

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

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