Skip to content
Snippets Groups Projects
run.py 3.29 KiB
Newer Older
import pika
import sys

config_name = os.getenv('FLASK_CONFIG')
app = create_app(config_name)
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
def messageReceived(methods=['GET', 'POST']):
    print('message was received!!!')


def check_dir(user, interval):
    # Todo: Make this mothod in a consumer

    """
    :param user: (string) username to check for in DB.
    :param interval: (int) Frequency to check in seconds.
    :return: (boolean) if account has been registered.
    """
    seconds = 0

    while seconds < 600:
        querystring = "_" + user + ".done"

        for filename in os.listdir("flat_db/"):
            if filename.endswith(querystring):
                return True
        time.sleep(interval)
        seconds = seconds + interval

    return False


@socketio.on('user connect')
def handle_my_custom_event(json, methods=['GET', 'POST']):
    username = json["user"]
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' connected.')


@socketio.on('user data')
def ingest_data(json, methods=['GET', 'POST']):
    print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json))
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()

    channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

    message = "Hey there" # todo: account info goes here

    channel.basic_publish(
        exchange='direct_logs', routing_key="ohpc", body=message)
    print(" [x] Sent %r:%r" % ("ohpc", message))

    channel.basic_publish(
        exchange='direct_logs', routing_key="ood", body=message)
    print(" [x] Sent %r:%r" % ("ood", message))
    connection.close()

    # Todo: Make the remaining portion of this method in a RabbitMQ consumer

    # try:
    #     fullname = json["fullname"]
    #     reason = json["reason"]
    #     username = json["username"]
    #
    #     time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S")
    #     directory = "flat_db/"
    #     complete_file_name = os.path.join(directory, time_stamp + "_" + username + ".txt")
    #
    #     if not os.path.exists(directory):
    #         os.makedirs(directory)
    #
    #     file = open(complete_file_name, "w")  # create time stamped file to be queued
    #
    #     file.write(fullname + "\n")
    #     file.write(reason)
    #
    #     file.close()
    #     print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
    #     socketio.emit("creating account")
    #
    # except Exception as e:
    #     print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in directory creation: ", e)
    #     socketio.emit("Account creation failed")


@socketio.on("validate creation")
def creation_confirmation(json, methods=['GET', 'POST']):
    # Todo: Make this mthod in a RabbitMQ consumer

    username = json["username"]

    if check_dir(username, 10):
        print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
        socketio.emit("Account created")
    else:
        socketio.emit("Account creation failed")