Skip to content
Snippets Groups Projects
rc_util.py 1.07 KiB
Newer Older
Bo-Chun Chen's avatar
Bo-Chun Chen committed
from rc_rmq import RCRMQ
import json

Bo-Chun Chen's avatar
Bo-Chun Chen committed
rc_rmq = RCRMQ({'exchange': 'Register'})
confirm_rmq = RCRMQ({'exchange': 'Confirm'})
Bo-Chun Chen's avatar
Bo-Chun Chen committed
tasks = {'ohpc_account': False, 'ohpc_homedir': False, 'ood_account': False, 'slurm_account': False}

def add_account(username, full='', reason=''):
  rc_rmq.publish_msg({
    'routing_key': 'ohpc_account',
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    'msg': {
      "username": username,
      "fullname": full,
      "reason": reason
    }
  })

def worker(ch, method, properties, body):
    msg = json.loads(body)
    task = msg['task']
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    print("get msg: {}".format(task))
    tasks[task] = msg['success']
Bo-Chun Chen's avatar
Bo-Chun Chen committed

    # Check if all tasks are done
    done = True
    for key, status in tasks.items():
        if not status:
Bo-Chun Chen's avatar
Bo-Chun Chen committed
            print("{} is not done yet.".format(key))
Bo-Chun Chen's avatar
Bo-Chun Chen committed
            done = False 
    if done:
        confirm_rmq.stop_consume()
        confirm_rmq.delete_queue()
Bo-Chun Chen's avatar
Bo-Chun Chen committed
  
def consume(username, callback, debug=False):
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    if debug:
        sleep(5)
    else:
        confirm_rmq.start_consume({
Bo-Chun Chen's avatar
Bo-Chun Chen committed
            'queue': username,
            'cb': callback
Bo-Chun Chen's avatar
Bo-Chun Chen committed
        })
  
    return { 'success' : True }